feat: rich dashboard with severity bars, heatmap, most flagged, live poll
This commit is contained in:
@@ -4,7 +4,7 @@ import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy import Integer, cast, func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from guarddog_nexus.database import get_session
|
||||
@@ -27,6 +27,17 @@ def _render(name: str, **context) -> HTMLResponse:
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
async def dashboard(request: Request, session: AsyncSession = Depends(get_session)):
|
||||
ctx = await _dashboard_data(session)
|
||||
return _render("dashboard.html", **ctx, request=request)
|
||||
|
||||
|
||||
@router.get("/dashboard/stats", response_class=HTMLResponse)
|
||||
async def dashboard_stats_fragment(session: AsyncSession = Depends(get_session)):
|
||||
ctx = await _dashboard_data(session)
|
||||
return _render("dashboard_stats.html", **ctx)
|
||||
|
||||
|
||||
async def _dashboard_data(session: AsyncSession) -> dict:
|
||||
total_scans = await session.scalar(select(func.count(Scan.id)))
|
||||
flagged_scans = await session.scalar(select(func.count(Scan.id)).where(Scan.flagged == True))
|
||||
recent_flagged = await session.scalar(
|
||||
@@ -35,7 +46,29 @@ async def dashboard(request: Request, session: AsyncSession = Depends(get_sessio
|
||||
Scan.started_at >= func.datetime("now", "-7 days"),
|
||||
)
|
||||
)
|
||||
completed_scans = await session.scalar(
|
||||
select(func.count(Scan.id)).where(Scan.status == "completed")
|
||||
)
|
||||
failed_scans = await session.scalar(select(func.count(Scan.id)).where(Scan.status == "failed"))
|
||||
total_findings = await session.scalar(select(func.count(Finding.id)))
|
||||
|
||||
warnings_count = await session.scalar(
|
||||
select(func.count(Finding.id)).where(Finding.severity == "WARNING")
|
||||
)
|
||||
errors_count = await session.scalar(
|
||||
select(func.count(Finding.id)).where(Finding.severity == "ERROR")
|
||||
)
|
||||
|
||||
latest_flagged = (
|
||||
(
|
||||
await session.execute(
|
||||
select(Scan).where(Scan.flagged == True).order_by(Scan.started_at.desc()).limit(8)
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
|
||||
latest_scans = (
|
||||
(await session.execute(select(Scan).order_by(Scan.started_at.desc()).limit(10)))
|
||||
.scalars()
|
||||
@@ -51,17 +84,54 @@ async def dashboard(request: Request, session: AsyncSession = Depends(get_sessio
|
||||
)
|
||||
).all()
|
||||
|
||||
return _render(
|
||||
"dashboard.html",
|
||||
total_scans=total_scans,
|
||||
flagged_scans=flagged_scans,
|
||||
recent_flagged=recent_flagged,
|
||||
total_findings=total_findings,
|
||||
latest_scans=latest_scans,
|
||||
top_rules=[(r.rule, r.cnt) for r in top_rules],
|
||||
now=datetime.datetime.now(datetime.timezone.utc),
|
||||
request=request,
|
||||
)
|
||||
most_flagged = (
|
||||
await session.execute(
|
||||
select(
|
||||
Scan.package_name,
|
||||
Scan.package_version,
|
||||
func.sum(Scan.total_findings).label("total"),
|
||||
func.max(Scan.started_at).label("last_scan"),
|
||||
)
|
||||
.where(Scan.flagged == True)
|
||||
.group_by(Scan.package_name, Scan.package_version)
|
||||
.order_by(func.sum(Scan.total_findings).desc())
|
||||
.limit(8)
|
||||
)
|
||||
).all()
|
||||
|
||||
max_findings = max((r.total for r in most_flagged), default=1)
|
||||
|
||||
# Heatmap: scans per day for last 14 days
|
||||
days_raw = (
|
||||
await session.execute(
|
||||
select(
|
||||
func.date(Scan.started_at).label("day"),
|
||||
func.count(Scan.id).label("cnt"),
|
||||
func.sum(cast(Scan.flagged, Integer)).label("flagged_cnt"),
|
||||
)
|
||||
.where(Scan.started_at >= func.datetime("now", "-14 days"))
|
||||
.group_by("day")
|
||||
.order_by("day")
|
||||
)
|
||||
).all()
|
||||
|
||||
return {
|
||||
"total_scans": total_scans or 0,
|
||||
"flagged_scans": flagged_scans or 0,
|
||||
"recent_flagged": recent_flagged or 0,
|
||||
"completed_scans": completed_scans or 0,
|
||||
"failed_scans": failed_scans or 0,
|
||||
"total_findings": total_findings or 0,
|
||||
"warnings_count": warnings_count or 0,
|
||||
"errors_count": errors_count or 0,
|
||||
"latest_flagged": latest_flagged,
|
||||
"latest_scans": latest_scans,
|
||||
"top_rules": [(r.rule, r.cnt) for r in top_rules],
|
||||
"most_flagged": most_flagged,
|
||||
"max_findings": max_findings,
|
||||
"days": [(d.day, d.cnt, d.flagged_cnt) for d in days_raw],
|
||||
"now": datetime.datetime.now(datetime.timezone.utc),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/scans", response_class=HTMLResponse)
|
||||
|
||||
Reference in New Issue
Block a user