import re, datetime, collections, statistics as st LOG = r"G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\logs\attendancehub_sync.log" print(f"now = {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") api_dur = collections.defaultdict(list) # api -> [ms] api_cnt = collections.Counter() api_fail = collections.Counter() dev_api = collections.defaultdict(collections.Counter) read_plan = collections.defaultdict(list) # device -> [HH:MM:SS] recreate = [] # (time, device, reason) disconnects = collections.Counter() errors = [] liveness = collections.Counter() COM_OP_TOTAL = 0 pat_op = re.compile( r"^(\d{2}:\d{2}:\d{2})\.\d+ .*COM_OP device=([^:]+):(\S+) api=(\S+) .*durationMs=(\d+) ok=(\S+) nativeLastError=(-?\d+)") pat_att = re.compile(r"READ_ATTEMPT device=(\S+).*plan=(\S+)") pat_rec = re.compile(r"^(\d{2}:\d{2}:\d{2}).*SESSION_(?:READ|LIVE)_RECREATE device=(\S+) reason=(\S+)") pat_disc = re.compile(r"DISCONNECT|SESSION_CLOSE|CONNECT_FAIL|session lost|reconnect", re.I) with open(LOG, "r", encoding="utf-8", errors="replace") as f: for ln in f: if "COM_OP" in ln: m = pat_op.match(ln) if m: COM_OP_TOTAL += 1 t, dev, kind, api, ms, ok, nle = m.groups() ms = int(ms) api_dur[api].append(ms) api_cnt[api] += 1 dev_api[dev][api] += 1 if ok != "true": api_fail[api] += 1 if len(errors) < 25: errors.append(ln.strip()[:200]) continue if "READ_ATTEMPT" in ln: m = pat_att.search(ln) if m: read_plan[m.group(1)].append((ln[:8], m.group(2))) continue if "SESSION_READ_RECREATE" in ln or "SESSION_LIVE_RECREATE" in ln: m = pat_rec.match(ln) if m: recreate.append(m.groups()) continue if "LivenessPing" in ln and "ok=false" in ln: liveness["fail"] += 1 elif "LivenessPing" in ln: liveness["ok"] += 1 if pat_disc.search(ln) and len(errors) < 40 and ("error" in ln.lower() or "fail" in ln.lower()): errors.append(ln.strip()[:200]) def pctl(v, p): v = sorted(v) if not v: return 0 return v[min(len(v) - 1, int(round(p / 100 * (len(v) - 1))))] print(f"\n=== 1) SDK API INVENTORY — total COM_OP = {COM_OP_TOTAL} ===") print(f"{'api':<24}{'calls':>8}{'min':>7}{'p50':>7}{'p95':>7}{'p99':>7}{'max':>9}{'fails':>7}") for api, d in sorted(api_dur.items(), key=lambda x: -len(x[1])): print(f"{api:<24}{len(d):>8}{min(d):>7}{st.median(d):>7.0f}{pctl(d,95):>7}{pctl(d,99):>7}{max(d):>9}{api_fail[api]:>7}") print("\n=== 2) per-device API call counts ===") for dev in sorted(dev_api): tot = sum(dev_api[dev].values()) items = ", ".join(f"{k}={v}" for k, v in dev_api[dev].most_common()) print(f" {dev:<16} total={tot:<6} {items}") print("\n=== 3) READ cadence + plan mix (whole log) ===") for dev in sorted(read_plan): ts = [x[0] for x in read_plan[dev]] plans = collections.Counter(x[1] for x in read_plan[dev]) gaps = [] for a, b in zip(ts, ts[1:]): try: A = datetime.datetime.strptime(a, "%H:%M:%S") B = datetime.datetime.strptime(b, "%H:%M:%S") g = (B - A).total_seconds() if 0 <= g < 3600: gaps.append(g) except Exception: pass gtxt = (f"gap p50={st.median(gaps):.0f}s min={min(gaps):.0f}s max={max(gaps):.0f}s" if gaps else "n/a") print(f" {dev:<16} reads={len(ts):<6} plans={dict(plans)} {gtxt}") print("\n=== 4) SESSION RECREATES (churn) ===") if recreate: bydev = collections.Counter(r[1] for r in recreate) byreason = collections.Counter(r[2] for r in recreate) print(f" total={len(recreate)} byDevice={dict(bydev)} byReason={dict(byreason)}") print(f" last 5: {recreate[-5:]}") else: print(" NONE (zero session churn)") print("\n=== 5) LivenessPing ===") print(f" {dict(liveness)}") print(f"\n=== 6) errors/fails captured: {len(errors)} ===") for e in errors[:12]: print(" " + e)