scan_all.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. """Run both reconstructed scanners and cache the shared hourly intermediate.
  3. Usage
  4. -----
  5. ::
  6. # Full DB scan (authoritative reproduction), cache hourly for later offline runs
  7. python models/scan_all.py --save-hourly
  8. # Offline rerun from a cached hourly table
  9. python models/scan_all.py --hourly-csv models/datas/hourly/pks_hourly_full.csv
  10. """
  11. from __future__ import annotations
  12. import argparse
  13. import sys
  14. from pathlib import Path
  15. sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
  16. from models.common import db, io, paths, queries
  17. from models.generalized import pressure_algorithm
  18. from models.vibration import vibration_algorithm
  19. def main() -> None:
  20. parser = argparse.ArgumentParser(description="PKS 压力+振动重构扫描器")
  21. parser.add_argument("--batch", nargs="+", type=int)
  22. parser.add_argument("--start", default="2025-01-01 00:00:00")
  23. parser.add_argument("--end", default="2026-09-01 00:00:00")
  24. parser.add_argument("--chunk-days", type=int, default=14)
  25. parser.add_argument("--hourly-csv", default=None, help="复用已落盘的小时中间表,不查询数据库")
  26. parser.add_argument("--save-hourly", action="store_true", help="扫描后将小时中间表写入 models/datas/hourly/pks_hourly_full.csv")
  27. args = parser.parse_args()
  28. batch_to_unit = db.load_pks_batches()
  29. selected = args.batch or sorted(batch_to_unit)
  30. configs = db.load_alarm_config({batch: batch_to_unit[batch] for batch in selected})
  31. if args.hourly_csv:
  32. print(f"复用小时中间表:{args.hourly_csv}")
  33. rows = queries.load_hourly_csv(Path(args.hourly_csv))
  34. else:
  35. start, end = queries.parse_dt(args.start), queries.parse_dt(args.end)
  36. print(f"只读数据库扫描 {start} .. {end},批次 {selected}")
  37. rows = queries.fetch_all_hourly(start, end, args.chunk_days, batch_to_unit, configs, selected)
  38. if args.save_hourly:
  39. queries.write_hourly_csv(paths.HOURLY_FULL, rows)
  40. print(f"小时中间表已缓存:{paths.HOURLY_FULL}")
  41. io.write_thresholds(paths.COMMON_RESULTS / "报警阈值配置.csv", configs)
  42. hourly_levels, hour_segments, cycles = pressure_algorithm.run(rows, configs)
  43. pressure_algorithm.write_results(paths.PRESSURE_RESULTS, hourly_levels, hour_segments, cycles)
  44. events = vibration_algorithm.run(rows, configs)
  45. vibration_algorithm.write_results(paths.VIBRATION_RESULTS, events)
  46. print(f"小时特征:{len(rows):,} 行")
  47. print(f"润滑油压力小时等级:{len(hourly_levels):,} 行")
  48. print(f"润滑油压力小时阶段段:{len(hour_segments):,} 段")
  49. print(f"润滑油压力周期:{len(cycles):,} 个")
  50. print(f"振动异常段:{len(events):,} 段")
  51. if __name__ == "__main__":
  52. main()