import sqlite3, datetime, collections, os DB = r"G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\attendancehub_v2.db" NOW = datetime.datetime.now(datetime.timezone.utc) today = NOW.strftime("%Y-%m-%d") END = today + "T23:59:59.9999999Z" def parse(s): if not s: return None s = s.rstrip("Z") w, _, fr = s.partition(".") fr = (fr + "0000000")[:7] return (datetime.datetime.fromisoformat(w).replace(tzinfo=datetime.timezone.utc) + datetime.timedelta(microseconds=int(fr) / 10)) con = sqlite3.connect(f"file:{DB}?mode=ro", uri=True, timeout=20) SKEW = datetime.timedelta(seconds=12516) # device label -> true instant # ---------------- live leg ---------------- rows = con.execute( "SELECT personnel_code, device_id, punch_time, first_seen_at FROM punches " "WHERE source='Realtime' AND punch_time >= ? AND punch_time <= ?", (today, END)).fetchall() lat = [] for pc, dev, pt, fs in rows: p, f = parse(pt), parse(fs) if p and f: lat.append(((f - (p - SKEW)).total_seconds(), dev)) lat.sort() vals = [x for x, _ in lat] n = len(vals) print(f"=== LIVE LEG (device event -> SQLite row) n={n} ===") q = lambda x: vals[min(n - 1, int(round(x * (n - 1))))] print(f" min={vals[0]:.0f}s p01={q(.01):.0f}s p05={q(.05):.0f}s p25={q(.25):.0f}s " f"p50={q(.5):.0f}s p75={q(.75):.0f}s p95={q(.95):.0f}s max={vals[-1]:.0f}s") h = collections.Counter(int(v) for v in vals if v < 46) print("\n histogram 8..30s:") for s in range(8, 31): c = h.get(s, 0) print(f" {s:>2}s : {c:>5} {'#' * min(70, c // 2)}") print("\n per-device floor:") per = collections.defaultdict(list) for v, d in lat: per[d].append(v) for d in sorted(per): v = sorted(per[d]) print(f" {d:<16} n={len(v):<5} min={v[0]:>4.0f}s p05={v[int(.05*len(v))]:>4.0f}s p50={v[len(v)//2]:>4.0f}s") # ---------------- upload leg ---------------- d = [] for fs, ak in con.execute( "SELECT first_seen_at, acked_at FROM punches " "WHERE acked_at IS NOT NULL AND punch_time >= ? AND punch_time <= ?", (today, END)).fetchall(): f, a = parse(fs), parse(ak) if f and a: d.append((a - f).total_seconds()) d.sort() if d: m = len(d) p = lambda x: d[min(m - 1, int(round(x * (m - 1))))] print(f"\n=== UPLOAD LEG (SQLite row -> server ack) n={m} ===") print(f" min={d[0]:.0f}s p05={p(.05):.0f}s p50={p(.5):.0f}s p95={p(.95):.0f}s max={d[-1]:.0f}s") for thr in (5, 10, 20, 30, 60, 120): k = sum(1 for x in d if x <= thr) print(f" <= {thr:>3}s : {k:>5}/{m} ({100*k/m:.1f}%)") # ---------------- end to end ---------------- e2e = [] for pt, fs, ak in con.execute( "SELECT punch_time, first_seen_at, acked_at FROM punches " "WHERE acked_at IS NOT NULL AND source='Realtime' AND punch_time >= ? AND punch_time <= ?", (today, END)).fetchall(): p, f, a = parse(pt), parse(fs), parse(ak) if p and f and a: e2e.append((a - (p - SKEW)).total_seconds()) e2e.sort() if e2e: m = len(e2e) p = lambda x: e2e[min(m - 1, int(round(x * (m - 1))))] print(f"\n=== END-TO-END (device event -> server ack, Realtime only) n={m} ===") print(f" min={e2e[0]:.0f}s p05={p(.05):.0f}s p50={p(.5):.0f}s p95={p(.95):.0f}s max={e2e[-1]:.0f}s") for thr in (30, 60, 120, 300, 600): k = sum(1 for x in e2e if x <= thr) print(f" <= {thr:>3}s : {k:>5}/{m} ({100*k/m:.1f}%)") # ---------------- hygiene ---------------- print("\n=== LEDGER HYGIENE ===") for r in con.execute("SELECT upload_state, COUNT(*) FROM punches GROUP BY upload_state"): print(f" state={r[0]:<12} {r[1]:>7}") print(" future-dated punch_time (garbage the service accepted):") for r in con.execute( "SELECT COUNT(*), MIN(punch_time), MAX(punch_time) FROM punches WHERE punch_time > ?", (NOW.isoformat().replace("+00:00", "Z"),)).fetchall(): print(f" n={r[0]} {r[1]} .. {r[2]}") print(" Pending rows older than 1 day (will never resolve):") for r in con.execute( "SELECT COUNT(*) FROM punches WHERE upload_state='Pending' AND first_seen_at < ?", (NOW.isoformat().replace("+00:00", "Z"),)).fetchall(): print(f" n={r[0]}") print(" rows with acked_at set but still Pending:", con.execute("SELECT COUNT(*) FROM punches WHERE upload_state='Pending' AND acked_at IS NOT NULL").fetchone()[0]) print(f"\n db size = {os.path.getsize(DB)/1024/1024:.1f} MB") con.close()