Files
ewoooc/scripts/archive/check_monthly_stats.py
ogt 1b4f3a7bbe
Some checks failed
CD Pipeline / deploy (push) Failing after 59s
feat: EwoooC 初始化 — 完整專案推版至 Gitea
- 建立 Gitea Actions CD pipeline (.gitea/workflows/cd.yaml)
- 部署模式: rsync Python 檔案至 188 → docker restart (volume mount)
- Dockerfile/requirements 變動時自動重建 Docker image
- 部署通知: Telegram (開始/成功/失敗)
- 健康檢查: https://mo.wooo.work/health (最多 5 次重試)
- 同步最新 CLAUDE.md / ADR-008 / memory (2026-04-19)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 01:21:13 +08:00

53 lines
1.7 KiB
Python

import pandas as pd # type: ignore
import os
import sys
# 設定專案路徑以導入模組
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from database.manager import DatabaseManager
def check_monthly_stats():
print("📊 正在分析資料庫中的月度業績數據...")
db = DatabaseManager()
try:
# 1. 取得資料表欄位名稱
df_head = pd.read_sql("SELECT * FROM realtime_sales_monthly LIMIT 1", db.engine)
cols = df_head.columns.tolist()
# 自動尋找日期欄位
date_col = next((c for c in cols if '日期' in c or 'Date' in c), None)
if not date_col:
print(f"❌ 找不到日期欄位。現有欄位: {cols}")
return
print(f"📅 偵測到日期欄位: {date_col}")
# 2. 讀取日期資料 (只讀取日期欄位以節省記憶體)
df = pd.read_sql(f"SELECT \"{date_col}\" FROM realtime_sales_monthly", db.engine)
if df.empty:
print("⚠️ 資料表為空。")
return
# 3. 轉換與統計
df[date_col] = pd.to_datetime(df[date_col], errors='coerce')
monthly_counts = df.groupby(df[date_col].dt.to_period('M')).size()
print("\n" + "="*30)
print("🗓️ 各月份資料筆數統計")
print("="*30)
if monthly_counts.empty:
print("無法解析日期或無資料。")
else:
for period, count in monthly_counts.items():
print(f"{period}: {count:6d}")
print("="*30)
print(f"總計: {len(df)}")
except Exception as e:
print(f"❌ 讀取或分析失敗: {e}")
if __name__ == "__main__":
check_monthly_stats()