refactor(db)!: finalize pooled WNDB v2 migration
This commit is contained in:
@@ -175,6 +175,53 @@ class AnalysisResultsRepository:
|
||||
await cur.execute(query, (run_id, link_id, start_time, end_time))
|
||||
return await cur.fetchall()
|
||||
|
||||
@staticmethod
|
||||
async def get_series_by_ids(
|
||||
conn: AsyncConnection,
|
||||
run_id: UUID,
|
||||
element_type: str,
|
||||
element_ids: list[str],
|
||||
start_time: datetime,
|
||||
end_time: datetime,
|
||||
field: str,
|
||||
) -> dict[str, list[dict[str, Any]]]:
|
||||
if element_type == "node":
|
||||
table_name, id_column, valid_fields = (
|
||||
"node_results", "node_id", AnalysisResultsRepository.NODE_FIELDS
|
||||
)
|
||||
elif element_type == "link":
|
||||
table_name, id_column, valid_fields = (
|
||||
"link_results", "link_id", AnalysisResultsRepository.LINK_FIELDS
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"invalid analysis element type: {element_type}")
|
||||
if field not in valid_fields:
|
||||
raise ValueError(f"invalid {element_type} result field: {field}")
|
||||
|
||||
result: dict[str, list[dict[str, Any]]] = {
|
||||
element_id: [] for element_id in element_ids
|
||||
}
|
||||
if not element_ids:
|
||||
return result
|
||||
query = sql.SQL(
|
||||
"SELECT {} AS element_id, time, {} AS value FROM analysis.{} "
|
||||
"WHERE run_id = %s AND {} = ANY(%s) AND time BETWEEN %s AND %s "
|
||||
"ORDER BY {}, time"
|
||||
).format(
|
||||
sql.Identifier(id_column),
|
||||
sql.Identifier(field),
|
||||
sql.Identifier(table_name),
|
||||
sql.Identifier(id_column),
|
||||
sql.Identifier(id_column),
|
||||
)
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, (run_id, element_ids, start_time, end_time))
|
||||
for row in await cur.fetchall():
|
||||
result.setdefault(str(row["element_id"]), []).append(
|
||||
{"time": row["time"], "value": row["value"]}
|
||||
)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
async def get_values_at_time(
|
||||
conn: AsyncConnection,
|
||||
|
||||
Reference in New Issue
Block a user