# A3-COPY helper: symbols that already had a token cross $1M. Usage: python3 -I famflag.py out.json
# A new band pool is flagged if upper(strip(symbol)) is in the output with a crossing time < entry time and token != itself.
import json,sqlite3,sys
names={}
c=sqlite3.connect('file:/opt/sw/dataeng/names.db?mode=ro',uri=True)
for tk,sy in c.execute('select token,symbol from names where symbol is not null'):names[tk.lower()]=sy.upper().strip()
fam={}
for f in ('/opt/sw/radar/radar_hist.jsonl','/opt/sw/radar/radar.jsonl'):
    for l in open(f):
        if not l.startswith('{'):continue
        r=json.loads(l)
        if r.get('ev')!='cross' or r.get('th')!=1000000:continue
        tk=r['token'].lower();s=names.get(tk)
        if not s:continue
        fam.setdefault(s,{});fam[s][tk]=min(fam[s].get(tk,9e18),r['t'])
json.dump(fam,open(sys.argv[1],'w'))
print(len(fam),'symbols with a 1M token')
