feat: add monitoring trends
This commit is contained in:
1 parent
19fc29c72c
commit
4d360fac42
18 files changed
+1683
No files matched your search
@@ -0,0 +1,70 @@
|
||||
"""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),
|
||||
)
|
||||
Reference in new issue
Block a user