106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from typing import List, Any, Dict
|
|
from datetime import datetime
|
|
from psycopg import AsyncConnection
|
|
|
|
from .database import get_db_connection
|
|
from .schemas.realtime import RealtimeRepository
|
|
from .schemas.scheme import SchemeRepository
|
|
from .schemas.scada import ScadaRepository
|
|
|
|
router = APIRouter(prefix="/timescaledb", tags=["TimescaleDB"])
|
|
|
|
# --- Realtime Endpoints ---
|
|
|
|
@router.post("/realtime/links/batch", status_code=201)
|
|
async def insert_realtime_links(
|
|
data: List[dict],
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
await RealtimeRepository.insert_links_batch(conn, data)
|
|
return {"message": f"Inserted {len(data)} records"}
|
|
|
|
@router.get("/realtime/links")
|
|
async def get_realtime_links(
|
|
start_time: datetime,
|
|
end_time: datetime,
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
return await RealtimeRepository.get_links_by_time(conn, start_time, end_time)
|
|
|
|
@router.patch("/realtime/links/{link_id}/field")
|
|
async def update_realtime_link_field(
|
|
link_id: str,
|
|
time: datetime,
|
|
field: str,
|
|
value: float, # Assuming float for now, could be Any but FastAPI needs type
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
try:
|
|
await RealtimeRepository.update_link_field(conn, time, link_id, field, value)
|
|
return {"message": "Updated successfully"}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
@router.post("/realtime/nodes/batch", status_code=201)
|
|
async def insert_realtime_nodes(
|
|
data: List[dict],
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
await RealtimeRepository.insert_nodes_batch(conn, data)
|
|
return {"message": f"Inserted {len(data)} records"}
|
|
|
|
@router.get("/realtime/nodes")
|
|
async def get_realtime_nodes(
|
|
start_time: datetime,
|
|
end_time: datetime,
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
return await RealtimeRepository.get_nodes_by_time(conn, start_time, end_time)
|
|
|
|
# --- Scheme Endpoints ---
|
|
|
|
@router.post("/scheme/links/batch", status_code=201)
|
|
async def insert_scheme_links(
|
|
data: List[dict],
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
await SchemeRepository.insert_links_batch(conn, data)
|
|
return {"message": f"Inserted {len(data)} records"}
|
|
|
|
@router.get("/scheme/links")
|
|
async def get_scheme_links(
|
|
scheme: str,
|
|
start_time: datetime,
|
|
end_time: datetime,
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
return await SchemeRepository.get_links_by_scheme_and_time(conn, scheme, start_time, end_time)
|
|
|
|
@router.post("/scheme/nodes/batch", status_code=201)
|
|
async def insert_scheme_nodes(
|
|
data: List[dict],
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
await SchemeRepository.insert_nodes_batch(conn, data)
|
|
return {"message": f"Inserted {len(data)} records"}
|
|
|
|
# --- SCADA Endpoints ---
|
|
|
|
@router.post("/scada/batch", status_code=201)
|
|
async def insert_scada_data(
|
|
data: List[dict],
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
await ScadaRepository.insert_batch(conn, data)
|
|
return {"message": f"Inserted {len(data)} records"}
|
|
|
|
@router.get("/scada")
|
|
async def get_scada_data(
|
|
device_id: str,
|
|
start_time: datetime,
|
|
end_time: datetime,
|
|
conn: AsyncConnection = Depends(get_db_connection)
|
|
):
|
|
return await ScadaRepository.get_data_by_time(conn, device_id, start_time, end_time)
|