fix(scada): verify first sample after field completion
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from typing import List, Any
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from collections import defaultdict
|
||||
from psycopg import AsyncConnection, Connection, sql
|
||||
from psycopg.rows import dict_row
|
||||
@@ -131,6 +131,29 @@ class ScadaRepository:
|
||||
)
|
||||
return await cur.fetchall()
|
||||
|
||||
@staticmethod
|
||||
async def get_recovery_scada_readings(
|
||||
conn: AsyncConnection, device_ids: list[str], incident_at: datetime,
|
||||
returned_at: datetime, through: datetime, *, baseline_limit: int = 100_001,
|
||||
) -> list[dict]:
|
||||
"""Read the causal reference and exactly the first stored frame after T."""
|
||||
if not device_ids or through <= returned_at:
|
||||
return []
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(
|
||||
"WITH next_frame AS (SELECT min(time) AS time FROM scada.measurements "
|
||||
"WHERE device_id=ANY(%s) AND time>%s AND time<=%s), "
|
||||
"reference AS (SELECT time,device_id,monitored_value,cleaned_value "
|
||||
"FROM scada.measurements WHERE device_id=ANY(%s) AND time>=%s AND time<%s "
|
||||
"ORDER BY time,device_id LIMIT %s) "
|
||||
"SELECT time,device_id,monitored_value,cleaned_value FROM reference UNION ALL "
|
||||
"SELECT m.time,m.device_id,m.monitored_value,m.cleaned_value FROM scada.measurements m "
|
||||
"JOIN next_frame f ON m.time=f.time WHERE m.device_id=ANY(%s) ORDER BY time,device_id",
|
||||
(device_ids, returned_at, through, device_ids, incident_at - timedelta(days=15),
|
||||
incident_at, baseline_limit, device_ids),
|
||||
)
|
||||
return [dict(row) for row in await cur.fetchall()]
|
||||
|
||||
@staticmethod
|
||||
async def get_latest_scada_readings(
|
||||
conn: AsyncConnection,
|
||||
|
||||
Reference in New Issue
Block a user