71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
"""Authenticated frontend routes for Monitoring Trends."""
|
|
|
|
from pathlib import Path
|
|
from typing import Annotated
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from fastapi.responses import HTMLResponse, Response
|
|
|
|
from app.frontend.views import render_app_page
|
|
from app.identity import require_permission, user_has_permission
|
|
from app.monitoring.models import (
|
|
MonitoringTrendQuery,
|
|
TrendRange,
|
|
TrendScopeType,
|
|
)
|
|
|
|
|
|
router = APIRouter(tags=["frontend"])
|
|
STATIC_ROOT = Path(__file__).parents[1] / "frontend" / "static"
|
|
MonitoringUser = Annotated[dict, Depends(require_permission("miners.view"))]
|
|
|
|
|
|
@router.get("/assets/minerops-monitoring-trends.css", include_in_schema=False)
|
|
def monitoring_trends_styles() -> Response:
|
|
return Response(
|
|
(STATIC_ROOT / "monitoring-trends.css").read_text(encoding="utf-8"),
|
|
media_type="text/css",
|
|
headers={"Cache-Control": "no-store"},
|
|
)
|
|
|
|
|
|
@router.get("/assets/minerops-monitoring-trends.js", include_in_schema=False)
|
|
def monitoring_trends_script() -> Response:
|
|
return Response(
|
|
(STATIC_ROOT / "monitoring-trends.js").read_text(encoding="utf-8"),
|
|
media_type="text/javascript",
|
|
headers={"Cache-Control": "no-store"},
|
|
)
|
|
|
|
|
|
@router.get(
|
|
"/monitoring/analytics",
|
|
response_class=HTMLResponse,
|
|
include_in_schema=False,
|
|
)
|
|
def monitoring_analytics_page(
|
|
current_user: MonitoringUser,
|
|
scope: Annotated[TrendScopeType, Query()] = "fleet",
|
|
scope_id: Annotated[UUID | None, Query()] = None,
|
|
time_range: Annotated[TrendRange, Query(alias="range")] = "24h",
|
|
) -> str:
|
|
try:
|
|
query = MonitoringTrendQuery(
|
|
scope=scope, scope_id=scope_id, range=time_range
|
|
)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
|
if query.scope in {"site", "room"} and not user_has_permission(
|
|
current_user, "locations.view"
|
|
):
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail="Location viewing permission is required for this scope",
|
|
)
|
|
return render_app_page(
|
|
"analytics",
|
|
current_user,
|
|
lambda permission: user_has_permission(current_user, permission),
|
|
)
|