feat(replay): execute periodic diagnosis and hydraulic checks
This commit is contained in:
@@ -139,6 +139,102 @@ class ScadaReplayRepository:
|
||||
)
|
||||
return [r["event"] for r in await cur.fetchall()]
|
||||
|
||||
async def queue_available(self) -> bool:
|
||||
cursor = await self.connection.execute("SELECT to_regclass('workbench.calculation_jobs') IS NOT NULL AS ready")
|
||||
return bool((await cursor.fetchone())["ready"])
|
||||
|
||||
async def periodic_events(self, replay_id, start, end, through_sequence: int) -> list[dict]:
|
||||
cursor = await self.connection.execute(
|
||||
"SELECT e.value AS event FROM peripheral_sample.scada_replay_batches b "
|
||||
"CROSS JOIN LATERAL jsonb_array_elements(COALESCE(b.document->'events','[]'::jsonb)) e "
|
||||
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
|
||||
"AND b.sequence>0 AND b.sequence<=%s "
|
||||
"AND (b.document->>'at')::timestamptz>=%s::timestamptz "
|
||||
"AND (b.document->>'at')::timestamptz<=%s::timestamptz ORDER BY b.sequence",
|
||||
(self.project_id, str(replay_id), through_sequence, start, end),
|
||||
)
|
||||
return [row["event"] for row in await cursor.fetchall()]
|
||||
|
||||
async def batch(self, replay_id, sequence: int) -> dict | None:
|
||||
cursor = await self.connection.execute(
|
||||
"SELECT document FROM peripheral_sample.scada_replay_batches WHERE project_id=%s AND replay_id=%s AND sequence=%s",
|
||||
(self.project_id, str(replay_id), sequence),
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return row["document"] if row else None
|
||||
|
||||
async def periodic_tasks(self, replay_id, offset=0, limit=100) -> list[dict]:
|
||||
cursor = await self.connection.execute(
|
||||
"SELECT t.value AS execution FROM peripheral_sample.scada_replay_batches b "
|
||||
"CROSS JOIN LATERAL jsonb_each(COALESCE(b.document->'scheduled_tasks','{}'::jsonb)) t "
|
||||
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
|
||||
"ORDER BY b.sequence DESC,t.value->>'task_key' LIMIT %s OFFSET %s",
|
||||
(self.project_id, str(replay_id), limit, offset),
|
||||
)
|
||||
return [row["execution"] for row in await cursor.fetchall()]
|
||||
|
||||
async def periodic_task(self, replay_id, execution_id) -> dict | None:
|
||||
cursor = await self.connection.execute(
|
||||
"SELECT document->'scheduled_tasks'->%s AS execution FROM peripheral_sample.scada_replay_batches "
|
||||
"WHERE project_id=%s AND replay_id=%s AND stage='detected' "
|
||||
"AND document->'scheduled_tasks' ? %s LIMIT 1",
|
||||
(str(execution_id), self.project_id, str(replay_id), str(execution_id)),
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return row["execution"] if row else None
|
||||
|
||||
async def previous_simulations(self, replay_id, before, limit=2) -> list[dict]:
|
||||
cursor = await self.connection.execute(
|
||||
"SELECT t.value AS execution FROM peripheral_sample.scada_replay_batches b "
|
||||
"CROSS JOIN LATERAL jsonb_each(COALESCE(b.document->'scheduled_tasks','{}'::jsonb)) t "
|
||||
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
|
||||
"AND t.value->>'task_key'='network-simulation' AND t.value->>'status'='completed' "
|
||||
"AND (t.value->>'period_end')::timestamptz<%s::timestamptz "
|
||||
"ORDER BY (t.value->>'period_end')::timestamptz DESC LIMIT %s",
|
||||
(self.project_id, str(replay_id), before, limit),
|
||||
)
|
||||
return [row["execution"] for row in await cursor.fetchall()]
|
||||
|
||||
async def reference_simulations(self, replay_id, period_ends: list[datetime]) -> list[dict]:
|
||||
cursor = await self.connection.execute(
|
||||
"SELECT t.value AS execution FROM peripheral_sample.scada_replay_batches b "
|
||||
"CROSS JOIN LATERAL jsonb_each(COALESCE(b.document->'scheduled_tasks','{}'::jsonb)) t "
|
||||
"WHERE b.project_id=%s AND b.replay_id=%s AND b.stage='detected' "
|
||||
"AND t.value->>'task_key'='network-simulation' AND t.value->>'status'='completed' "
|
||||
"AND (t.value->>'period_end')::timestamptz=ANY(%s::timestamptz[]) "
|
||||
"ORDER BY (t.value->>'period_end')::timestamptz DESC LIMIT 7",
|
||||
(self.project_id, str(replay_id), period_ends),
|
||||
)
|
||||
return [row["execution"] for row in await cursor.fetchall()]
|
||||
|
||||
async def save_retry_receipt(self, replay_id, sequence, request_id, execution_id) -> None:
|
||||
await self.connection.execute(
|
||||
"UPDATE peripheral_sample.scada_replay_batches SET document=jsonb_set(document,'{periodic_retry_receipts}',"
|
||||
"COALESCE(document->'periodic_retry_receipts','{}'::jsonb) || jsonb_build_object(%s::text,%s::text)) "
|
||||
"WHERE project_id=%s AND replay_id=%s AND sequence=%s",
|
||||
(str(request_id), str(execution_id), self.project_id, str(replay_id), sequence),
|
||||
)
|
||||
|
||||
async def save_periodic_task(self, replay_id, sequence: int, execution: dict) -> None:
|
||||
cursor = await self.connection.execute(
|
||||
"UPDATE peripheral_sample.scada_replay_batches SET document=jsonb_set(document,'{scheduled_tasks}',"
|
||||
"COALESCE(document->'scheduled_tasks','{}'::jsonb) || jsonb_build_object(%s::text,%s::jsonb)) "
|
||||
"WHERE project_id=%s AND replay_id=%s AND sequence=%s AND stage='detected' RETURNING sequence",
|
||||
(str(execution["execution_id"]), Jsonb(execution), self.project_id, str(replay_id), sequence),
|
||||
)
|
||||
if await cursor.fetchone() is None:
|
||||
raise ValueError("PERIOD_BATCH_UNAVAILABLE")
|
||||
|
||||
async def simulation_state(self, replay_id, execution: dict) -> None:
|
||||
state = {"execution_id": str(execution["execution_id"]), "status": execution["status"],
|
||||
"error": execution["summary"] if execution["status"] == "failed" else None}
|
||||
await self.connection.execute(
|
||||
"UPDATE peripheral_sample.scada_replays SET document=jsonb_set(document,'{last_scheduled_simulation}',%s),updated_at=now() "
|
||||
"WHERE project_id=%s AND replay_id=%s "
|
||||
"AND (document->'last_monitoring'->>'period_end')::timestamptz=%s::timestamptz",
|
||||
(Jsonb(state), self.project_id, str(replay_id), execution["period_end"]),
|
||||
)
|
||||
|
||||
async def monitoring_outputs(
|
||||
self,
|
||||
replay_id,
|
||||
|
||||
Reference in New Issue
Block a user