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 n={len(v)} min={min(v):.1f}{unit} 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}") SITE = 12600 # UTC+3:30 in seconds DEV_SKEW = 84 # device clock measured ~84s behind host (DeviceClock.cs) # ---- A) LIVE path: ALL realtime punches, device->ingest, clock-adjusted ---- print("=== A) LIVE path: device punch -> service ingest (ALL Realtime rows) ===") raw, 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: d = (f - p).total_seconds() raw.append(d); adj.append(d + SITE - DEV_SKEW) show(" raw delta (first_seen - punch_time)", raw) show(" ADJUSTED latency (device->ingest)", adj) # ---- B) upload leg: ingest -> MySQL ack, for REAL recent punches ---- print("\n=== B) upload leg: service ingest -> MySQL ack (real today punches) ===") today = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d") rows = con.execute( "SELECT punch_time, first_seen_at, acked_at, source FROM punches " "WHERE punch_time LIKE ? AND upload_state='Sent' AND acked_at IS NOT NULL", (today + "%",)).fetchall() up = [] for pt, fs, ack, src in rows: a, f = parse(ack), parse(fs) if a and f: up.append((a - f).total_seconds()) show(" first_seen -> acked", up) by = {} for pt, fs, ack, src in rows: a, f = parse(ack), parse(fs) if a and f: by.setdefault(src, []).append((a - f).total_seconds()) for k, v in sorted(by.items()): show(f" {k}", v) # ---- C) ingest timeliness for today: punch_time -> first_seen (clock-adjusted) ---- print("\n=== C) today: device punch -> ingest, clock-adjusted ===") c_adj = [] for pt, fs, ack, src in rows: p, f = parse(pt), parse(fs) if p and f: c_adj.append((f - p).total_seconds() + SITE - DEV_SKEW) show(" adjusted device->ingest (today, all sources)", c_adj) # ---- D) source mix today ---- print("\n=== D) today's punch source mix ===") for r in con.execute("SELECT source, COUNT(*) FROM punches WHERE punch_time LIKE ? GROUP BY source", (today + "%",)): print(f" {r[0]}: {r[1]}") # ---- E) realtime count vs total (share of LIVE) ---- print("\n=== E) LIVE share (whole ledger) ===") tot, rt = con.execute("SELECT COUNT(*), SUM(source='Realtime') FROM punches").fetchone() print(f" total={tot} realtime={rt} ({100.0*rt/tot:.1f}%)") con.close()