完善区域漏损识别
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import ast
|
||||
import json
|
||||
from datetime import date
|
||||
|
||||
import geopandas as gpd
|
||||
import pandas as pd
|
||||
@@ -170,6 +171,198 @@ def query_scheme_list(name: str) -> list:
|
||||
print(f"查询错误:{e}")
|
||||
|
||||
|
||||
def ensure_leakage_identify_result_table(name: str) -> None:
|
||||
conn_string = get_pgconn_string(db_name=name)
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS public.leakage_identify_result (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
scheme_name VARCHAR(255) NOT NULL,
|
||||
network VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
run_status VARCHAR(32) NOT NULL DEFAULT 'completed',
|
||||
error_message TEXT,
|
||||
sensor_nodes JSONB NOT NULL DEFAULT '[]'::jsonb,
|
||||
result_rows JSONB NOT NULL DEFAULT '[]'::jsonb,
|
||||
node_area_map JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
areas JSONB NOT NULL DEFAULT '[]'::jsonb,
|
||||
drawing_payload JSONB NOT NULL DEFAULT '{"type":"FeatureCollection","features":[]}'::jsonb,
|
||||
CONSTRAINT uq_leakage_identify_result_scheme UNIQUE (scheme_name),
|
||||
CONSTRAINT fk_leakage_identify_result_scheme
|
||||
FOREIGN KEY (scheme_name)
|
||||
REFERENCES public.scheme_list (scheme_name)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
"""
|
||||
)
|
||||
cur.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_leakage_identify_result_network ON public.leakage_identify_result (network);"
|
||||
)
|
||||
cur.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_leakage_identify_result_created_at ON public.leakage_identify_result (created_at DESC);"
|
||||
)
|
||||
cur.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_leakage_identify_result_run_status ON public.leakage_identify_result (run_status);"
|
||||
)
|
||||
cur.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_leakage_identify_result_rows_gin ON public.leakage_identify_result USING GIN (result_rows);"
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def store_leakage_identify_result(
|
||||
name: str,
|
||||
scheme_name: str,
|
||||
network: str,
|
||||
sensor_nodes: list[str],
|
||||
result_rows: list[dict],
|
||||
node_area_map: dict[str, str],
|
||||
areas: list[dict],
|
||||
drawing_payload: dict,
|
||||
run_status: str = "completed",
|
||||
error_message: str | None = None,
|
||||
) -> None:
|
||||
ensure_leakage_identify_result_table(name)
|
||||
conn_string = get_pgconn_string(db_name=name)
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO public.leakage_identify_result
|
||||
(
|
||||
scheme_name, network, run_status, error_message,
|
||||
sensor_nodes, result_rows, node_area_map, areas, drawing_payload
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s::jsonb, %s::jsonb, %s::jsonb, %s::jsonb, %s::jsonb)
|
||||
ON CONFLICT (scheme_name)
|
||||
DO UPDATE SET
|
||||
network = EXCLUDED.network,
|
||||
run_status = EXCLUDED.run_status,
|
||||
error_message = EXCLUDED.error_message,
|
||||
sensor_nodes = EXCLUDED.sensor_nodes,
|
||||
result_rows = EXCLUDED.result_rows,
|
||||
node_area_map = EXCLUDED.node_area_map,
|
||||
areas = EXCLUDED.areas,
|
||||
drawing_payload = EXCLUDED.drawing_payload,
|
||||
created_at = NOW();
|
||||
""",
|
||||
(
|
||||
scheme_name,
|
||||
network,
|
||||
run_status,
|
||||
error_message,
|
||||
json.dumps(sensor_nodes),
|
||||
json.dumps(result_rows),
|
||||
json.dumps(node_area_map),
|
||||
json.dumps(areas),
|
||||
json.dumps(drawing_payload),
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def query_leakage_identify_schemes(
|
||||
name: str,
|
||||
network: str,
|
||||
scheme_type: str = "dma_leak_identification",
|
||||
query_date: date | None = None,
|
||||
) -> list[dict]:
|
||||
conn_string = get_pgconn_string(db_name=name)
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
if query_date is None:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT scheme_id, scheme_name, scheme_type, username, create_time, scheme_start_time, scheme_detail
|
||||
FROM public.scheme_list
|
||||
WHERE scheme_type = %s
|
||||
ORDER BY create_time DESC
|
||||
""",
|
||||
(scheme_type,),
|
||||
)
|
||||
else:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT scheme_id, scheme_name, scheme_type, username, create_time, scheme_start_time, scheme_detail
|
||||
FROM public.scheme_list
|
||||
WHERE scheme_type = %s AND DATE(create_time) = %s
|
||||
ORDER BY create_time DESC
|
||||
""",
|
||||
(scheme_type, query_date),
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
result = []
|
||||
for row in rows:
|
||||
detail = row[6] if isinstance(row[6], dict) else {}
|
||||
if network and detail.get("network") not in (None, network):
|
||||
continue
|
||||
result.append(
|
||||
{
|
||||
"scheme_id": row[0],
|
||||
"scheme_name": row[1],
|
||||
"scheme_type": row[2],
|
||||
"username": row[3],
|
||||
"create_time": row[4],
|
||||
"scheme_start_time": row[5],
|
||||
"scheme_detail": detail,
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def query_leakage_identify_scheme_detail(name: str, scheme_name: str) -> dict:
|
||||
ensure_leakage_identify_result_table(name)
|
||||
conn_string = get_pgconn_string(db_name=name)
|
||||
with psycopg.connect(conn_string) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT scheme_id, scheme_name, scheme_type, username, create_time, scheme_start_time, scheme_detail
|
||||
FROM public.scheme_list
|
||||
WHERE scheme_name = %s
|
||||
LIMIT 1
|
||||
""",
|
||||
(scheme_name,),
|
||||
)
|
||||
base_row = cur.fetchone()
|
||||
if base_row is None:
|
||||
return {}
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT network, created_at, run_status, error_message, sensor_nodes, result_rows, node_area_map, areas, drawing_payload
|
||||
FROM public.leakage_identify_result
|
||||
WHERE scheme_name = %s
|
||||
LIMIT 1
|
||||
""",
|
||||
(scheme_name,),
|
||||
)
|
||||
result_row = cur.fetchone()
|
||||
if result_row is None:
|
||||
return {}
|
||||
return {
|
||||
"scheme_id": base_row[0],
|
||||
"scheme_name": base_row[1],
|
||||
"scheme_type": base_row[2],
|
||||
"username": base_row[3],
|
||||
"create_time": base_row[4],
|
||||
"scheme_start_time": base_row[5],
|
||||
"scheme_detail": base_row[6] if isinstance(base_row[6], dict) else {},
|
||||
"network": result_row[0],
|
||||
"result_created_at": result_row[1],
|
||||
"run_status": result_row[2],
|
||||
"error_message": result_row[3],
|
||||
"sensor_nodes": result_row[4] if isinstance(result_row[4], list) else [],
|
||||
"rows": result_row[5] if isinstance(result_row[5], list) else [],
|
||||
"node_area_map": result_row[6] if isinstance(result_row[6], dict) else {},
|
||||
"areas": result_row[7] if isinstance(result_row[7], list) else [],
|
||||
"drawing_payload": (
|
||||
result_row[8] if isinstance(result_row[8], dict) else {"type": "FeatureCollection", "features": []}
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
# 2025/03/23
|
||||
def upload_shp_to_pg(name: str, table_name: str, role: str, shp_file_path: str):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user