411 lines
14 KiB
Python
411 lines
14 KiB
Python
import os
|
|
import sys
|
|
import json
|
|
import time
|
|
import sqlite3
|
|
import datetime
|
|
import threading
|
|
from typing import Optional, Dict, Any, List
|
|
import requests
|
|
import jdatetime
|
|
from fastapi import FastAPI, HTTPException, Query, Request
|
|
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from pydantic import BaseModel
|
|
import uvicorn
|
|
|
|
# Config & Constants
|
|
DB_PATH = os.environ.get("DB_PATH", "/data/packages.db" if os.path.exists("/data") else "/workspace/dashboard/packages.db")
|
|
METABASE_URL = "https://metabase.ekeepa-dev.ir/api/session"
|
|
CREDS = {"username": "Fallahi.hamed@gmail.com", "password": "r5kivS&JR4d2BfUaS95xCDY^NxX*"}
|
|
|
|
PERSIAN_MONTH_NAMES = [
|
|
"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور",
|
|
"مهر", "آبان", "آذر", "دی", "بهمن", "اسفند"
|
|
]
|
|
|
|
PACKAGE_TYPE_MAP = {
|
|
0: "پکیج طلایی - اتاق فرار",
|
|
1: "پکیج نقره ای - اتاق فرار",
|
|
2: "پکیج برنزی - اتاق فرار",
|
|
3: "پکیج VIP",
|
|
10: "پکیج اختصاصی",
|
|
12: "پکیج تلگرام",
|
|
15: "پکیج کمپین",
|
|
16: "پکیج فصلی"
|
|
}
|
|
|
|
PAYMENT_TYPE_MAP = {
|
|
0: "پرداخت ثابت",
|
|
1: "درصدی",
|
|
3: "ترکیبی"
|
|
}
|
|
|
|
# DB Setup
|
|
def get_db():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
def init_db():
|
|
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS package_channels (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
package_id INTEGER NOT NULL,
|
|
channel TEXT NOT NULL, -- 'telegram', 'story', 'reels_post'
|
|
status TEXT DEFAULT 'pending', -- 'pending', 'completed', 'cancelled'
|
|
execution_date TEXT DEFAULT '',
|
|
note TEXT DEFAULT '',
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE(package_id, channel)
|
|
);
|
|
""")
|
|
cursor.execute("""
|
|
CREATE INDEX IF NOT EXISTS idx_pkg_channel ON package_channels(package_id, channel);
|
|
""")
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS meta_cache (
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
""")
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
init_db()
|
|
|
|
# Metabase & Cache helpers
|
|
CACHED_PACKAGES = []
|
|
LAST_METABASE_SYNC = 0
|
|
|
|
def get_metabase_token():
|
|
for attempt in range(3):
|
|
try:
|
|
res = requests.post(METABASE_URL, json=CREDS, timeout=30)
|
|
res.raise_for_status()
|
|
return res.json().get("id")
|
|
except Exception as e:
|
|
if attempt == 2:
|
|
raise e
|
|
time.sleep(2)
|
|
|
|
def to_jalali_str(dt_str):
|
|
if not dt_str:
|
|
return ""
|
|
try:
|
|
clean = dt_str.split("T")[0]
|
|
parts = clean.split("-")
|
|
g_dt = datetime.date(int(parts[0]), int(parts[1]), int(parts[2]))
|
|
j_dt = jdatetime.date.fromgregorian(date=g_dt)
|
|
return j_dt.strftime("%Y-%m-%d")
|
|
except Exception:
|
|
return dt_str
|
|
|
|
def fetch_all_packages_from_metabase():
|
|
global CACHED_PACKAGES, LAST_METABASE_SYNC
|
|
try:
|
|
token = get_metabase_token()
|
|
headers = {"X-Metabase-Session": token}
|
|
query = """
|
|
SELECT
|
|
ph.id as pkg_id,
|
|
ph.room_id,
|
|
COALESCE(r.name, 'نامشخص') as room_name,
|
|
COALESCE(p.name, 'نامشخص') as brand,
|
|
COALESCE(r.city, 'نامشخص') as city,
|
|
COALESCE(r.district, 'نامشخص') as district,
|
|
ph.advertisement_type,
|
|
ph.payment_type,
|
|
ph.percentage_rate,
|
|
ph.fixed_amount,
|
|
ph.guaranteed_sales_amount,
|
|
ph.start_date,
|
|
ph.end_date,
|
|
ph.notes,
|
|
ph.deleted_at,
|
|
ph.status as raw_status
|
|
FROM package_history ph
|
|
LEFT JOIN rooms r ON ph.room_id = r.id
|
|
LEFT JOIN providers p ON r.provider_id = p.id
|
|
WHERE ph.start_date >= '2025-03-21' OR ph.created_at >= '2025-03-21'
|
|
ORDER BY ph.id DESC
|
|
"""
|
|
res = requests.post(
|
|
"https://metabase.ekeepa-dev.ir/api/dataset",
|
|
headers=headers,
|
|
json={"database": 2, "type": "native", "native": {"query": query}},
|
|
timeout=60
|
|
)
|
|
res.raise_for_status()
|
|
data = res.json()
|
|
cols = [c["name"] for c in data.get("data", {}).get("cols", [])]
|
|
rows = data.get("data", {}).get("rows", [])
|
|
|
|
parsed = []
|
|
for r in rows:
|
|
d = dict(zip(cols, r))
|
|
rate = float(d["percentage_rate"] or 0)
|
|
fixed = int(d["fixed_amount"] or 0)
|
|
status_label = "حذف شده" if d["deleted_at"] else "درحال اجرا"
|
|
pkg_model = PACKAGE_TYPE_MAP.get(d["advertisement_type"], f"پکیج کد {d['advertisement_type']}")
|
|
pay_model = PAYMENT_TYPE_MAP.get(d["payment_type"], "درصدی")
|
|
|
|
start_j = to_jalali_str(d["start_date"])
|
|
end_j = to_jalali_str(d["end_date"])
|
|
|
|
item = {
|
|
"id": d["pkg_id"],
|
|
"room_id": d["room_id"],
|
|
"room_name": d["room_name"],
|
|
"brand": d["brand"],
|
|
"city": d["city"],
|
|
"district": d["district"],
|
|
"status": status_label,
|
|
"package_model": pkg_model,
|
|
"start_date": start_j,
|
|
"end_date": end_j,
|
|
"start_date_g": (d["start_date"] or "")[:10],
|
|
"end_date_g": (d["end_date"] or "")[:10],
|
|
"payment_type": pay_model,
|
|
"fixed_amount": fixed,
|
|
"percentage_rate": rate,
|
|
"guarantee": int(d["guaranteed_sales_amount"] or 0),
|
|
"notes": d["notes"] or "",
|
|
"deleted_at": d["deleted_at"] or ""
|
|
}
|
|
parsed.append(item)
|
|
|
|
CACHED_PACKAGES = parsed
|
|
LAST_METABASE_SYNC = time.time()
|
|
|
|
# Save to SQLite cache for persistent bootstrap
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
cursor.execute("REPLACE INTO meta_cache (key, value, updated_at) VALUES ('packages', ?, CURRENT_TIMESTAMP)", (json.dumps(parsed, ensure_ascii=False),))
|
|
conn.commit()
|
|
conn.close()
|
|
print(f"Loaded and cached {len(parsed)} packages from Metabase.")
|
|
except Exception as e:
|
|
print(f"Error fetching from Metabase: {e}")
|
|
# Try loading from local cache
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT value FROM meta_cache WHERE key='packages'")
|
|
row = cursor.fetchone()
|
|
if row:
|
|
CACHED_PACKAGES = json.loads(row[0])
|
|
print(f"Restored {len(CACHED_PACKAGES)} packages from local cache.")
|
|
conn.close()
|
|
|
|
# Initial load
|
|
threading.Thread(target=fetch_all_packages_from_metabase, daemon=True).start()
|
|
|
|
app = FastAPI(title="Ekeepa Packages BI Dashboard")
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
class ChannelUpdatePayload(BaseModel):
|
|
package_id: int
|
|
channel: str # 'telegram', 'story', 'reels_post'
|
|
status: str # 'pending', 'completed', 'cancelled'
|
|
execution_date: Optional[str] = ""
|
|
note: Optional[str] = ""
|
|
|
|
def get_jalali_range(year: int, month: Optional[int] = None):
|
|
if month is None or month == 0:
|
|
start_j = jdatetime.date(year, 1, 1)
|
|
end_j = jdatetime.date(year, 12, 30 if jdatetime.date(year, 1, 1).isleap() else 29)
|
|
else:
|
|
if month <= 6:
|
|
days = 31
|
|
elif month <= 11:
|
|
days = 30
|
|
else:
|
|
days = 30 if jdatetime.date(year, 1, 1).isleap() else 29
|
|
start_j = jdatetime.date(year, month, 1)
|
|
end_j = jdatetime.date(year, month, days)
|
|
|
|
start_g = start_j.togregorian()
|
|
end_g = end_j.togregorian()
|
|
return {
|
|
"start_j": start_j.strftime("%Y-%m-%d"),
|
|
"end_j": end_j.strftime("%Y-%m-%d"),
|
|
"start_g": start_g.strftime("%Y-%m-%d"),
|
|
"end_g": end_g.strftime("%Y-%m-%d"),
|
|
}
|
|
|
|
@app.get("/api/filter-options")
|
|
def get_filter_options():
|
|
now_j = jdatetime.datetime.now()
|
|
current_year = now_j.year
|
|
current_month = now_j.month
|
|
|
|
# Extract unique cities, package models from cache
|
|
cities = sorted(list(set(p["city"] for p in CACHED_PACKAGES if p["city"] and p["city"] != "نامشخص")))
|
|
models = sorted(list(set(p["package_model"] for p in CACHED_PACKAGES if p["package_model"])))
|
|
|
|
months = [{"num": i + 1, "name": name} for i, name in enumerate(PERSIAN_MONTH_NAMES)]
|
|
years = [1405, 1404]
|
|
|
|
return {
|
|
"current_year": current_year,
|
|
"current_month": current_month,
|
|
"years": years,
|
|
"months": months,
|
|
"cities": cities,
|
|
"models": models
|
|
}
|
|
|
|
@app.get("/api/packages")
|
|
def get_packages(
|
|
year: int = Query(1405),
|
|
month: Optional[int] = Query(None), # None or 0 means all year
|
|
city: Optional[str] = None,
|
|
model: Optional[str] = None,
|
|
status: Optional[str] = None
|
|
):
|
|
global CACHED_PACKAGES
|
|
if not CACHED_PACKAGES:
|
|
# Load from DB if memory is empty
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT value FROM meta_cache WHERE key='packages'")
|
|
row = cursor.fetchone()
|
|
if row:
|
|
CACHED_PACKAGES = json.loads(row[0])
|
|
conn.close()
|
|
|
|
date_range = get_jalali_range(year, month)
|
|
filter_start_g = date_range["start_g"]
|
|
filter_end_g = date_range["end_g"]
|
|
|
|
# Filter overlapping packages
|
|
# A package is active in the period if: pkg.start_date_g <= filter_end_g AND pkg.end_date_g >= filter_start_g
|
|
filtered = []
|
|
for p in CACHED_PACKAGES:
|
|
p_start_g = p.get("start_date_g") or ""
|
|
p_end_g = p.get("end_date_g") or ""
|
|
|
|
# If no dates, consider created date fallback
|
|
if not p_start_g or not p_end_g:
|
|
continue
|
|
|
|
if p_start_g <= filter_end_g and p_end_g >= filter_start_g:
|
|
if city and p.get("city") != city:
|
|
continue
|
|
if model and p.get("package_model") != model:
|
|
continue
|
|
if status and p.get("status") != status:
|
|
continue
|
|
filtered.append(p)
|
|
|
|
# Load channel custom data from SQLite
|
|
pkg_ids = [p["id"] for p in filtered]
|
|
channel_map = {}
|
|
if pkg_ids:
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
placeholders = ",".join("?" * len(pkg_ids))
|
|
cursor.execute(f"""
|
|
SELECT package_id, channel, status, execution_date, note, updated_at
|
|
FROM package_channels
|
|
WHERE package_id IN ({placeholders})
|
|
""", pkg_ids)
|
|
for r in cursor.fetchall():
|
|
pid = r["package_id"]
|
|
ch = r["channel"]
|
|
if pid not in channel_map:
|
|
channel_map[pid] = {}
|
|
channel_map[pid][ch] = {
|
|
"status": r["status"] or "pending",
|
|
"execution_date": r["execution_date"] or "",
|
|
"note": r["note"] or "",
|
|
"updated_at": r["updated_at"] or ""
|
|
}
|
|
conn.close()
|
|
|
|
# Merge channel data into output
|
|
result = []
|
|
for p in filtered:
|
|
item = dict(p)
|
|
p_channels = channel_map.get(p["id"], {})
|
|
|
|
item["telegram"] = p_channels.get("telegram", {
|
|
"status": "pending",
|
|
"execution_date": "",
|
|
"note": "",
|
|
"updated_at": ""
|
|
})
|
|
item["story"] = p_channels.get("story", {
|
|
"status": "pending",
|
|
"execution_date": "",
|
|
"note": "",
|
|
"updated_at": ""
|
|
})
|
|
item["reels_post"] = p_channels.get("reels_post", {
|
|
"status": "pending",
|
|
"execution_date": "",
|
|
"note": "",
|
|
"updated_at": ""
|
|
})
|
|
result.append(item)
|
|
|
|
return {
|
|
"year": year,
|
|
"month": month,
|
|
"date_range": date_range,
|
|
"count": len(result),
|
|
"packages": result
|
|
}
|
|
|
|
@app.post("/api/channels/save")
|
|
def save_channel_data(payload: ChannelUpdatePayload):
|
|
if payload.channel not in ["telegram", "story", "reels_post"]:
|
|
raise HTTPException(status_code=400, detail="Invalid channel")
|
|
if payload.status not in ["pending", "completed", "cancelled"]:
|
|
raise HTTPException(status_code=400, detail="Invalid status")
|
|
|
|
conn = get_db()
|
|
cursor = conn.cursor()
|
|
cursor.execute("""
|
|
INSERT INTO package_channels (package_id, channel, status, execution_date, note, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
ON CONFLICT(package_id, channel) DO UPDATE SET
|
|
status = excluded.status,
|
|
execution_date = excluded.execution_date,
|
|
note = excluded.note,
|
|
updated_at = CURRENT_TIMESTAMP;
|
|
""", (payload.package_id, payload.channel, payload.status, payload.execution_date, payload.note))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
return {"status": "success", "package_id": payload.package_id, "channel": payload.channel}
|
|
|
|
@app.post("/api/sync-now")
|
|
def sync_now():
|
|
threading.Thread(target=fetch_all_packages_from_metabase, daemon=True).start()
|
|
return {"status": "sync_started"}
|
|
|
|
@app.get("/")
|
|
def get_index():
|
|
index_path = "/workspace/dashboard/index.html"
|
|
if os.path.exists("/app/index.html"):
|
|
index_path = "/app/index.html"
|
|
elif os.path.exists("/data/dashboards/packages/index.html"):
|
|
index_path = "/data/dashboards/packages/index.html"
|
|
with open(index_path, "r", encoding="utf-8") as f:
|
|
return HTMLResponse(content=f.read())
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|