verify-industry-recovered-data.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Exercise recovered real evidence through frozen read APIs, without network or dispatch."""
  2. import argparse
  3. from contextlib import closing
  4. import hashlib
  5. import json
  6. from pathlib import Path
  7. import sqlite3
  8. import sys
  9. ROOT=Path(__file__).resolve().parents[1]
  10. sys.path.insert(0,str(ROOT/'server/voc-library'))
  11. from industry_monitor import run
  12. from monitoring_dispatch import batch_preflight
  13. from monitoring_ledger import read
  14. def main():
  15. p=argparse.ArgumentParser(description=__doc__);p.add_argument('--snapshot',type=Path,required=True)
  16. p.add_argument('--out',type=Path,required=True);a=p.parse_args()
  17. store=ROOT/'data/monitoring-registry';snapshot=a.snapshot.resolve()
  18. sha=lambda path:hashlib.sha256(path.read_bytes()).hexdigest()
  19. before=sha(snapshot);database_before=sha(store/'monitoring.sqlite')
  20. generation=json.loads(snapshot.read_bytes())['generation']
  21. config={'snapshot':str(snapshot),'monitoringStore':str(store)}
  22. with closing(sqlite3.connect((store/'monitoring.sqlite').as_uri()+'?mode=ro',uri=True)) as c:
  23. c.row_factory=sqlite3.Row;_,rows=read(c)
  24. bound=[r for r in rows if r['params']]
  25. assert len(rows)==154 and len(bound)==139
  26. requests=[]
  27. for row in bound:
  28. modes=['general','feedbackdate'] if row['endpointId'].startswith('taobao.') else [None]
  29. for mode in modes:
  30. params=dict(row['params'])
  31. if mode:params['orderType']=mode
  32. requests.append({'endpointId':row['endpointId'],'params':params,'generation':generation,'window':'independent-recovery-audit'})
  33. requests.append(requests[0])
  34. audited=batch_preflight(store,requests,generation)
  35. assert len(audited['rows'])==143
  36. assert all(not r['dispatchAllowed'] and 'REQUEST_ALREADY_CHARGED_NO_AUTOMATIC_REPLAY' in r['blockingReasons'] for r in audited['rows'])
  37. assert all(len(r['alreadyCharged'])==sum(x['endpointId']==r['specification']['endpointId'] and x['params']=={k:v for k,v in r['specification']['params'].items() if k!='orderType'} for x in bound) for r in audited['rows'])
  38. assert 'DUPLICATE_REQUEST_IN_BATCH' in audited['rows'][-1]['blockingReasons']
  39. result=run(config,{'op':'listings','generation':generation,'filters':{'platform':'jd','shopId':'1000084104'},
  40. 'detailQuery':{'state':'TEXT_AVAILABLE','keyword':'久煮不烂'}})
  41. detail=result['detailEvidence']
  42. assert detail['coverage']['withResponse']==41 and detail['coverage']['withDetailText']==12
  43. found=[r for r in detail['rows'] if r['productId']=='jd:100005232115']
  44. assert len(found)==1 and any(f['text']=='麻辣爽口|久煮不烂|醇厚口感' for f in found[0]['fields'])
  45. assert not found[0]['rawAvailableLocally'] and found[0]['projectionVerified']
  46. assert before==sha(snapshot) and database_before==sha(store/'monitoring.sqlite')
  47. scope=json.loads((ROOT/'handoff/collector-scope.json').read_bytes())
  48. assert scope['chargedHistory']['identifiedRows']==154 and scope['chargedHistory']['parameterBoundRows']==139
  49. assert sum(len(r['settledRequests']) for r in scope['primaryProducts'])>76
  50. receipt={'ok':True,'generation':generation,'snapshotSha256':before,'identifiedRows':154,'parameterBoundRows':139,
  51. 'unboundRows':15,'preflightCases':143,'allPaidPagesBlocked':True,'taobaoBothModesBlocked':True,
  52. 'duplicateInBatchRejected':True,'primaryDetailsWithResponse':41,'primaryDetailsWithText':12,
  53. 'recoveredLiteralTextVerified':True,'snapshotUnchanged':True,'auxiliaryUnchanged':True,
  54. 'chargedMinor':4620,'paidCalls':0,'productionWrites':0}
  55. a.out.parent.mkdir(parents=True,exist_ok=True);a.out.write_text(json.dumps(receipt,ensure_ascii=False,indent=2)+'\n','utf-8')
  56. print(json.dumps(receipt,ensure_ascii=False))
  57. if __name__=='__main__':main()