66 lines
4.7 KiB
Python
66 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Synthetic, disposable application fixture for authenticated browser acceptance."""
|
|
import json
|
|
import re
|
|
import time
|
|
import uuid
|
|
from pathlib import Path
|
|
from isolated_runtime import isolated_api
|
|
from benchmark_scale import sql
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
OUT=ROOT/'output/20261113-defect-drilldown'
|
|
|
|
def literal(value):
|
|
if value is None:return 'NULL'
|
|
return "CONVERT(0x"+str(value).encode().hex()+" USING utf8mb4)"
|
|
|
|
def seed(database):
|
|
release=str(uuid.uuid4())
|
|
sql("INSERT INTO data_release(id,name,status,rule_version,mapping_version,complete) VALUES ("+','.join(map(literal,[release,'合成钻取验收','PUBLISHED','pq-dax-v1','observed-2026-v2']))+",TRUE);",database)
|
|
service=(ROOT/'backend/src/main/java/cn/yunsheng/quality/analysis/AnalysisFineDefectsService.java').read_text()
|
|
for stage in ['black','bright']:
|
|
document,revision,build=[str(uuid.uuid4()) for _ in range(3)]
|
|
sql("INSERT INTO source_document(id,source_slot,stage,source_year,source_month,source_file) VALUES("+','.join(map(literal,[document,'fixture-'+stage,stage]))+",2026,1,'synthetic.xlsx');",database)
|
|
sql("INSERT INTO source_revision(id,source_document_id,sha256,source_file,archive_path,file_size,parser_version,status) VALUES("+','.join(map(literal,[revision,document,'0'*64,'synthetic.xlsx','synthetic-only',0,'fixture','COMPLETE']))+");",database)
|
|
sql("INSERT INTO transform_build(id,source_revision_id,build_key,parser_version,rule_version,mapping_version,status) VALUES("+','.join(map(literal,[build,revision,uuid.uuid4().hex*2,'fixture','pq-dax-v1','observed-2026-v2','COMPLETE']))+");",database)
|
|
sql("INSERT INTO release_member(release_id,source_document_id,source_revision_id,transform_build_id) VALUES("+','.join(map(literal,[release,document,revision,build]))+");",database)
|
|
fieldblock=service.split('private static final List<DefectField> '+('BLACK_FIELDS' if stage=='black' else 'BRIGHT_FIELDS'))[1].split(');',1)[0]
|
|
fields=re.findall(r'field\("([^"]+)"',fieldblock)
|
|
row=0
|
|
for year,month in [(2025,1),(2026,1),(2026,2),(2026,3)]:
|
|
for product in ['合成产品A','合成产品B']:
|
|
row+=1;total=1000 if product.endswith('A') else 2000
|
|
child=10*month;parent=100*month
|
|
raw={key:0 for key in fields}
|
|
if stage=='bright':raw.update({'加工缺角重量':child,'加工磕边重量':parent-child})
|
|
else:raw.update({'责废缺角重量':child,'责废尺寸重量':parent-child})
|
|
codes=['black_responsibility_scrap','black_material_scrap','black_external_scrap'] if stage=='black' else ['bright_processing_scrap','bright_plating_scrap','bright_material_scrap','bright_diffusion_scrap','bright_headquarters_scrap']
|
|
components={key:0 for key in codes};components[codes[0]]=parent
|
|
values=[build,revision,'合成数据',row,f'{year}-{month:02d}','合成部门','合成业务组',product,total-parent,parent,total,json.dumps(raw,ensure_ascii=False),json.dumps(components)]
|
|
sql('INSERT INTO fact_'+stage+'(build_id,source_revision_id,sheet_name,physical_row_no,month_key,department,group_name,product,qualified_weight,scrap_weight,total_weight,raw_values,components) VALUES('+','.join(map(literal,values))+');',database)
|
|
sql("UPDATE current_release SET release_id="+literal(release)+",pointer_version=pointer_version+1 WHERE dataset_id='quality';",database)
|
|
return release
|
|
|
|
def main():
|
|
OUT.mkdir(parents=True,exist_ok=True)
|
|
stop=OUT/'stop-fixture'
|
|
if stop.exists():stop.unlink()
|
|
with isolated_api(port=5287,fresh=True,evidence_dir=OUT/'isolated') as (api,database,evidence):
|
|
release=seed(database)
|
|
context={'releaseId':release,'year':2026,'stage':'bright','category':'加工废','dimension':'period','trendGranularity':'month'}
|
|
status,data,_=api.call('/api/analysis/defects/drilldown',context)
|
|
assert status==200,(status,data)
|
|
assert len(data['fields'])==9,data
|
|
january=next(g for g in data['groups'] if g['key']=='2026-01')
|
|
assert abs(january['totalWeight']-3000)<1e-8
|
|
assert abs(next(c['rate'] for c in january['children'] if c['key']=='加工缺角重量')-20/3000)<1e-10
|
|
(OUT/'fixture-ready.json').write_text(json.dumps({'ready':True,'port':5287,'synthetic':True,'apiVerified':True,'releaseId':release},ensure_ascii=False,indent=2))
|
|
print('SYNTHETIC_FIXTURE_READY',flush=True)
|
|
deadline=time.monotonic()+1800
|
|
while not stop.exists() and time.monotonic()<deadline:time.sleep(1)
|
|
if not stop.exists():raise RuntimeError('Fixture acceptance timed out')
|
|
print('SYNTHETIC_FIXTURE_CLEANED',flush=True)
|
|
|
|
if __name__=='__main__':main()
|