import sqlite3, datetime, os 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) today = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d") print("=== today punches by hour (device-local label) ===") for r in con.execute( "SELECT substr(punch_time,12,2) h, COUNT(*), SUM(source='Realtime') " "FROM punches WHERE punch_time LIKE ? GROUP BY h ORDER BY h", (today + "%",)): bar = "#" * min(60, r[1]) print(f" {r[0]}:00 total={r[1]:<4} realtime={r[2]:<4} {bar}") print("\n=== today per-device ===") for r in con.execute( "SELECT device_id, COUNT(*), SUM(source='Realtime'), SUM(source='Reconciliation') " "FROM punches WHERE punch_time LIKE ? GROUP BY device_id ORDER BY device_id", (today + "%",)): print(f" {r[0]:<16} total={r[1]:<4} realtime={r[2]:<4} recon={r[3]}") print("\n=== punches in last 60 min (live flow right now) ===") cut = (datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(minutes=60)) cut_s = cut.strftime("%Y-%m-%dT%H:%M:%S.0000000Z") for r in con.execute( "SELECT device_id, COUNT(*), SUM(source='Realtime') FROM punches " "WHERE first_seen_at >= ? GROUP BY device_id", (cut_s,)): print(f" {r[0]:<16} n={r[1]:<4} realtime={r[2]}") print("\n=== last 8 punches (arrival detail) ===") for r in con.execute( "SELECT personnel_code, device_id, punch_time, first_seen_at, source, upload_state " "FROM punches ORDER BY first_seen_at DESC LIMIT 8"): print(f" pin={r[0]:<8} dev={r[1]:<16} punch={r[2][:19]} ingest={r[3][:19]} " f"{r[4]:<14} {r[5]}") print("\n=== ledger health ===") for r in con.execute("SELECT upload_state, COUNT(*) FROM punches GROUP BY upload_state"): print(f" {r[0]}: {r[1]}") print(f" db size: {os.path.getsize(db)/1024/1024:.1f} MB") # read cadence from log log = r"G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\logs\attendancehub_sync.log" print("\n=== READ plans (last 60 attempts) ===") plans = {} times = [] with open(log, "r", encoding="utf-8", errors="replace") as f: lines = f.readlines()[-6000:] for ln in lines: if "READ_ATTEMPT" in ln and "plan=" in ln: try: t = ln[:8] p = ln.split("plan=")[1].split("(")[0].split(" ")[0] plans[p] = plans.get(p, 0) + 1 times.append(t) except Exception: pass for k, v in sorted(plans.items()): print(f" plan={k}: {v}") if len(times) >= 2: print(f" window: {times[0]} .. {times[-1]}") con.close()