#!/usr/bin/env python3 """Run both reconstructed scanners and cache the shared hourly intermediate. Usage ----- :: # Full DB scan (authoritative reproduction), cache hourly for later offline runs python models/scan_all.py --save-hourly # Offline rerun from a cached hourly table python models/scan_all.py --hourly-csv models/datas/hourly/pks_hourly_full.csv """ from __future__ import annotations import argparse import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from models.common import db, io, paths, queries from models.generalized import pressure_algorithm from models.vibration import vibration_algorithm def main() -> None: parser = argparse.ArgumentParser(description="PKS 压力+振动重构扫描器") parser.add_argument("--batch", nargs="+", type=int) parser.add_argument("--start", default="2025-01-01 00:00:00") parser.add_argument("--end", default="2026-09-01 00:00:00") parser.add_argument("--chunk-days", type=int, default=14) parser.add_argument("--hourly-csv", default=None, help="复用已落盘的小时中间表,不查询数据库") parser.add_argument("--save-hourly", action="store_true", help="扫描后将小时中间表写入 models/datas/hourly/pks_hourly_full.csv") args = parser.parse_args() batch_to_unit = db.load_pks_batches() selected = args.batch or sorted(batch_to_unit) configs = db.load_alarm_config({batch: batch_to_unit[batch] for batch in selected}) if args.hourly_csv: print(f"复用小时中间表:{args.hourly_csv}") rows = queries.load_hourly_csv(Path(args.hourly_csv)) else: start, end = queries.parse_dt(args.start), queries.parse_dt(args.end) print(f"只读数据库扫描 {start} .. {end},批次 {selected}") rows = queries.fetch_all_hourly(start, end, args.chunk_days, batch_to_unit, configs, selected) if args.save_hourly: queries.write_hourly_csv(paths.HOURLY_FULL, rows) print(f"小时中间表已缓存:{paths.HOURLY_FULL}") io.write_thresholds(paths.COMMON_RESULTS / "报警阈值配置.csv", configs) hourly_levels, hour_segments, cycles = pressure_algorithm.run(rows, configs) pressure_algorithm.write_results(paths.PRESSURE_RESULTS, hourly_levels, hour_segments, cycles) events = vibration_algorithm.run(rows, configs) vibration_algorithm.write_results(paths.VIBRATION_RESULTS, events) print(f"小时特征:{len(rows):,} 行") print(f"润滑油压力小时等级:{len(hourly_levels):,} 行") print(f"润滑油压力小时阶段段:{len(hour_segments):,} 段") print(f"润滑油压力周期:{len(cycles):,} 个") print(f"振动异常段:{len(events):,} 段") if __name__ == "__main__": main()