88 lines
5.4 KiB
Python
88 lines
5.4 KiB
Python
# Compares a 1.0 store (read over /dhd/1/read) with the 0.19 export.
|
|||
|
|
# Prints counts and field NAMES only: never a name, an address or a text.
|
||
|
|
#
|
||
|
|
# compare.py <site> <token file> <records-1.0.yaml> <places.yaml> [--files] [bucket ...]
|
||
|
|
#
|
||
|
|
# The token must be named `read` in the site's DHD_TOKENS. With buckets
|
||
|
|
# named, only those are held to the store; --files also fetches every
|
||
|
|
# file a record names, as a visitor would.
|
||
|
|
import json, sys, re, hashlib, urllib.request, urllib.error, yaml, collections
|
||
|
|
args = [a for a in sys.argv[1:] if not a.startswith('--')]
|
||
|
|
files = '--files' in sys.argv
|
||
|
|
site, tokfile, records, places = args[:4]
|
||
|
|
buckets = args[4:]
|
||
|
|
tok=open(tokfile).read().strip()
|
||
|
|
def read(bucket):
|
||
|
|
r=urllib.request.Request(site+'/dhd/1/read',data=json.dumps({'bucket':bucket}).encode(),
|
||
|
|
headers={'Authorization':'Bearer '+tok,'Content-Type':'application/json'})
|
||
|
|
return json.load(urllib.request.urlopen(r))
|
||
|
|
def h(s): return hashlib.sha256(str(s).encode()).hexdigest()[:8]
|
||
|
|
def norm(s): return re.sub(r'\s+',' ',re.sub(r'<[^>]+>',' ',str(s))).strip()
|
||
|
|
bad=0
|
||
|
|
y=yaml.safe_load(open(records))
|
||
|
|
for b,recs in y.items():
|
||
|
|
if buckets and b not in buckets: continue
|
||
|
|
if not isinstance(recs,list): print(f'{b}: not a list in the export, skipped'); continue
|
||
|
|
st=read(b); c=collections.Counter(); diff=collections.Counter(); kinds=collections.Counter()
|
||
|
|
ids={str(r['id']) for r in recs}
|
||
|
|
for r in recs:
|
||
|
|
s=st.get(str(r['id']))
|
||
|
|
if s is None: c['missing in store']+=1; continue
|
||
|
|
c['found']+=1
|
||
|
|
if r.get('state') and r['state']!=s['state']: c['state differs']+=1
|
||
|
|
at=r.get('at')
|
||
|
|
if at is not None:
|
||
|
|
got=(s.get('at') or {}).get('text','')
|
||
|
|
if not str(got).startswith(str(at)[:10]): c['at differs']+=1
|
||
|
|
f=r.get('fields') or {}; g=s.get('responses') or {}
|
||
|
|
for k,v in f.items():
|
||
|
|
if k not in g: diff[k+': not in store']+=1
|
||
|
|
elif str(g[k])==str(v): pass
|
||
|
|
elif norm(g[k])==norm(v): diff[k+': differs in markup or spacing only']+=1
|
||
|
|
else: diff[k+': DIFFERS']+=1
|
||
|
|
for k in g:
|
||
|
|
if k not in f and g[k] not in ('',None): diff[k+': in store only']+=1
|
||
|
|
for k in ('vedlegg','bilde'):
|
||
|
|
v=g.get(k)
|
||
|
|
if not v: continue
|
||
|
|
if not files: kinds[k+' named']+=1; continue
|
||
|
|
try:
|
||
|
|
with urllib.request.urlopen(f'{site}/attachment/{b}/{r["id"]}/{k}', timeout=30) as a:
|
||
|
|
kinds[f'{k} fetched' if len(a.read())>0 else f'{k} EMPTY']+=1
|
||
|
|
except urllib.error.HTTPError as e:
|
||
|
|
# a list that needs a sign-in keeps its files to those signed in
|
||
|
|
kinds[f'{k} needs a sign-in' if e.code in (401,403) else f'{k} FAILED {e.code}']+=1
|
||
|
|
extra=[k for k in st if k not in ids]
|
||
|
|
states=collections.Counter(s['state'] for s in st.values())
|
||
|
|
print(f'{b}: export {len(recs)}, store {len(st)}, {dict(c)}, extra in store {len(extra)}')
|
||
|
|
print(f' states in store {dict(states)}')
|
||
|
|
if diff: print(f' fields {dict(diff)}')
|
||
|
|
if kinds: print(f' files {dict(kinds)}')
|
||
|
|
bad+=c['missing in store']+c['state differs']+c['at differs']+len(extra)+sum(n for k,n in diff.items() if 'DIFFERS' in k or 'not in store' in k)
|
||
|
|
bad+=sum(n for k,n in kinds.items() if 'FAILED' in k or 'EMPTY' in k)
|
||
|
|
# the sign-ups
|
||
|
|
p=yaml.safe_load(open(places)); st=read('pameldinger'); led=read('portal_places')
|
||
|
|
for b,recs in p.items():
|
||
|
|
live=sorted([r for r in recs if r.get('state') in ('tatt','venter')], key=lambda r:str(r['at']))
|
||
|
|
print(f'{b}: export {len(recs)} ({dict(collections.Counter(r.get("state") for r in recs))}), store {len(st)} ({dict(collections.Counter(s["state"] for s in st.values()))})')
|
||
|
|
sig=lambda f:(str(f.get('arrangement')),h(str(f.get('email','')).lower()),h(f.get('navn')),str(f.get('antall') or 1))
|
||
|
|
want=collections.Counter(sig(r['fields']) for r in live); got=collections.Counter(sig(s['responses']) for s in st.values())
|
||
|
|
print(f' same people, events and numbers: {want==got} (only in export {sum((want-got).values())}, only in store {sum((got-want).values())})')
|
||
|
|
bad+=sum((want-got).values())+sum((got-want).values())
|
||
|
|
seats=lambda rs,key:sum(int(key(r).get('antall') or 1) for r in rs)
|
||
|
|
print(f' guests: export {seats(live,lambda r:r["fields"])}, store {seats(st.values(),lambda s:s["responses"])}')
|
||
|
|
# the state each has: a seat stays a seat, a place in line a place in line
|
||
|
|
wst=collections.Counter((sig(r['fields']),r['state']) for r in live)
|
||
|
|
gst=collections.Counter((sig(s['responses']),s['state']) for s in st.values())
|
||
|
|
print(f' same state each: {wst==gst}'); bad+= (wst!=gst)
|
||
|
|
# the order: the ledger's against the order they came in 0.19
|
||
|
|
for key,l in led.items():
|
||
|
|
for line in ('taken','waiting'):
|
||
|
|
ids=[e['id'] for e in l.get(line,[])]
|
||
|
|
order=[sig(st[i]['responses']) for i in ids if i in st]
|
||
|
|
wanted=[sig(r['fields']) for r in live if r['state']==('tatt' if line=='taken' else 'venter') and str(r['fields'].get('arrangement')) in key]
|
||
|
|
ok = order==wanted
|
||
|
|
print(f' ledger {h(key)} {line}: {len(ids)} entries, each a record: {len(order)==len(ids)}, in the order they came: {ok}')
|
||
|
|
bad+= (not ok) or len(order)!=len(ids)
|
||
|
|
dup=[k for k,n in got.items() if n>1]; print(f' taken twice: {len(dup)}'); bad+=len(dup)
|
||
|
|
print('OK' if not bad else f'{bad} THINGS TO LOOK AT'); sys.exit(1 if bad else 0)
|