#!/usr/bin/env python3
"""Real-server watch measurement. No timers or app sources are modified.
Run: python3 measure.py --base-home BASE/.t3 --head-home HEAD/.t3 --project-id ID --sha MR_HEAD --out NEW_DIRECTORY
Start both watches immediately beforehand through the app command boundary.
Only sanitized watcher span names, durations, counts and synthetic event timestamps are exported.
"""
import argparse,collections,datetime,json,pathlib,statistics,subprocess,time
p=argparse.ArgumentParser()
for k in ['base-home','head-home','project-id','sha','out']:p.add_argument('--'+k,required=True)
a=p.parse_args(); out=pathlib.Path(a.out);out.mkdir(parents=True,exist_ok=False)
start=time.time(); events=[]
def stamp():return datetime.datetime.now(datetime.timezone.utc).isoformat()
def event(name,path,*args):
 t=stamp();r=subprocess.run(['glab','api',path,*args],capture_output=True,text=True)
 events.append(dict(event=name,at=t,success=r.returncode==0));(out/'events.json').write_text(json.dumps(events,indent=2))
 if r.returncode:print(name,'FAILED',flush=True)
def read(home):
 spans={}
 for f in (pathlib.Path(home)/'userdata/logs').glob('server.trace.ndjson*'):
  for l in f.open():
   try:x=json.loads(l)
   except:continue
   if 'spanId' in x:spans[x['spanId']]=x
 sweeps={x['spanId']:dict(at=int(x['startTimeUnixNano'])/1e9,durationMs=x['durationMs'],calls=[]) for x in spans.values() if x.get('name')=='PullRequestWatchReactor.sweep' and int(x['startTimeUnixNano'])/1e9>=start-30}
 sync=0
 for x in spans.values():
  if x.get('name')!='VcsProcess.run':continue
  y=x;names=[];seen=set();sweep=None
  while y and y['spanId'] not in seen:
   seen.add(y['spanId']);names.append(y.get('name',''))
   if y['spanId'] in sweeps:sweep=y['spanId']
   y=spans.get(y.get('parentSpanId'))
  if sweep:sweeps[sweep]['calls'].append(dict(operation=next((n for n in names if n.startswith('GitLabPullRequestCli.')),'other glab read'),durationMs=x['durationMs'],success=x.get('exit',{}).get('_tag')=='Success'))
  if any(n.startswith('PullRequestSyncReactor.') for n in names) and int(x['startTimeUnixNano'])/1e9>=start:sync+=1
 rows=[]
 for s in sorted(sweeps.values(),key=lambda x:x['at']):
  rows.append(dict(elapsedMinutes=round((s['at']-start)/60,3),utc=datetime.datetime.fromtimestamp(s['at'],datetime.timezone.utc).isoformat(),durationMs=round(s['durationMs'],3),glabCalls=len(s['calls']),operations=s['calls']))
 pending=[s for s in rows if 1<s['elapsedMinutes']<19.8 and s['glabCalls']]
 return dict(sweeps=rows,linkSyncCalls=sync,pending=dict(samples=len(pending),calls=[s['glabCalls'] for s in pending],medianCalls=statistics.median([s['glabCalls'] for s in pending]) if pending else None,spreadCalls=max([s['glabCalls'] for s in pending])-min([s['glabCalls'] for s in pending]) if pending else None,medianDurationMs=statistics.median([s['durationMs'] for s in pending]) if pending else None))
done=set()
while True:
 elapsed=time.time()-start
 for sec,name,path,args in [(1200,'CI success',f'projects/{a.project_id}/statuses/{a.sha}',['-X','POST','-f','state=success','-f','name=ext-ci']),(1380,'Viewer comment',f'projects/{a.project_id}/merge_requests/1/notes',['-X','POST','-f','body=Synthetic watch measurement comment']),(2220,'Outside push',f'projects/{a.project_id}/repository/files/outside.txt',['-X','POST','-f','branch=feature','-f','content=Outside change','-f','commit_message=Outside change'])]:
  if elapsed>=sec and name not in done:event(name,path,*args);done.add(name)
 result=dict(startedAt=datetime.datetime.fromtimestamp(start,datetime.timezone.utc).isoformat(),elapsedMinutes=round(elapsed/60,2),environment='Linux, same machine, two real development servers, fresh databases, GitLab.com private MR, Replay multi-turn; unmodified two-minute watch timers',base=read(a.base_home),head=read(a.head_home),events=events)
 (out/'measurement.json').write_text(json.dumps(result,indent=2))
 print(json.dumps({k:result[k]['pending'] for k in ['base','head']}),flush=True)
 if elapsed>=2820:break
 time.sleep(30)
