facetted-interfaces/history/FIAM_v0.2_Draft6/tools/operation_coverage_lens.py
tegwick 1353267569 Establish FIAM repository governance baseline
Assistant: codex
Assistant-Model: gpt-5.6-luna
Assistant-Session: 01a087b0-7b1e-7531-8ef3-6525ebaf7ee7
2026-09-09 21:53:43 +02:00

32 lines
2.5 KiB
Python
Executable file

#!/usr/bin/env python3
"""Declared operation coverage/dormancy from runtime aggregate last-seen evidence."""
from __future__ import annotations
import argparse,json,sqlite3
from datetime import datetime,timedelta
from pathlib import Path
import yaml
def dt(v):return datetime.fromisoformat(str(v).replace('Z','+00:00'))
def main():
ap=argparse.ArgumentParser();ap.add_argument('db',type=Path);ap.add_argument('--population',required=True);ap.add_argument('--at',required=True);ap.add_argument('--window-days',type=int,default=90);ap.add_argument('-o','--output',type=Path,required=True);a=ap.parse_args();c=sqlite3.connect(a.db);c.row_factory=sqlite3.Row;since=(dt(a.at)-timedelta(days=a.window_days)).isoformat().replace('+00:00','Z')
# latest declared snapshot per source, then operations declared by those exchanges
xs=c.execute("SELECT source_id,exchange_id,snapshot_time FROM exchange_documents WHERE mode='declared-snapshot' AND snapshot_time<=? AND (population_id IS NULL OR population_id=?) ORDER BY source_id,snapshot_time DESC",(a.at,a.population)).fetchall();latest={}
for x in xs:
if x['source_id'] not in latest:latest[x['source_id']]=x['exchange_id']
ops=[]
for exid in latest.values():
for r in c.execute("SELECT de.entity_id,e.name FROM declared_entities de JOIN entity_versions e ON e.entity_id=de.entity_id AND e.exchange_id=de.exchange_id WHERE de.exchange_id=? AND e.kind='operation'",(exid,)).fetchall():ops.append((r['entity_id'],r['name']))
seen={}
for r in c.execute('SELECT operation_ref,MAX(last_seen) ls,SUM(count) cnt,SUM(error_count) err FROM runtime_aggregates WHERE population_id=? AND last_seen<=? GROUP BY operation_ref',(a.population,a.at)).fetchall():seen[r['operation_ref']]={'last_seen':r['ls'],'count':int(r['cnt'] or 0),'error_count':int(r['err'] or 0)}
rows=[]
for oid,name in sorted(set(ops)):
s=seen.get(oid)
if not s:cls='never-observed'
elif s['last_seen']<since:cls='dormant'
else:cls='observed-in-window'
rows.append({'operation_ref':oid,'name':name,'classification':cls,**(s or {})})
summary={}
for r in rows:summary[r['classification']]=summary.get(r['classification'],0)+1
out={'lens_id':'fiam.lens.operation-coverage','population_id':a.population,'window':{'since':since,'until':a.at,'days':a.window_days},'summary':dict(sorted(summary.items())),'operations':rows};a.output.write_text(yaml.safe_dump(out,sort_keys=False))
if __name__=='__main__':main()