refactor(db)!: finalize pooled WNDB v2 migration
This commit is contained in:
@@ -35,7 +35,8 @@ class ScadaRepository:
|
||||
) -> List[dict]:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(
|
||||
"SELECT * FROM scada.measurements WHERE device_id = ANY(%s) AND time >= %s AND time <= %s",
|
||||
"SELECT * FROM scada.measurements WHERE device_id = ANY(%s) "
|
||||
"AND time >= %s AND time <= %s ORDER BY device_id, time",
|
||||
(device_ids, start_time, end_time),
|
||||
)
|
||||
return await cur.fetchall()
|
||||
@@ -49,7 +50,8 @@ class ScadaRepository:
|
||||
) -> List[dict]:
|
||||
with conn.cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(
|
||||
"SELECT * FROM scada.measurements WHERE device_id = ANY(%s) AND time >= %s AND time <= %s",
|
||||
"SELECT * FROM scada.measurements WHERE device_id = ANY(%s) "
|
||||
"AND time >= %s AND time <= %s ORDER BY device_id, time",
|
||||
(device_ids, start_time, end_time),
|
||||
)
|
||||
return cur.fetchall()
|
||||
@@ -88,7 +90,9 @@ class ScadaRepository:
|
||||
raise ValueError(f"Invalid field: {field}")
|
||||
|
||||
query = sql.SQL(
|
||||
"SELECT device_id, time, {} FROM scada.measurements WHERE time >= %s AND time <= %s AND device_id = ANY(%s)"
|
||||
"SELECT device_id, time, {} FROM scada.measurements "
|
||||
"WHERE time >= %s AND time <= %s AND device_id = ANY(%s) "
|
||||
"ORDER BY device_id, time"
|
||||
).format(sql.Identifier(field))
|
||||
|
||||
async with conn.cursor() as cur:
|
||||
@@ -122,6 +126,31 @@ class ScadaRepository:
|
||||
if cur.rowcount == 0:
|
||||
await cur.execute(insert_query, (time, device_id, value))
|
||||
|
||||
@staticmethod
|
||||
async def update_scada_field_batch(
|
||||
conn: AsyncConnection,
|
||||
rows: list[tuple[datetime, str, float | None]],
|
||||
field: str,
|
||||
) -> int:
|
||||
"""Update existing SCADA samples in one set-based statement."""
|
||||
valid_fields = {"monitored_value", "cleaned_value"}
|
||||
if field not in valid_fields:
|
||||
raise ValueError(f"Invalid field: {field}")
|
||||
if not rows:
|
||||
return 0
|
||||
|
||||
query = sql.SQL(
|
||||
"UPDATE scada.measurements AS measurement SET {} = batch.value "
|
||||
"FROM unnest(%s::timestamptz[], %s::text[], %s::double precision[]) "
|
||||
"AS batch(time, device_id, value) "
|
||||
"WHERE measurement.time = batch.time "
|
||||
"AND measurement.device_id = batch.device_id"
|
||||
).format(sql.Identifier(field))
|
||||
times, device_ids, values = zip(*rows)
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, (list(times), list(device_ids), list(values)))
|
||||
return cur.rowcount
|
||||
|
||||
@staticmethod
|
||||
async def delete_scada_by_id_time_range(
|
||||
conn: AsyncConnection, device_id: str, start_time: datetime, end_time: datetime
|
||||
|
||||
Reference in New Issue
Block a user