feat add connections management and health summary improvements
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from ...core.database import get_db
|
||||
from ...core.database import get_db, ensure_connection_configs_table
|
||||
from ...models.connection import ConnectionConfig
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
@@ -18,6 +18,16 @@ from ...core.logger import logger, belief_scope
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
# [DEF:_ensure_connections_schema:Function]
|
||||
# @PURPOSE: Ensures the connection_configs table exists before CRUD access.
|
||||
# @PRE: db is an active SQLAlchemy session.
|
||||
# @POST: The current bind can safely query ConnectionConfig.
|
||||
def _ensure_connections_schema(db: Session):
|
||||
with belief_scope("ConnectionsRouter.ensure_schema"):
|
||||
ensure_connection_configs_table(db.get_bind())
|
||||
# [/DEF:_ensure_connections_schema:Function]
|
||||
|
||||
# [DEF:ConnectionSchema:Class]
|
||||
# @PURPOSE: Pydantic model for connection response.
|
||||
class ConnectionSchema(BaseModel):
|
||||
@@ -55,6 +65,7 @@ class ConnectionCreate(BaseModel):
|
||||
@router.get("", response_model=List[ConnectionSchema])
|
||||
async def list_connections(db: Session = Depends(get_db)):
|
||||
with belief_scope("ConnectionsRouter.list_connections"):
|
||||
_ensure_connections_schema(db)
|
||||
connections = db.query(ConnectionConfig).all()
|
||||
return connections
|
||||
# [/DEF:list_connections:Function]
|
||||
@@ -69,6 +80,7 @@ async def list_connections(db: Session = Depends(get_db)):
|
||||
@router.post("", response_model=ConnectionSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def create_connection(connection: ConnectionCreate, db: Session = Depends(get_db)):
|
||||
with belief_scope("ConnectionsRouter.create_connection", f"name={connection.name}"):
|
||||
_ensure_connections_schema(db)
|
||||
db_connection = ConnectionConfig(**connection.dict())
|
||||
db.add(db_connection)
|
||||
db.commit()
|
||||
@@ -87,6 +99,7 @@ async def create_connection(connection: ConnectionCreate, db: Session = Depends(
|
||||
@router.delete("/{connection_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_connection(connection_id: str, db: Session = Depends(get_db)):
|
||||
with belief_scope("ConnectionsRouter.delete_connection", f"id={connection_id}"):
|
||||
_ensure_connections_schema(db)
|
||||
db_connection = db.query(ConnectionConfig).filter(ConnectionConfig.id == connection_id).first()
|
||||
if not db_connection:
|
||||
logger.error(f"[ConnectionsRouter.delete_connection][State] Connection {connection_id} not found")
|
||||
@@ -97,4 +110,4 @@ async def delete_connection(connection_id: str, db: Session = Depends(get_db)):
|
||||
return
|
||||
# [/DEF:delete_connection:Function]
|
||||
|
||||
# [/DEF:ConnectionsRouter:Module]
|
||||
# [/DEF:ConnectionsRouter:Module]
|
||||
|
||||
Reference in New Issue
Block a user