import sqlite3, datetime, statistics as st db = r"G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\attendancehub_v2.db" con = sqlite3.connect(f"file:{db}?mode=ro", uri=True, timeout=15) def parse(s): if s is None: return None s = s.rstrip("Z") if "." in s: whole, frac = s.split("."); frac = (frac + "0000000")[:7] else: whole, frac = s, "0000000" dt = datetime.datetime.fromisoformat(whole).replace(tzinfo=datetime.timezone.utc) return dt + datetime.timedelta(microseconds=int(frac) / 10) # Deploy 2026-10-10 11:21:41 +03:30 == 07:51:41 UTC DEPLOY = datetime.datetime(2026, 10, 10, 7, 51, 41, tzinfo=datetime.timezone.utc) deploy_str = DEPLOY.strftime("%Y-%m-%dT%H:%M:%S.0000000Z") def pct(v, p): if not v: return None v = sorted(v); return v[min(len(v)-1, int(round(p/100*(len(v)-1))))] def show(label, v): if not v: print(f"{label}: (empty)"); return print(f"{label}: n={len(v)} min={min(v):.1f} p05={pct(v,5):.1f} median={st.median(v):.1f} " f"mean={st.mean(v):.1f} p95={pct(v,95):.1f} max={max(v):.1f}") # ---- 1) UPLOAD leg on POST-DEPLOY rows only (no churn backlog) ---- rows = con.execute( "SELECT punch_time, first_seen_at, acked_at, source, upload_state FROM punches " "WHERE first_seen_at >= ?", (deploy_str,)).fetchall() print(f"=== rows ingested AFTER deploy (first_seen_at >= 07:51:41Z): {len(rows)} ===") print(" by state:", dict(con.execute("SELECT upload_state, COUNT(*) FROM punches WHERE first_seen_at >= ? GROUP BY upload_state", (deploy_str,)).fetchall())) print(" by source:", dict(con.execute("SELECT source, COUNT(*) FROM punches WHERE first_seen_at >= ? GROUP BY source", (deploy_str,)).fetchall())) up_all, up_rt, up_rc = [], [], [] pend_noack = 0 for pt, fs, ack, src, state in rows: if state == "Sent" and ack: a, f = parse(ack), parse(fs) if a and f: d = (a - f).total_seconds() up_all.append(d) (up_rt if src == "Realtime" else up_rc).append(d) else: pend_noack += 1 print(f" not-yet-acked: {pend_noack}") show(" upload leg ALL (first_seen->acked, sec)", up_all) show(" upload leg Realtime ", up_rt) show(" upload leg Reconcile ", up_rc) # ---- 2) REALTIME capture->ingest (device->host), with device clock caveat ---- print("\n=== REALTIME: first_seen_at - punch_time (sec) — device clock is local(+3:30) ===") rt = [] for pt, fs, ack, src, state in rows: if src == "Realtime": p, f = parse(pt), parse(fs) if p and f: rt.append((f - p).total_seconds()) show(" realtime delta", rt) if rt: off = 12600 # +3:30 in seconds print(f" if device clock == exactly +3:30: implied delay = delta + {off}") adj = [d + off for d in rt] show(" implied realtime latency (sec)", adj) # ---- 3) whole DB: how many total realtime punches exist ---- print("\n=== realtime totals (whole ledger) ===") for r in con.execute("SELECT source, COUNT(*) FROM punches GROUP BY source"): print(" ", r[0], r[1]) # ---- 4) today's punch flow by hour (device local, punch_time) ---- print("\n=== today's punches (punch_time local) by hour ===") for r in con.execute("SELECT substr(punch_time,12,2) h, COUNT(*) FROM punches WHERE punch_time LIKE '2026-10-10%' GROUP BY h ORDER BY h"): print(f" {r[0]}:00 {r[1]}") con.close()