|
@@ -30,14 +30,14 @@ from pathlib import Path
|
|
|
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
|
|
|
|
-from models.common import alarm_store, db, devices, io, queries
|
|
|
|
|
|
|
+from models.common import alarm_store, db, devices, io, model_predict, queries
|
|
|
from models.generalized import pressure_algorithm
|
|
from models.generalized import pressure_algorithm
|
|
|
from models.vibration import vibration_algorithm
|
|
from models.vibration import vibration_algorithm
|
|
|
|
|
|
|
|
REALTIME_CHUNK_DAYS = 7
|
|
REALTIME_CHUNK_DAYS = 7
|
|
|
|
|
|
|
|
|
|
|
|
|
-def run_tick(now: datetime, device_list: list[str], lookback_days: int, write: bool, journal: list | None) -> dict:
|
|
|
|
|
|
|
+def run_tick(now: datetime, device_list: list[str], lookback_days: int, write: bool, journal: list | None, with_model: bool = False) -> dict:
|
|
|
device_batches = devices.load_device_batches(device_list)
|
|
device_batches = devices.load_device_batches(device_list)
|
|
|
unit_by_batch = devices.batch_to_unit(device_batches)
|
|
unit_by_batch = devices.batch_to_unit(device_batches)
|
|
|
batch_device = devices.batch_to_device(device_batches)
|
|
batch_device = devices.batch_to_device(device_batches)
|
|
@@ -53,6 +53,9 @@ def run_tick(now: datetime, device_list: list[str], lookback_days: int, write: b
|
|
|
rows = queries.fetch_all_hourly(start, end, REALTIME_CHUNK_DAYS, unit_by_batch, configs, sorted(unit_by_batch))
|
|
rows = queries.fetch_all_hourly(start, end, REALTIME_CHUNK_DAYS, unit_by_batch, configs, sorted(unit_by_batch))
|
|
|
hourly_levels, hour_segments, cycles = pressure_algorithm.run(rows, configs)
|
|
hourly_levels, hour_segments, cycles = pressure_algorithm.run(rows, configs)
|
|
|
events = vibration_algorithm.run(rows, configs)
|
|
events = vibration_algorithm.run(rows, configs)
|
|
|
|
|
+ if with_model:
|
|
|
|
|
+ model_predict.annotate("pressure", rows, hour_segments)
|
|
|
|
|
+ model_predict.annotate("vibration", rows, events)
|
|
|
alarms = alarm_store.build_alarms(io.clean_rows(hour_segments), io.clean_rows(events), batch_device)
|
|
alarms = alarm_store.build_alarms(io.clean_rows(hour_segments), io.clean_rows(events), batch_device)
|
|
|
result = alarm_store.merge_realtime(alarms, start, end, journal=journal if write else None, dry_run=not write)
|
|
result = alarm_store.merge_realtime(alarms, start, end, journal=journal if write else None, dry_run=not write)
|
|
|
return {
|
|
return {
|
|
@@ -86,6 +89,7 @@ def main() -> None:
|
|
|
parser.add_argument("--loop", action="store_true", help="按 --interval 循环运行")
|
|
parser.add_argument("--loop", action="store_true", help="按 --interval 循环运行")
|
|
|
parser.add_argument("--interval", type=int, default=300, help="循环间隔秒,默认 300")
|
|
parser.add_argument("--interval", type=int, default=300, help="循环间隔秒,默认 300")
|
|
|
parser.add_argument("--simulate", default=None, help="逗号分隔的时间列表,冒烟测试")
|
|
parser.add_argument("--simulate", default=None, help="逗号分隔的时间列表,冒烟测试")
|
|
|
|
|
+ parser.add_argument("--with-model", action="store_true", help="对故障段做模型滑动预测(默认关闭)")
|
|
|
parser.add_argument("--no-rollback", action="store_true", help="冒烟测试后不回滚数据库")
|
|
parser.add_argument("--no-rollback", action="store_true", help="冒烟测试后不回滚数据库")
|
|
|
args = parser.parse_args()
|
|
args = parser.parse_args()
|
|
|
device_list = devices.normalize_devices(args.device)
|
|
device_list = devices.normalize_devices(args.device)
|
|
@@ -96,7 +100,7 @@ def main() -> None:
|
|
|
times = [queries.parse_dt(item.strip()) for item in args.simulate.split(",") if item.strip()]
|
|
times = [queries.parse_dt(item.strip()) for item in args.simulate.split(",") if item.strip()]
|
|
|
journal: list = []
|
|
journal: list = []
|
|
|
for moment in times:
|
|
for moment in times:
|
|
|
- summary = run_tick(moment, device_list, args.lookback_days, write=True, journal=journal)
|
|
|
|
|
|
|
+ summary = run_tick(moment, device_list, args.lookback_days, write=True, journal=journal, with_model=args.with_model)
|
|
|
print(format_tick(summary), flush=True)
|
|
print(format_tick(summary), flush=True)
|
|
|
print(" 当前告警分布:" + snapshot(device_list), flush=True)
|
|
print(" 当前告警分布:" + snapshot(device_list), flush=True)
|
|
|
if not args.no_rollback:
|
|
if not args.no_rollback:
|
|
@@ -107,11 +111,11 @@ def main() -> None:
|
|
|
if args.loop:
|
|
if args.loop:
|
|
|
print(f"实时服务启动,每 {args.interval} 秒运行一次;Ctrl+C 退出。", flush=True)
|
|
print(f"实时服务启动,每 {args.interval} 秒运行一次;Ctrl+C 退出。", flush=True)
|
|
|
while True:
|
|
while True:
|
|
|
- summary = run_tick(datetime.now(), device_list, args.lookback_days, write=True, journal=None)
|
|
|
|
|
|
|
+ summary = run_tick(datetime.now(), device_list, args.lookback_days, write=True, journal=None, with_model=args.with_model)
|
|
|
print(format_tick(summary), flush=True)
|
|
print(format_tick(summary), flush=True)
|
|
|
time.sleep(args.interval)
|
|
time.sleep(args.interval)
|
|
|
|
|
|
|
|
- summary = run_tick(datetime.now(), device_list, args.lookback_days, write=True, journal=None)
|
|
|
|
|
|
|
+ summary = run_tick(datetime.now(), device_list, args.lookback_days, write=True, journal=None, with_model=args.with_model)
|
|
|
print(format_tick(summary))
|
|
print(format_tick(summary))
|
|
|
print("当前告警分布:" + snapshot(device_list))
|
|
print("当前告警分布:" + snapshot(device_list))
|
|
|
|
|
|