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.
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
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.analysis import AnalysisRepository
|
|
from app.auth.project_dependencies import get_project_pg_connection
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
async def get_database_connection(
|
|
conn: AsyncConnection = Depends(get_project_pg_connection),
|
|
):
|
|
"""获取数据库连接"""
|
|
yield conn
|
|
|
|
|
|
@router.get("/scada-info/database-view", summary="获取SCADA信息", description="使用连接池查询所有SCADA信息")
|
|
async def get_scada_info_with_connection(
|
|
conn: AsyncConnection = Depends(get_database_connection),
|
|
):
|
|
"""
|
|
获取所有SCADA信息
|
|
|
|
返回项目中所有的SCADA设备信息
|
|
"""
|
|
try:
|
|
scada_data = await ScadaInfoRepository.get_scadas(conn)
|
|
return {"success": True, "data": scada_data, "count": len(scada_data)}
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=500, detail=f"查询SCADA信息时发生错误: {str(e)}"
|
|
)
|
|
|
|
|
|
@router.get("/analysis/runs", summary="获取分析运行列表")
|
|
async def get_analysis_runs(
|
|
conn: AsyncConnection = Depends(get_database_connection),
|
|
):
|
|
"""
|
|
获取所有方案信息
|
|
|
|
返回项目中所有方案的详细信息
|
|
"""
|
|
try:
|
|
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)}")
|
|
|
|
|
|
@router.get("/analysis/runs/{run_id}", summary="获取分析运行")
|
|
async def get_analysis_run(
|
|
run_id: UUID,
|
|
conn: AsyncConnection = Depends(get_database_connection),
|
|
):
|
|
"""
|
|
获取所有爆管定位结果
|
|
|
|
返回项目中所有的爆管定位分析结果
|
|
"""
|
|
try:
|
|
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)}"
|
|
)
|
|
|
|
|
|
@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),
|
|
):
|
|
"""
|
|
根据爆管事件ID查询爆管定位结果
|
|
|
|
参数:
|
|
burst_incident: 爆管事件的唯一标识符
|
|
"""
|
|
try:
|
|
return await AnalysisRepository.list_results(conn, run_id, result_type)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail=f"查询分析结果时发生错误: {str(e)}",
|
|
)
|