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" return (datetime.datetime.fromisoformat(whole).replace(tzinfo=datetime.timezone.utc) + datetime.timedelta(microseconds=int(frac) / 10)) def pct(v, p): v = sorted(v); return v[min(len(v)-1, int(round(p/100*(len(v)-1))))] def show(label, v, unit="s"): 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}{unit}") # window: after pump-fix deploy 11:56 local = 08:26 UTC CUT = datetime.datetime(2026,10,10,8,26,0, tzinfo=datetime.timezone.utc) cut_s = CUT.strftime("%Y-%m-%dT%H:%M:%S.0000000Z") print("=== 1) UPLOAD leg (host clock both sides => CLOCK-CLEAN) ===") rows = con.execute( "SELECT punch_time, first_seen_at, acked_at, source, upload_state FROM punches " "WHERE first_seen_at >= ?", (cut_s,)).fetchall() up = [] for pt, fs, ack, src, state in rows: if state == "Sent" and ack: a, f = parse(ack), parse(fs) if a and f: up.append((a - f).total_seconds()) show("first_seen -> acked (post-fix rows)", up) print(f" (rows in window: {len(rows)})") print("\n=== 2) DEVICE -> INGEST (LIVE path), skew-corrected (S=84s) ===") S = 84; SITE = 12600 adj = [] for pt, fs in con.execute("SELECT punch_time, first_seen_at FROM punches WHERE source='Realtime'"): p, f = parse(pt), parse(fs) if p and f: adj.append((f - p).total_seconds() + SITE - S) show("realtime ingest latency", adj) print("\n=== 3) naive raw end-to-end within SQLite (punch_time -> acked), skew NOT removed ===") raw = [] for pt, fs, ack, src, state in rows: if state == "Sent" and ack: p, a = parse(pt), parse(ack) if p and a: raw.append((a - p).total_seconds()) show("punch_time -> acked (raw)", raw) print(f" => removing S={S}s skew gives REAL end-to-end:") real = [x - S for x in raw] show("real end-to-end (raw - S)", real) print("\n=== 4) today's source mix (how punches actually arrive) ===") today = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d") for r in con.execute("SELECT source, COUNT(*) FROM punches WHERE punch_time LIKE ? GROUP BY source", (today+"%",)): print(f" {r[0]}: {r[1]}") print("\n=== 5) ledger health ===") for r in con.execute("SELECT upload_state, COUNT(*) FROM punches GROUP BY upload_state"): print(f" {r[0]}: {r[1]}") con.close()