import json,os,glob,collections,datetime,bisect
ROOT=os.path.expanduser("~/.claude/projects"); UTC=datetime.timezone.utc
best={}; rows=0; bad=0
for fp in glob.glob(os.path.join(ROOT,"**","*.jsonl"),recursive=True):
    try: fh=open(fp,errors="replace")
    except OSError: continue
    with fh:
        for line in fh:
            if '"usage"' not in line: continue
            try: d=json.loads(line)
            except: continue
            if d.get("type")!="assistant": continue
            m=d.get("message") or {}; u=m.get("usage")
            if not isinstance(u,dict): continue
            mid=m.get("id") or d.get("requestId")
            if not mid: continue
            model=m.get("model") or "unknown"
            if model=="<synthetic>": continue
            try: dt=datetime.datetime.fromisoformat((d.get("timestamp") or "").replace("Z","+00:00"))
            except: continue
            rows+=1
            cc=u.get("cache_creation") or {}
            w5=cc.get("ephemeral_5m_input_tokens") or 0; w1=cc.get("ephemeral_1h_input_tokens") or 0
            tw=u.get("cache_creation_input_tokens",0) or 0
            if w5+w1==0 and tw: w5=tw
            r=dict(dt=dt,model=model,inp=u.get("input_tokens",0) or 0,w5=w5,w1=w1,
                   rd=u.get("cache_read_input_tokens",0) or 0,out=u.get("output_tokens",0) or 0,
                   side=bool(d.get("isSidechain")))
            p=best.get(mid)
            # one record per API response; output_tokens = final (max) value across streamed partials
            if p is None: best[mid]=r
            else:
                if (p["inp"],p["w5"],p["w1"],p["rd"])!=(r["inp"],r["w5"],r["w1"],r["rd"]): bad+=1
                if r["out"]>p["out"]: r["dt"]=min(r["dt"],p["dt"]); best[mid]=r
recs=sorted(best.values(),key=lambda r:r["dt"])
print(f"raw rows {rows:,} -> unique API responses {len(recs):,} (collapse {rows/max(len(recs),1):.2f}x); in/cache mismatches {bad}")
print(f"span {recs[0]['dt']} -> {recs[-1]['dt']}")
print("\nmodels in last 14 days:")
cut=recs[-1]["dt"]-datetime.timedelta(days=14)
c=collections.Counter(r["model"] for r in recs if r["dt"]>=cut)
for k,v in c.most_common(): print(f"   {k:32s} {v:,}")
json.dump([{**r,'dt':r['dt'].isoformat()} for r in recs],open("recs.json","w"))
