import sys,pathlib,json,decimal,datetime sys.path.insert(0,'/tmp/codex-mileage-diag-pymysql.zip');sys.path.insert(0,'/tmp') from db_access import connect root=pathlib.Path('/opt/lingniu-go-native/backups/mileage-all-baseline-20260918');root.mkdir(parents=True,exist_ok=True) c=connect();D=decimal.Decimal;changes=[] with c.cursor() as q: q.execute('START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY') q.execute("""SELECT * FROM vehicle_daily_mileage_source WHERE stat_date BETWEEN '2026-09-15' AND '2026-09-18' AND quality_status='OK' AND latest_total_mileage_km>0 AND first_event_time IS NOT NULL AND source_ip NOT IN ('legacy-mysql.lingniu-prod','manual-lingniu-prod-day-mileage') AND COALESCE(quality_reason,'')<>'gps_coordinate_accumulation' AND latest_event_time>=TIMESTAMP(stat_date) AND latest_event_time0 AND latest_event_time>=TIMESTAMP(stat_date) AND latest_event_timeD(2500*max(1,(r['latest_event_time'].date()-p['latest_event_time'].date()).days)):status='INVALID_DELTA';reason='outside_daily_range' changes.append(dict(before=r,baseline=dict(total=p['latest_total_mileage_km'],time=p['latest_event_time']),daily=delta,status=status,reason=reason)) c.rollback();c.close() (root/'plan.json').write_text(json.dumps(changes,default=str)) from collections import Counter print(json.dumps({'scanned':len(rows),'changes':len(changes),'byProtocol':dict(Counter(x['before']['protocol'] for x in changes)),'invalid':sum(x['status']!='OK' for x in changes),'examples':[dict(vin=x['before']['vin'],date=x['before']['stat_date'],protocol=x['before']['protocol'],old=x['before']['daily_mileage_km'],new=x['daily']) for x in changes if x['before']['vin']=='LMRKH9AC8R1004109']},default=str))