123 lines
3.7 KiB
Python
123 lines
3.7 KiB
Python
"""
|
|
Seismograph Dashboard API Router
|
|
Provides endpoints for the seismograph-specific dashboard
|
|
"""
|
|
|
|
from datetime import date
|
|
|
|
from fastapi import APIRouter, Request, Depends, Query
|
|
from fastapi.responses import HTMLResponse
|
|
from sqlalchemy.orm import Session
|
|
from backend.database import get_db
|
|
from backend.models import RosterUnit
|
|
from backend.templates_config import templates
|
|
|
|
router = APIRouter(prefix="/api/seismo-dashboard", tags=["seismo-dashboard"])
|
|
|
|
|
|
@router.get("/stats", response_class=HTMLResponse)
|
|
async def get_seismo_stats(request: Request, db: Session = Depends(get_db)):
|
|
"""
|
|
Returns HTML partial with seismograph statistics summary
|
|
"""
|
|
# Get all seismograph units
|
|
seismos = db.query(RosterUnit).filter_by(
|
|
device_type="seismograph",
|
|
retired=False
|
|
).all()
|
|
|
|
total = len(seismos)
|
|
deployed = sum(1 for s in seismos if s.deployed)
|
|
benched = sum(1 for s in seismos if not s.deployed and not s.out_for_calibration)
|
|
out_for_calibration = sum(1 for s in seismos if s.out_for_calibration)
|
|
|
|
# Count modems assigned to deployed seismographs
|
|
with_modem = sum(1 for s in seismos if s.deployed and s.deployed_with_modem_id)
|
|
without_modem = deployed - with_modem
|
|
|
|
return templates.TemplateResponse(
|
|
"partials/seismo_stats.html",
|
|
{
|
|
"request": request,
|
|
"total": total,
|
|
"deployed": deployed,
|
|
"benched": benched,
|
|
"out_for_calibration": out_for_calibration,
|
|
"with_modem": with_modem,
|
|
"without_modem": without_modem
|
|
}
|
|
)
|
|
|
|
|
|
@router.get("/units", response_class=HTMLResponse)
|
|
async def get_seismo_units(
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
search: str = Query(None),
|
|
sort: str = Query("id"),
|
|
order: str = Query("asc"),
|
|
status: str = Query(None),
|
|
modem: str = Query(None)
|
|
):
|
|
"""
|
|
Returns HTML partial with filterable and sortable seismograph unit list
|
|
"""
|
|
query = db.query(RosterUnit).filter_by(
|
|
device_type="seismograph",
|
|
retired=False
|
|
)
|
|
|
|
# Apply search filter
|
|
if search:
|
|
query = query.filter(
|
|
(RosterUnit.id.ilike(f"%{search}%")) |
|
|
(RosterUnit.note.ilike(f"%{search}%")) |
|
|
(RosterUnit.address.ilike(f"%{search}%"))
|
|
)
|
|
|
|
# Apply status filter
|
|
if status == "deployed":
|
|
query = query.filter(RosterUnit.deployed == True)
|
|
elif status == "benched":
|
|
query = query.filter(RosterUnit.deployed == False, RosterUnit.out_for_calibration == False)
|
|
elif status == "out_for_calibration":
|
|
query = query.filter(RosterUnit.out_for_calibration == True)
|
|
|
|
# Apply modem filter
|
|
if modem == "with":
|
|
query = query.filter(RosterUnit.deployed_with_modem_id.isnot(None))
|
|
elif modem == "without":
|
|
query = query.filter(RosterUnit.deployed_with_modem_id.is_(None))
|
|
|
|
# Apply sorting
|
|
sort_column_map = {
|
|
"id": RosterUnit.id,
|
|
"status": RosterUnit.deployed,
|
|
"modem": RosterUnit.deployed_with_modem_id,
|
|
"location": RosterUnit.address,
|
|
"last_calibrated": RosterUnit.last_calibrated,
|
|
"notes": RosterUnit.note
|
|
}
|
|
sort_column = sort_column_map.get(sort, RosterUnit.id)
|
|
|
|
if order == "desc":
|
|
query = query.order_by(sort_column.desc())
|
|
else:
|
|
query = query.order_by(sort_column.asc())
|
|
|
|
seismos = query.all()
|
|
|
|
return templates.TemplateResponse(
|
|
"partials/seismo_unit_list.html",
|
|
{
|
|
"request": request,
|
|
"units": seismos,
|
|
"search": search or "",
|
|
"sort": sort,
|
|
"order": order,
|
|
"status": status or "",
|
|
"modem": modem or "",
|
|
"today": date.today()
|
|
}
|
|
)
|