265 lines
9.3 KiB
Python
265 lines
9.3 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""Parastoo Dashboard — Team hours endpoint (Mobina + Ghazal ONLY).
|
||
Reads the Mobina and ghazal-contetnt tabs, computes the 8h-standard comparison."""
|
||
import re
|
||
from collections import defaultdict
|
||
|
||
def _fa(n):
|
||
"""Format a number with Persian digits and the Persian decimal separator (٫ U+066B)."""
|
||
tr = {"0": "۰", "1": "۱", "2": "۲", "3": "۳", "4": "۴", "5": "۵",
|
||
"6": "۶", "7": "۷", "8": "۸", "9": "۹"}
|
||
if n is None:
|
||
return "—"
|
||
if isinstance(n, float):
|
||
s = ("%g" % round(n, 2))
|
||
else:
|
||
s = str(n)
|
||
return "".join(tr.get(ch, "٫" if ch == "." else ch) for ch in s)
|
||
|
||
|
||
def parse_time(v):
|
||
if v is None:
|
||
return 0.0
|
||
s = str(v).strip().replace(",", ".")
|
||
if not s:
|
||
return 0.0
|
||
if ":" in s:
|
||
parts = s.split(":")
|
||
try:
|
||
if len(parts) == 2:
|
||
return float(parts[0]) + float(parts[1]) / 60
|
||
if len(parts) == 3:
|
||
return float(parts[0]) + float(parts[1]) / 60 + float(parts[2]) / 3600
|
||
except Exception:
|
||
return 0.0
|
||
tr = {"۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4", "۵": "5",
|
||
"۶": "6", "۷": "7", "۸": "8", "۹": "9"}
|
||
try:
|
||
return float("".join(tr.get(c, c) for c in s))
|
||
except Exception:
|
||
return 0.0
|
||
|
||
|
||
def parse_date(s):
|
||
m = re.match(r"(\d{1,2})/(\d{1,2})/(\d{4})", str(s))
|
||
if not m:
|
||
return None
|
||
a, b, y = int(m.group(1)), int(m.group(2)), int(m.group(3))
|
||
for mo, da in ((a, b), (b, a)):
|
||
try:
|
||
from datetime import datetime
|
||
if 2000 <= y <= 2100:
|
||
return datetime(y, mo, da)
|
||
except Exception:
|
||
continue
|
||
return None
|
||
|
||
|
||
def _scan(opener, days=120, sheet_id=None):
|
||
"""opener: function(composio slug, args) — injection point for tests."""
|
||
from datetime import datetime, timedelta
|
||
cutoff = datetime.now() - timedelta(days=days)
|
||
|
||
def get_tab(tab):
|
||
out = []
|
||
for start in range(1, 2200, 100):
|
||
d = opener("GOOGLESHEETS_VALUES_GET",
|
||
{"spreadsheet_id": sheet_id, "range": f"'{tab}'!A{start}:R{start + 99}"})
|
||
v = d.get("values", []) if isinstance(d, dict) else []
|
||
if not v:
|
||
break
|
||
out.extend(v)
|
||
if len(v) < 100:
|
||
break
|
||
return out
|
||
|
||
# ---- MOBINA ----
|
||
mobina_daily = defaultdict(float)
|
||
mobina_tasks = []
|
||
for r in get_tab("Mobina"):
|
||
if len(r) < 6:
|
||
continue
|
||
task = str(r[1]).strip()
|
||
if not task or task.startswith("Task"):
|
||
continue
|
||
dt = parse_date(r[3]) if len(r) > 3 else None
|
||
act = parse_time(r[5]) if len(r) > 5 else 0.0
|
||
status = str(r[6]).strip() if len(r) > 6 else ""
|
||
if dt and dt >= cutoff:
|
||
mobina_daily[dt.date().isoformat()] += act
|
||
mobina_tasks.append({"date": dt.date().isoformat(), "task": task[:60],
|
||
"act": _fa(round(act, 2)), "status": status})
|
||
mobina_days = sorted(mobina_daily)
|
||
mobina_sum = sum(mobina_daily.values())
|
||
mobina = {
|
||
"name": "مبینا", "tab": "Mobina",
|
||
"workdays": _fa(len(mobina_days)),
|
||
"sum_hours": _fa(round(mobina_sum, 1)),
|
||
"sum_raw": round(mobina_sum, 1),
|
||
"avg_per_day": _fa(round(mobina_sum / len(mobina_days), 2)) if mobina_days else _fa(0),
|
||
"avg_raw": round(mobina_sum / len(mobina_days), 2) if mobina_days else 0.0,
|
||
"days_ge8": _fa(sum(1 for v in mobina_daily.values() if v >= 8)),
|
||
"days_lt8": _fa(sum(1 for v in mobina_daily.values() if 0 < v < 8)),
|
||
"last7": [{"date": dd, "hours": _fa(round(mobina_daily[dd], 2)),
|
||
"hours_raw": round(mobina_daily[dd], 2)} for dd in mobina_days[-7:]],
|
||
"recent_tasks": sorted(mobina_tasks, key=lambda x: x["date"], reverse=True)[:15],
|
||
}
|
||
|
||
# ---- GHAZAL ----
|
||
ghazal_rows = []
|
||
for r in get_tab("ghazal-contetnt"):
|
||
if len(r) < 10:
|
||
continue
|
||
task = str(r[1]).strip()
|
||
if not task or task.startswith("Task"):
|
||
continue
|
||
cin = str(r[10]).strip() if len(r) > 10 else ""
|
||
cout = str(r[11]).strip() if len(r) > 11 else ""
|
||
wh = parse_time(r[12]) if len(r) > 12 else 0.0
|
||
act = parse_time(r[5]) if len(r) > 5 else 0.0
|
||
status = str(r[6]).strip() if len(r) > 6 else ""
|
||
date_s = str(r[3]).strip() if len(r) > 3 else ""
|
||
if cin or cout or wh or act:
|
||
ghazal_rows.append({"task": task[:60], "cin": cin, "cout": cout,
|
||
"wh": wh, "act": act, "date": date_s, "status": status})
|
||
# workdays = distinct (in,out) pairs; hours from wh col (fallback compute in-out)
|
||
pairs = defaultdict(list)
|
||
for g in ghazal_rows:
|
||
if g["cin"] and g["cout"]:
|
||
pairs[(g["cin"], g["cout"])].append(g)
|
||
wh_by_day = []
|
||
for (cin, cout), recs in pairs.items():
|
||
best = max((r["wh"] for r in recs if r["wh"] > 0), default=0.0)
|
||
if best <= 0:
|
||
def hm(x):
|
||
p = re.match(r"(\d{1,2})(?::(\d{2}))?(?::\d{2})?", x)
|
||
if not p:
|
||
return None
|
||
h = int(p.group(1))
|
||
mm = int(p.group(2)) if p.group(2) else 0
|
||
return (h, mm)
|
||
a, b = hm(cin), hm(cout)
|
||
if a and b:
|
||
best = max((b[0] * 60 + b[1] - a[0] * 60 - a[1]) / 60, 0.0)
|
||
wh_by_day.append({"clock_in": cin, "clock_out": cout, "hours": _fa(round(best, 2)), "hours_raw": round(best, 2),
|
||
"tasks": [g["task"] for g in recs]})
|
||
ghazal_sum = sum(d["hours_raw"] for d in wh_by_day)
|
||
ghazal = {
|
||
"name": "غزل", "tab": "ghazal-contetnt",
|
||
"workdays": _fa(len(wh_by_day)),
|
||
"sum_hours": _fa(round(ghazal_sum, 1)),
|
||
"sum_raw": round(ghazal_sum, 1),
|
||
"avg_per_day": _fa(round(ghazal_sum / len(wh_by_day), 2)) if wh_by_day else _fa(0),
|
||
"avg_raw": round(ghazal_sum / len(wh_by_day), 2) if wh_by_day else 0.0,
|
||
"days_ge8": _fa(sum(1 for d in wh_by_day if d["hours_raw"] >= 8)),
|
||
"days_lt8": _fa(sum(1 for d in wh_by_day if 0 < d["hours_raw"] < 8)),
|
||
"last7": wh_by_day[-7:],
|
||
"recent_tasks": [g for g in ghazal_rows if g["act"] > 0][-15:],
|
||
}
|
||
|
||
return {
|
||
"ok": True,
|
||
"window_days": days,
|
||
"standard": 8,
|
||
"generated": datetime.now().isoformat(),
|
||
"team": [mobina, ghazal],
|
||
}
|
||
|
||
# ---------------------------------------------------------------
|
||
# Caching layer: the full scan (685 Mobina rows + ghazal tab) takes
|
||
# far longer than Cloudflare's 120s origin timeout when cold.
|
||
# Serve the last good snapshot instantly; refresh in a background
|
||
# thread so the first request after a cold start never blocks.
|
||
# ---------------------------------------------------------------
|
||
import json as _json
|
||
import os as _os
|
||
import threading as _threading
|
||
import time as _time
|
||
|
||
_CACHE_DIR = _os.environ.get("TEAM_CACHE_DIR", "/tmp/parastoo_cache")
|
||
_CACHE_FILE = _os.path.join(_CACHE_DIR, "team_hours.json")
|
||
_LOCK = _threading.Lock()
|
||
_TTL = int(_os.environ.get("TEAM_CACHE_TTL", "21600")) # 6 hours
|
||
_refreshing = {"flag": False}
|
||
|
||
|
||
def _read_cache():
|
||
try:
|
||
with open(_CACHE_FILE, encoding="utf-8") as f:
|
||
return _json.load(f)
|
||
except Exception:
|
||
return None
|
||
|
||
|
||
def _write_cache(payload):
|
||
try:
|
||
_os.makedirs(_CACHE_DIR, exist_ok=True)
|
||
tmp = _CACHE_FILE + ".tmp"
|
||
with open(tmp, "w", encoding="utf-8") as f:
|
||
_json.dump(payload, f, ensure_ascii=False)
|
||
_os.replace(tmp, _CACHE_FILE)
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def _refresh(opener, days, sheet_id):
|
||
try:
|
||
payload = _scan(opener, days, sheet_id)
|
||
payload["cached"] = False
|
||
_write_cache(payload)
|
||
except Exception as exc:
|
||
cached = _read_cache()
|
||
if cached:
|
||
cached["stale_error"] = str(exc)[:180]
|
||
cached["cached"] = True
|
||
_write_cache(cached)
|
||
finally:
|
||
with _LOCK:
|
||
_refreshing["flag"] = False
|
||
|
||
|
||
def sheet_team_cached(opener, days=120, sheet_id=None):
|
||
"""Instant response. Serves cache; kicks off a background refresh when stale."""
|
||
cached = _read_cache()
|
||
now = _time.time()
|
||
|
||
fresh_enough = (
|
||
cached
|
||
and cached.get("window_days") == days
|
||
and (now - _parse_ts(cached.get("generated"))) < _TTL
|
||
)
|
||
if fresh_enough:
|
||
cached["cached"] = True
|
||
return cached
|
||
|
||
# trigger a background refresh (never more than one at a time)
|
||
started = False
|
||
with _LOCK:
|
||
if not _refreshing["flag"]:
|
||
_refreshing["flag"] = True
|
||
started = True
|
||
if started:
|
||
t = _threading.Thread(target=_refresh, args=(opener, days, sheet_id), daemon=True)
|
||
t.start()
|
||
|
||
if cached:
|
||
cached["cached"] = True
|
||
cached["refresh_started"] = started
|
||
return cached
|
||
|
||
# truly cold: block this one request (the caller should retry later)
|
||
payload = _scan(opener, days, sheet_id)
|
||
payload["cached"] = False
|
||
_write_cache(payload)
|
||
with _LOCK:
|
||
_refreshing["flag"] = False
|
||
return payload
|
||
|
||
|
||
def _parse_ts(value):
|
||
try:
|
||
from datetime import datetime
|
||
return datetime.fromisoformat(str(value)).timestamp()
|
||
except Exception:
|
||
return 0.0
|