import re, datetime, collections, statistics as st, json, os LOG = r"G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\logs\attendancehub_sync.log" CFG = r"G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\config\phase1.device.json" NOW = datetime.datetime.now() # ---------- config: read cadence knobs ---------- print("=== config: read cadence knobs ===") with open(CFG, "r", encoding="utf-8") as f: txt = f.read() for key in ["fullEveryMin", "intervalMs", "readInterval", "reconcileEvery", "window", "enabled"]: for m in re.finditer(rf'"{key}"\s*:\s*([^\n,}}]+)', txt): print(f" {key} = {m.group(1).strip()}") # ---------- parse reads: distinguish FULL vs WINDOW ---------- patR = re.compile(r"^(\d{2}:\d{2}:\d{2})\.\d+ .*COM_OP device=([^:]+):read api=ReadAttendance " r".*?durationMs=(\d+) ok=(\S+)") patA = re.compile(r"^(\d{2}:\d{2}:\d{2}).*READ_ATTEMPT device=(\S+).*plan=(\S+)") plan_of = {} # we approximate by time: FULL reads are the long ones dur = collections.defaultdict(list) # device -> [ms] fh = collections.defaultdict(list) with open(LOG, "r", encoding="utf-8", errors="replace") as f: for ln in f: if "api=ReadAttendance" in ln: m = patR.match(ln) if m: dur[m.group(2)].append(int(m.group(3))) elif "api=FunctionalHealth" in ln: m = re.search(r"device=([^:]+):read api=FunctionalHealth .*durationMs=(\d+)", ln) if m: fh[m.group(1)].append(int(m.group(2))) def pctl(v, p): v = sorted(v) return v[min(len(v)-1, int(round(p/100*(len(v)-1))))] print("\n=== ReadAttendance cost per device (how long the device stays busy) ===") print(f"{'device':<16}{'n':>6}{'p50':>9}{'p95':>10}{'max(s)':>9}{'reads>20s':>10}{'busyMin':>9}") summary = {} for d in sorted(dur): v = dur[d] big = [x for x in v if x >= 20000] busy = sum(big)/1000/60 summary[d] = dict(n=len(v), p50=st.median(v), p95=pctl(v,95), max_s=max(v)/1000, n_long=len(big), busy_min=round(busy,1)) print(f"{d:<16}{len(v):>6}{st.median(v):>9.0f}{pctl(v,95):>10}{max(v)/1000:>9.1f}" f"{len(big):>10}{busy:>9.1f}") print("\n=== FunctionalHealth (runs only after a session recreate) ===") for d in sorted(fh): print(f" {d:<16} n={len(fh[d])} durations(s) = {[round(x/1000,1) for x in sorted(fh[d])]}") # ---------- FRESH status ---------- print(f"\n=== FRESH STATUS @ {NOW.strftime('%H:%M:%S')} ===") latest = {} recent_err = [] recent_recreate = [] plans = collections.defaultdict(list) with open(LOG, "r", encoding="utf-8", errors="replace") as f: for ln in f: if "DUAL_HEALTH device=" in ln: m = re.search(r"device=(\S+)", ln) if m: latest[m.group(1)] = ln.rstrip() if "SESSION_READ_RECREATE" in ln or "SESSION_LIVE_RECREATE" in ln: recent_recreate.append(ln.strip()[:150]) if re.search(r"warn:|error:|Exception|FAILED|_FAIL\b", ln): recent_err.append(ln.strip()[:170]) if "READ_ATTEMPT" in ln: m = patA.search(ln) if m: plans[m.group(1)].append((ln[:8], m.group(3))) g = lambda ln, p: (re.search(p, ln).group(1) if re.search(p, ln) else "-") print(f"{'device':<16}{'time':<9}{'live':>12}{'read':>12}{'evDirect':>10}{'evDrop':>8}") for d in sorted(latest): ln = latest[d] print(f"{d:<16}{ln[:8]:<9}{g(ln,'liveC=(\\S+)')+'/'+g(ln,'livePump=(\\S+)'):>12}" f"{g(ln,'readC=(\\S+)')+'/'+g(ln,'readPump=(\\S+)'):>12}" f"{g(ln,'evDirect=(\\S+)'):>10}{g(ln,'evDropped=(\\S+)'):>8}") print(f"\nlast 4 session-recreate events (total in log: {len(recent_recreate)}):") for r in recent_recreate[-4:]: print(" " + r) print(f"\nwarn/error/exception lines in log: {len(recent_err)}") bykind = collections.Counter() for e in recent_err: k = re.search(r"(SESSION_\w+|READ_\w+|UPLOAD_\w+|RECON_\w+|WATERMARK_\w+|SQLITE_\w+|LIVE_\w+)", e) bykind[k.group(1) if k else e.split()[1] if len(e.split())>1 else "other"] += 1 for k, v in bykind.most_common(12): print(f" {k:<34} {v}") print("\nread plan mix in effect (last 12 attempts per device):") for d in sorted(plans): last = plans[d][-12:] c = collections.Counter(p for _, p in last) print(f" {d:<16} {dict(c)} last={last[-1][0]}")