updated dash

This commit is contained in:
2026-05-17 14:12:06 +00:00
parent 317a7f3f13
commit 72c71bb95d
14 changed files with 369 additions and 93 deletions
+20 -5
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"sync"
"time"
"crowdsec-dashy/internal/crowdsec"
@@ -20,7 +21,8 @@ func NewAPIHandler(deps Deps) *APIHandler {
type statsResponse struct {
Decisions int `json:"decisions"`
Alerts int `json:"alerts"`
Alerts24h int `json:"alerts_24h"`
Alerts7d int `json:"alerts_7d"`
Bouncers int `json:"bouncers"`
Machines int `json:"machines"`
Healthy bool `json:"healthy"`
@@ -36,18 +38,31 @@ func (h *APIHandler) Stats(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
decisions, _ := h.deps.LAPI.ListDecisions(ctx, crowdsec.DecisionFilter{Limit: 500})
alerts, _ := h.deps.LAPI.ListAlerts(ctx, crowdsec.AlertFilter{Limit: 500})
// Fetch 24h and 7d alert counts in parallel to minimize latency.
var alerts24h, alerts7d []crowdsec.Alert
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
alerts24h, _ = h.deps.LAPI.ListAlerts(ctx, crowdsec.AlertFilter{Limit: 5000, Since: "24h"})
}()
go func() {
defer wg.Done()
alerts7d, _ = h.deps.LAPI.ListAlerts(ctx, crowdsec.AlertFilter{Limit: 5000, Since: "168h"})
}()
wg.Wait()
resp := statsResponse{
Decisions: len(decisions),
Alerts: len(alerts),
Alerts24h: len(alerts24h),
Alerts7d: len(alerts7d),
Healthy: h.deps.LAPI.IsHealthy(ctx),
}
if h.deps.CLIAvailable {
decisions, _ := h.deps.CLI.ListDecisions(ctx, crowdsec.DecisionFilter{Limit: 500})
bouncers, _ := h.deps.CLI.ListBouncers(ctx)
machines, _ := h.deps.CLI.ListMachines(ctx)
resp.Decisions = len(decisions)
resp.Bouncers = len(bouncers)
resp.Machines = len(machines)
}