refactor(db)!: clean up business SQL access
- make realtime replacement and analysis result writes transactional\n- consolidate SCADA repositories and remove process-global project state\n- validate SCADA batches and use indexed GIS-backed business queries\n\nBREAKING CHANGE: remove the public analysis result writer and the pipeline-health network_name query parameter.
This commit is contained in:
@@ -88,16 +88,15 @@ class InternalQueries:
|
||||
rows = ScadaRepository.get_scada_by_ids_time_range_sync(
|
||||
conn, device_ids, start_time, end_time
|
||||
)
|
||||
# 处理结果,返回每个 device_id 的第一个值
|
||||
result = {}
|
||||
for device_id in device_ids:
|
||||
device_rows = [
|
||||
row for row in rows if row["device_id"] == device_id
|
||||
]
|
||||
if device_rows:
|
||||
result[device_id] = device_rows[0]["monitored_value"]
|
||||
else:
|
||||
result[device_id] = None
|
||||
# Rows are ordered by device/time; retain the first sample
|
||||
# for each requested device in one pass.
|
||||
result = {device_id: None for device_id in device_ids}
|
||||
seen: set[str] = set()
|
||||
for row in rows:
|
||||
device_id = str(row["device_id"])
|
||||
if device_id in result and device_id not in seen:
|
||||
result[device_id] = row["monitored_value"]
|
||||
seen.add(device_id)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"查询尝试 {attempt + 1} 失败: {e}")
|
||||
@@ -135,8 +134,6 @@ class InternalQueries:
|
||||
result.setdefault(device_id, []).append(
|
||||
{"time": row["time"].isoformat(), "value": value}
|
||||
)
|
||||
for device_id in result:
|
||||
result[device_id].sort(key=lambda item: item["time"])
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"查询尝试 {attempt + 1} 失败: {e}")
|
||||
@@ -307,16 +304,7 @@ class InternalQueries:
|
||||
def _resolve_simulation_table(element_type: str) -> tuple[str, str, set[str]]:
|
||||
normalized_type = element_type.lower()
|
||||
if normalized_type == "node":
|
||||
return "node_results", "node_id", {"actual_demand", "total_head", "pressure", "quality"}
|
||||
return "node_results", "node_id", set(RealtimeRepository.NODE_FIELDS)
|
||||
if normalized_type == "link":
|
||||
return "link_results", "link_id", {
|
||||
"flow",
|
||||
"friction",
|
||||
"headloss",
|
||||
"quality",
|
||||
"reaction",
|
||||
"setting",
|
||||
"status",
|
||||
"velocity",
|
||||
}
|
||||
return "link_results", "link_id", set(RealtimeRepository.LINK_FIELDS)
|
||||
raise ValueError(f"Unsupported element_type: {element_type}")
|
||||
|
||||
Reference in New Issue
Block a user