seed_demo.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Create a synthetic demo workspace; optionally import masked CSV references.
  2. Run with CDA_DATA_DIR set to the service's absolute data directory.
  3. """
  4. import argparse
  5. import json
  6. import sys
  7. from pathlib import Path
  8. ROOT=Path(__file__).resolve().parents[1]
  9. sys.path.insert(0,str(ROOT/'tools-service'))
  10. from toolsvc.db import database
  11. from toolsvc.objects import store
  12. from toolsvc.runs import runs
  13. def main():
  14. parser=argparse.ArgumentParser()
  15. parser.add_argument('--masked-dir',type=Path)
  16. args=parser.parse_args()
  17. existing={w['name']:w for w in store.list_workspaces()}
  18. name='合成样本演示(可复现)'
  19. if name in existing:
  20. workspace=existing[name]
  21. else:
  22. workspace=store.create_workspace(name,'使用合成样本演示原始数据处理、统计问答和行级溯源;不代表真实贸易数据。')
  23. source=store.register_upload(workspace['id'],'合成演示_small.csv',(ROOT/'samples/small.csv').read_bytes())
  24. run=runs.create(workspace['id'],source['id'],options={'demo':True})
  25. import time
  26. while run['status'] in {'queued','running'}:
  27. time.sleep(.3);run=runs.get(run['id'])
  28. if run['status']!='succeeded':raise RuntimeError(run.get('error'))
  29. imported=[]
  30. if args.masked_dir:
  31. name='真实统计参考(已遮掩)'
  32. if name not in existing:
  33. masked=store.create_workspace(name,'2026年11月11日结果:16张已遮掩统计表。金额数量中的区间不是精确值,不可取中点或对重复层级求和。此处没有原始申报明细。')
  34. for path in sorted(args.masked_dir.glob('*.csv')):
  35. item=store.register_upload(masked['id'],'【已遮掩】'+path.name,path.read_bytes(),kind='stat',producer='import_masked_reference')
  36. imported.append({'name':item['name'],'rows':item['rows']})
  37. print(json.dumps({'synthetic_workspace':workspace['id'],'masked_imports':imported},ensure_ascii=False,indent=2))
  38. if __name__=='__main__': main()