refactor(db)!: adopt project-routed pooled databases

Reorganize WNDB by responsibility and remove legacy scheme endpoints.\n\nRoute analysis and time-series access through project pools, preserve transactional realtime replacement, and refresh GIS materialized views after writes.\n\nAdd database architecture documentation, live pooling coverage, API contract updates, and executable container verification.\n\nBREAKING CHANGE: legacy scheme APIs and flat app.native.wndb module imports are removed.
This commit is contained in:
2026-08-25 18:35:05 +08:00
parent fdbcc5c033
commit fa188af0b1
181 changed files with 8446 additions and 33546 deletions
+25 -19
View File
@@ -1,8 +1,10 @@
from fastapi import APIRouter, Depends, HTTPException, Path, Query
from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, Query
from psycopg import AsyncConnection
from app.infra.db.postgresql.scada import ScadaInfoRepository
from app.infra.db.postgresql.scheme import SchemeRepository
from app.infra.db.postgresql.analysis import AnalysisRepository
from app.auth.project_dependencies import get_project_pg_connection
router = APIRouter()
@@ -33,8 +35,8 @@ async def get_scada_info_with_connection(
)
@router.get("/schemes/list-with-connection", summary="获取方案列表", description="使用连接池查询所有方案信息")
async def get_scheme_list_with_connection(
@router.get("/analysis/runs", summary="获取分析运行列表")
async def get_analysis_runs(
conn: AsyncConnection = Depends(get_database_connection),
):
"""
@@ -43,14 +45,15 @@ async def get_scheme_list_with_connection(
返回项目中所有方案的详细信息
"""
try:
scheme_data = await SchemeRepository.get_schemes(conn)
return {"success": True, "data": scheme_data, "count": len(scheme_data)}
runs = await AnalysisRepository.list_runs(conn)
return {"success": True, "data": runs, "count": len(runs)}
except Exception as e:
raise HTTPException(status_code=500, detail=f"查询方案信息时发生错误: {str(e)}")
raise HTTPException(status_code=500, detail=f"查询分析运行时发生错误: {str(e)}")
@router.get("/burst-locations/database-view", summary="获取爆管定位结果", description="使用连接池查询所有爆管定位结果")
async def get_burst_locate_result_with_connection(
@router.get("/analysis/runs/{run_id}", summary="获取分析运行")
async def get_analysis_run(
run_id: UUID,
conn: AsyncConnection = Depends(get_database_connection),
):
"""
@@ -59,17 +62,22 @@ async def get_burst_locate_result_with_connection(
返回项目中所有的爆管定位分析结果
"""
try:
burst_data = await SchemeRepository.get_burst_locate_results(conn)
return {"success": True, "data": burst_data, "count": len(burst_data)}
run = await AnalysisRepository.get_run(conn, run_id)
if run is None:
raise HTTPException(status_code=404, detail="分析运行不存在")
return run
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=500, detail=f"查询爆管定位结果时发生错误: {str(e)}"
status_code=500, detail=f"查询分析运行时发生错误: {str(e)}"
)
@router.get("/burst-locations/{burst_incident}", summary="按事件查询爆管定位结果", description="根据爆管事件ID查询对应的爆管定位结果")
async def get_burst_locate_result_by_incident(
burst_incident: str = Path(..., description="爆管事件ID"),
@router.get("/analysis/runs/{run_id}/results", summary="获取分析结果")
async def get_analysis_results(
run_id: UUID,
result_type: str | None = Query(default=None, description="结果类型"),
conn: AsyncConnection = Depends(get_database_connection),
):
"""
@@ -79,11 +87,9 @@ async def get_burst_locate_result_by_incident(
burst_incident: 爆管事件的唯一标识符
"""
try:
return await SchemeRepository.get_burst_locate_result_by_incident(
conn, burst_incident
)
return await AnalysisRepository.list_results(conn, run_id, result_type)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"根据 burst_incident 查询爆管定位结果时发生错误: {str(e)}",
detail=f"查询分析结果时发生错误: {str(e)}",
)