211 lines
8.8 KiB
Python
211 lines
8.8 KiB
Python
"""Deterministic monitoring summaries built only from frozen replay outputs."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta
|
|
from statistics import median
|
|
from typing import Any
|
|
from uuid import NAMESPACE_URL, uuid5
|
|
|
|
import pandas as pd
|
|
|
|
from app.domain.schemas.scada_replay import ScadaReplayCreate
|
|
from app.services.burst_detection import (
|
|
TARGET_MU,
|
|
TARGET_N_ESTIMATORS,
|
|
TARGET_RANDOM_STATE,
|
|
TARGET_SCORE_THRESHOLD,
|
|
run_burst_detection,
|
|
)
|
|
|
|
|
|
def _time(value: str | datetime) -> datetime:
|
|
return value if isinstance(value, datetime) else datetime.fromisoformat(value)
|
|
|
|
|
|
def _valid_rows(rows: list[dict], device_id: str) -> list[dict]:
|
|
return sorted(
|
|
(
|
|
row for row in rows
|
|
if row.get("device_id") == device_id
|
|
and row.get("quality") in {"valid", "corrected"}
|
|
and row.get("cleaned_value") is not None
|
|
),
|
|
key=lambda row: _time(row["time"]),
|
|
)
|
|
|
|
|
|
def _relative_indicator(
|
|
rows: list[dict],
|
|
config: ScadaReplayCreate,
|
|
device_ids: list[str],
|
|
period_start: datetime,
|
|
period_end: datetime,
|
|
*,
|
|
direction: str,
|
|
) -> dict[str, Any]:
|
|
details: list[dict[str, Any]] = []
|
|
gaps: list[dict[str, str]] = []
|
|
for device_id in device_ids:
|
|
rule = next((
|
|
item for item in config.rules
|
|
if item.kind == "relative_change" and device_id in item.device_ids
|
|
and item.threshold is not None
|
|
), None)
|
|
if rule is None:
|
|
gaps.append({"device_id": device_id, "reason": "relative_change_rule_missing"})
|
|
continue
|
|
valid = _valid_rows(rows, device_id)
|
|
baseline = [row for row in valid if _time(row["time"]) < period_start][-rule.window_samples:]
|
|
current = [row for row in valid if period_start <= _time(row["time"]) <= period_end]
|
|
if len(baseline) < rule.minimum_samples:
|
|
gaps.append({"device_id": device_id, "reason": "baseline_not_ready"})
|
|
continue
|
|
if not current:
|
|
gaps.append({"device_id": device_id, "reason": "period_reading_missing"})
|
|
continue
|
|
reference = median(float(row["cleaned_value"]) for row in baseline)
|
|
if reference == 0:
|
|
gaps.append({"device_id": device_id, "reason": "zero_baseline"})
|
|
continue
|
|
latest = current[-1]
|
|
value = float(latest["cleaned_value"])
|
|
change = (value - reference) / abs(reference) * 100
|
|
hit = change <= -rule.threshold if direction == "decrease" else change >= rule.threshold
|
|
details.append({
|
|
"device_id": device_id,
|
|
"observed_at": _time(latest["time"]).isoformat(),
|
|
"baseline": reference,
|
|
"value": value,
|
|
"change_percent": change,
|
|
"threshold_percent": rule.threshold,
|
|
"hit": hit,
|
|
})
|
|
return {
|
|
"status": "ready" if not gaps else "insufficient_data",
|
|
"hit": any(item["hit"] for item in details),
|
|
"hit_device_ids": [item["device_id"] for item in details if item["hit"]],
|
|
"devices": details,
|
|
"gaps": gaps,
|
|
}
|
|
|
|
|
|
def _burst_detection(
|
|
rows: list[dict],
|
|
pressure_device_ids: list[str],
|
|
interval_seconds: int,
|
|
period_end: datetime,
|
|
) -> dict[str, Any]:
|
|
if len(pressure_device_ids) < 5:
|
|
return {"status": "sensor_coverage_insufficient", "hit": False,
|
|
"required_sensors": 5, "available_sensors": len(pressure_device_ids)}
|
|
if interval_seconds % 60 or 86400 % interval_seconds:
|
|
return {"status": "unsupported_sampling_interval", "hit": False,
|
|
"interval_seconds": interval_seconds}
|
|
points_per_day = 86400 // interval_seconds
|
|
count = 15 * points_per_day
|
|
series_by_device: dict[str, dict[datetime, float]] = {}
|
|
common_times: set[datetime] | None = None
|
|
for device_id in pressure_device_ids:
|
|
values = {_time(row["time"]): float(row["cleaned_value"])
|
|
for row in _valid_rows(rows, device_id) if _time(row["time"]) <= period_end}
|
|
series_by_device[device_id] = values
|
|
common_times = set(values) if common_times is None else common_times & set(values)
|
|
if not common_times:
|
|
return {"status": "baseline_not_ready", "hit": False, "required_samples": count}
|
|
target = max(common_times)
|
|
expected = [target - timedelta(seconds=interval_seconds * offset)
|
|
for offset in range(count - 1, -1, -1)]
|
|
incomplete = [device_id for device_id, values in series_by_device.items()
|
|
if any(at not in values for at in expected)]
|
|
if incomplete:
|
|
return {"status": "baseline_not_ready", "hit": False, "required_samples": count,
|
|
"incomplete_device_ids": incomplete}
|
|
frame = pd.DataFrame({device_id: [series_by_device[device_id][at] for at in expected]
|
|
for device_id in pressure_device_ids}, index=expected)
|
|
try:
|
|
result = run_burst_detection(
|
|
network="scada-replay",
|
|
username="scada-replay",
|
|
observed_pressure_data=frame,
|
|
points_per_day=points_per_day,
|
|
mu=TARGET_MU,
|
|
iforest_params={
|
|
"n_estimators": TARGET_N_ESTIMATORS,
|
|
"random_state": TARGET_RANDOM_STATE,
|
|
"contamination": "auto",
|
|
},
|
|
sensor_nodes=pressure_device_ids,
|
|
)
|
|
except (TypeError, ValueError) as exc:
|
|
return {"status": "analysis_failed", "hit": False, "reason": str(exc)[:300]}
|
|
latest = result["rows"][-1]
|
|
score = float(latest["Score"])
|
|
return {
|
|
"status": "ready",
|
|
"hit": score <= TARGET_SCORE_THRESHOLD,
|
|
"target_time": target.isoformat(),
|
|
"target_score": score,
|
|
"score_threshold": TARGET_SCORE_THRESHOLD,
|
|
"sensor_count": len(pressure_device_ids),
|
|
"reference_day_count": 14,
|
|
"latest_sensor_rankings": result["summary"].get("latest_sensor_rankings", []),
|
|
}
|
|
|
|
|
|
def evaluate_monitoring_period(
|
|
document: dict,
|
|
rows: list[dict],
|
|
*,
|
|
period_start: datetime,
|
|
period_end: datetime,
|
|
batch_sequence: int,
|
|
) -> dict[str, Any]:
|
|
config = ScadaReplayCreate.model_validate(document["config"])
|
|
monitoring = document.get("monitoring_config")
|
|
rule_version = (monitoring or {}).get("rule_version", "unconfigured")
|
|
monitoring_id = str(uuid5(
|
|
NAMESPACE_URL,
|
|
f"{document['replay_id']}:{period_end.isoformat()}:{rule_version}",
|
|
))
|
|
base = {
|
|
"monitoring_id": monitoring_id,
|
|
"period_start": period_start.isoformat(),
|
|
"period_end": period_end.isoformat(),
|
|
"knowledge_cutoff": period_end.isoformat(),
|
|
"batch_sequence": batch_sequence,
|
|
"rule_version": rule_version,
|
|
"task_id": None,
|
|
}
|
|
if not monitoring:
|
|
return {**base, "status": "insufficient_data", "detail": "项目未配置 SCADA 监测设备角色",
|
|
"pressure": {"status": "unconfigured", "hit": False},
|
|
"outlet_flow": {"status": "unconfigured", "hit": False},
|
|
"burst_detection": {"status": "not_run", "hit": False}}
|
|
pressure_ids = monitoring.get("pressure_device_ids", [])
|
|
flow_ids = monitoring.get("outlet_flow_device_ids", [])
|
|
selected = set(config.device_ids)
|
|
role_gaps = sorted((set(pressure_ids) | set(flow_ids)) - selected)
|
|
if not pressure_ids or not flow_ids or role_gaps:
|
|
return {**base, "status": "insufficient_data", "detail": "监测角色为空或不属于本轮回放设备",
|
|
"role_gaps": role_gaps,
|
|
"pressure": {"status": "unconfigured", "hit": False},
|
|
"outlet_flow": {"status": "unconfigured", "hit": False},
|
|
"burst_detection": {"status": "not_run", "hit": False}}
|
|
pressure = _relative_indicator(rows, config, pressure_ids, period_start, period_end, direction="decrease")
|
|
outlet_flow = _relative_indicator(rows, config, flow_ids, period_start, period_end, direction="increase")
|
|
detector = _burst_detection(
|
|
rows, pressure_ids, int(document["monitoring_interval_seconds"]), period_end,
|
|
)
|
|
complete = pressure["status"] == outlet_flow["status"] == detector["status"] == "ready"
|
|
hits = (bool(detector["hit"]), bool(pressure["hit"]), bool(outlet_flow["hit"]))
|
|
if not complete:
|
|
status, detail = "insufficient_data", "监测数据、设备角色或爆管侦测基线不足"
|
|
elif all(hits):
|
|
status, detail = "suspected_burst", "爆管侦测、压力下降与出厂边界流量增加相互支持"
|
|
elif any(hits):
|
|
status, detail = "attention", "仅部分爆管相关指标命中,需要继续观察或人工核查"
|
|
else:
|
|
status, detail = "normal", "本周期爆管相关指标均未命中"
|
|
return {**base, "status": status, "detail": detail, "pressure": pressure,
|
|
"outlet_flow": outlet_flow, "burst_detection": detector}
|