补全 realtime scheme 中的复合 存储、查询方法

This commit is contained in:
JIANG
2025-12-05 10:52:04 +08:00
parent ef2ad7e107
commit 03e2fb9fd8
7 changed files with 910 additions and 275 deletions

View File

@@ -25,7 +25,7 @@ class ScadaRepository:
)
@staticmethod
async def get_scada_by_id_time(
async def get_scada_by_id_time_range(
conn: AsyncConnection, device_id: str, start_time: datetime, end_time: datetime
) -> List[dict]:
async with conn.cursor() as cur:
@@ -36,19 +36,23 @@ class ScadaRepository:
return await cur.fetchall()
@staticmethod
async def get_scada_field_by_id_time(
conn: AsyncConnection, time: datetime, device_id: str, field: str
async def get_scada_field_by_id_time_range(
conn: AsyncConnection,
device_id: str,
start_time: datetime,
end_time: datetime,
field: str,
) -> Any:
valid_fields = {"monitored_value", "cleaned_value"}
if field not in valid_fields:
raise ValueError(f"Invalid field: {field}")
query = sql.SQL(
"SELECT {} FROM scada.scada_data WHERE time = %s AND device_id = %s"
"SELECT {} FROM scada.scada_data WHERE time >= %s AND time <= %s AND device_id = %s"
).format(sql.Identifier(field))
async with conn.cursor() as cur:
await cur.execute(query, (time, device_id))
await cur.execute(query, (start_time, end_time, device_id))
row = await cur.fetchone()
return row[field] if row else None
@@ -68,7 +72,7 @@ class ScadaRepository:
await cur.execute(query, (value, time, device_id))
@staticmethod
async def delete_scada_by_id_time(
async def delete_scada_by_id_time_range(
conn: AsyncConnection, device_id: str, start_time: datetime, end_time: datetime
):
async with conn.cursor() as cur: