SLM dashboard rework, diagnostics and command pages added

This commit is contained in:
serversdwn
2026-01-12 04:42:08 +00:00
parent ee025f1f34
commit e1b965c24c
4 changed files with 513 additions and 155 deletions

View File

@@ -6,7 +6,7 @@ Provides API endpoints for the Sound Level Meters dashboard page.
from fastapi import APIRouter, Request, Depends, Query
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi.responses import HTMLResponse, JSONResponse
from sqlalchemy.orm import Session
from sqlalchemy import func
from datetime import datetime, timedelta
@@ -60,14 +60,20 @@ async def get_slm_stats(request: Request, db: Session = Depends(get_db)):
async def get_slm_units(
request: Request,
db: Session = Depends(get_db),
search: str = Query(None)
search: str = Query(None),
project: str = Query(None)
):
"""
Get list of SLM units for the sidebar.
Returns HTML partial with unit cards.
Supports filtering by search term and project.
"""
query = db.query(RosterUnit).filter_by(device_type="sound_level_meter")
# Filter by project if provided
if project:
query = query.filter(RosterUnit.project_id == project)
# Filter by search term if provided
if search:
search_term = f"%{search}%"
@@ -326,3 +332,55 @@ async def test_modem_connection(modem_id: str, db: Session = Depends(get_db)):
"modem_id": modem_id,
"detail": str(e)
}
@router.get("/diagnostics/{unit_id}", response_class=HTMLResponse)
async def get_diagnostics(request: Request, unit_id: str, db: Session = Depends(get_db)):
"""
Get compact diagnostics card for a specific SLM unit.
Returns HTML partial with key metrics only.
"""
unit = db.query(RosterUnit).filter_by(id=unit_id, device_type="sound_level_meter").first()
if not unit:
return HTMLResponse(
content='<div class="p-6 text-center text-red-600">Unit not found</div>',
status_code=404
)
# Get modem info
modem = None
modem_ip = None
if unit.deployed_with_modem_id:
modem = db.query(RosterUnit).filter_by(id=unit.deployed_with_modem_id, device_type="modem").first()
if modem:
# Try modem_rx_host first (if it exists), then fall back to ip_address
modem_ip = getattr(modem, 'modem_rx_host', None) or modem.ip_address
elif unit.slm_host:
modem_ip = unit.slm_host
return templates.TemplateResponse("partials/slm_diagnostics_card.html", {
"request": request,
"unit": unit,
"modem": modem,
"modem_ip": modem_ip
})
@router.get("/projects")
async def get_projects(db: Session = Depends(get_db)):
"""
Get list of unique projects from deployed SLMs.
Returns JSON array of project names.
"""
projects = db.query(RosterUnit.project_id).filter(
RosterUnit.device_type == "sound_level_meter",
RosterUnit.deployed == True,
RosterUnit.retired == False,
RosterUnit.project_id.isnot(None)
).distinct().order_by(RosterUnit.project_id).all()
# Extract project names from query result tuples
project_list = [p[0] for p in projects if p[0]]
return JSONResponse(content={"projects": project_list})