feat: фаза 3 — i18n RU/EN, /metrics, AI disclaimer, initiator+IP, LLM очередь
3.3 i18n: модуль с RU/EN словарями, LangMiddleware (cookie+query),
Jinja-фильтр t(), переключатель EN/RU в nav, перевод ключевых
строк интерфейса
3.5 /metrics: Prometheus-совместимый endpoint (scans_total,
scans_flagged, findings_total, by_status, by_ecosystem,
last_scan_timestamp)
3.2 AI disclaimer: сноска под каждым LLM-вердиктом (.llm-disclaimer)
3.4 LLM очередь: asyncio.Semaphore(LLM_MAX_CONCURRENT_ANALYSES)
3.1 initiator + source_ip: поля в Scan, захват из webhook payload,
показ в scan_detail + API
3.6 UI: убран stat-minibar и heatmap с дашборда
This commit is contained in:
80
guarddog_nexus/routes/metrics.py
Normal file
80
guarddog_nexus/routes/metrics.py
Normal file
@@ -0,0 +1,80 @@
|
||||
"""Prometheus-compatible metrics endpoint."""
|
||||
|
||||
import time
|
||||
|
||||
from fastapi import APIRouter, Depends, Response
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from ..db.engine import get_session
|
||||
from ..db.models import Finding, Scan
|
||||
|
||||
router = APIRouter(tags=["metrics"])
|
||||
|
||||
|
||||
@router.get("/metrics")
|
||||
async def metrics(session: AsyncSession = Depends(get_session)):
|
||||
total = await session.scalar(select(func.count(Scan.id))) or 0
|
||||
flagged = await session.scalar(
|
||||
select(func.count(Scan.id)).where(Scan.flagged == True)
|
||||
) or 0
|
||||
findings_total = await session.scalar(select(func.count(Finding.id))) or 0
|
||||
|
||||
# By status
|
||||
status_rows = (
|
||||
await session.execute(
|
||||
select(Scan.status, func.count(Scan.id)).group_by(Scan.status)
|
||||
)
|
||||
).all()
|
||||
by_status = {row[0]: row[1] for row in status_rows}
|
||||
|
||||
# By ecosystem
|
||||
eco_rows = (
|
||||
await session.execute(
|
||||
select(Scan.ecosystem, func.count(Scan.id)).group_by(Scan.ecosystem)
|
||||
)
|
||||
).all()
|
||||
by_eco = {row[0]: row[1] for row in eco_rows}
|
||||
|
||||
# Latest scan timestamp
|
||||
latest = await session.scalar(
|
||||
select(func.max(Scan.started_at))
|
||||
)
|
||||
|
||||
lines = [
|
||||
"# HELP guarddog_scans_total Total number of package scans.",
|
||||
"# TYPE guarddog_scans_total counter",
|
||||
f"guarddog_scans_total {total}",
|
||||
"",
|
||||
"# HELP guarddog_scans_flagged_total Total flagged (vulnerable) scans.",
|
||||
"# TYPE guarddog_scans_flagged_total counter",
|
||||
f"guarddog_scans_flagged_total {flagged}",
|
||||
"",
|
||||
"# HELP guarddog_findings_total Total security findings.",
|
||||
"# TYPE guarddog_findings_total counter",
|
||||
f"guarddog_findings_total {findings_total}",
|
||||
"",
|
||||
"# HELP guarddog_scans_by_status Scans grouped by status.",
|
||||
"# TYPE guarddog_scans_by_status gauge",
|
||||
]
|
||||
for status, count in sorted(by_status.items()):
|
||||
lines.append(f'guarddog_scans_by_status{{status="{status}"}} {count}')
|
||||
|
||||
lines += [
|
||||
"",
|
||||
"# HELP guarddog_scans_by_ecosystem Scans grouped by ecosystem.",
|
||||
"# TYPE guarddog_scans_by_ecosystem gauge",
|
||||
]
|
||||
for eco, count in sorted(by_eco.items()):
|
||||
lines.append(f'guarddog_scans_by_ecosystem{{ecosystem="{eco}"}} {count}')
|
||||
|
||||
if latest:
|
||||
ts = time.mktime(latest.timetuple())
|
||||
lines += [
|
||||
"",
|
||||
"# HELP guarddog_last_scan_timestamp_seconds Unix timestamp of most recent scan.",
|
||||
"# TYPE guarddog_last_scan_timestamp_seconds gauge",
|
||||
f"guarddog_last_scan_timestamp_seconds {ts:.0f}",
|
||||
]
|
||||
|
||||
return Response(content="\n".join(lines) + "\n", media_type="text/plain")
|
||||
@@ -22,6 +22,7 @@ from ..db.queries import (
|
||||
build_scan_list_query,
|
||||
get_dashboard_stats,
|
||||
)
|
||||
from ..i18n import t as _t
|
||||
|
||||
router = APIRouter(tags=["web"])
|
||||
|
||||
@@ -29,6 +30,7 @@ _jinja_env = Environment(
|
||||
loader=PackageLoader(APP_PACKAGE, "web/templates"),
|
||||
autoescape=select_autoescape(),
|
||||
)
|
||||
_jinja_env.globals["t"] = _t
|
||||
|
||||
|
||||
def _render(name: str, **context) -> HTMLResponse:
|
||||
|
||||
Reference in New Issue
Block a user