删除旧文件;更新数据库查询方法
This commit is contained in:
@@ -2,10 +2,11 @@ from typing import List, Any
|
||||
from datetime import datetime
|
||||
from psycopg import AsyncConnection, sql
|
||||
|
||||
|
||||
class ScadaRepository:
|
||||
|
||||
|
||||
@staticmethod
|
||||
async def insert_batch(conn: AsyncConnection, data: List[dict]):
|
||||
async def insert_scada_batch(conn: AsyncConnection, data: List[dict]):
|
||||
if not data:
|
||||
return
|
||||
|
||||
@@ -14,26 +15,64 @@ class ScadaRepository:
|
||||
"COPY scada.scada_data (time, device_id, monitored_value, cleaned_value) FROM STDIN"
|
||||
) as copy:
|
||||
for item in data:
|
||||
await copy.write_row((
|
||||
item['time'], item['device_id'], item.get('monitored_value'), item.get('cleaned_value')
|
||||
))
|
||||
await copy.write_row(
|
||||
(
|
||||
item["time"],
|
||||
item["device_id"],
|
||||
item.get("monitored_value"),
|
||||
item.get("cleaned_value"),
|
||||
)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def get_data_by_time(conn: AsyncConnection, device_id: str, start_time: datetime, end_time: datetime) -> List[dict]:
|
||||
async def get_scada_by_id_time(
|
||||
conn: AsyncConnection, device_id: str, start_time: datetime, end_time: datetime
|
||||
) -> List[dict]:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(
|
||||
"SELECT * FROM scada.scada_data WHERE device_id = %s AND time >= %s AND time <= %s",
|
||||
(device_id, start_time, end_time)
|
||||
(device_id, start_time, end_time),
|
||||
)
|
||||
return await cur.fetchall()
|
||||
|
||||
@staticmethod
|
||||
async def update_field(conn: AsyncConnection, time: datetime, device_id: str, field: str, value: Any):
|
||||
async def get_scada_field_by_id_time(
|
||||
conn: AsyncConnection, time: datetime, device_id: str, field: str
|
||||
) -> Any:
|
||||
valid_fields = {"monitored_value", "cleaned_value"}
|
||||
if field not in valid_fields:
|
||||
raise ValueError(f"Invalid field: {field}")
|
||||
|
||||
query = sql.SQL("UPDATE scada.scada_data SET {} = %s WHERE time = %s AND device_id = %s").format(sql.Identifier(field))
|
||||
|
||||
query = sql.SQL(
|
||||
"SELECT {} FROM scada.scada_data WHERE time = %s AND device_id = %s"
|
||||
).format(sql.Identifier(field))
|
||||
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, (time, device_id))
|
||||
row = await cur.fetchone()
|
||||
return row[field] if row else None
|
||||
|
||||
@staticmethod
|
||||
async def update_scada_field(
|
||||
conn: AsyncConnection, time: datetime, device_id: str, field: str, value: Any
|
||||
):
|
||||
valid_fields = {"monitored_value", "cleaned_value"}
|
||||
if field not in valid_fields:
|
||||
raise ValueError(f"Invalid field: {field}")
|
||||
|
||||
query = sql.SQL(
|
||||
"UPDATE scada.scada_data SET {} = %s WHERE time = %s AND device_id = %s"
|
||||
).format(sql.Identifier(field))
|
||||
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, (value, time, device_id))
|
||||
|
||||
@staticmethod
|
||||
async def delete_scada_by_id_time(
|
||||
conn: AsyncConnection, device_id: str, start_time: datetime, end_time: datetime
|
||||
):
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(
|
||||
"DELETE FROM scada.scada_data WHERE device_id = %s AND time >= %s AND time <= %s",
|
||||
(device_id, start_time, end_time),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user