Files
urutau/internal/server/api_analytics.go
T
Lucas RayanandClaude Opus 5.5 2ebda1e4d7
Release / Plan (reuse the published image?) (push) Successful in 3s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 20s
CI / Tests (Linux, race detector) (push) Successful in 1m10s
Release / ci (push) Successful in 1m10s
Release / Build image (arm64) (push) Successful in 55s
Release / Build image (amd64) (push) Successful in 1m16s
Release / Publish tags (push) Successful in 14s
Release / Gitea release (push) Skipped
feat: 1.3.0 thirteen new pages, charts, tooltips and error analysis
New pages grouped in the rail (Monitor, Investigate, Analyze, Operate,
Reports): Services (SLO, Apdex, latency app vs proxy), Sessions (stage
funnel), Campaigns, Event search, MITRE ATT&CK matrix, Evolution,
Concentration (Pareto, Gini), Survival (Kaplan-Meier per attack
episode, recidivists), Where errors come from (application vs failed
attack vs proxy vs client), Protocols and TLS, Defense (fail2ban, SSH,
blocklist), Integrity, and a printable executive report.

Chart library (donut, radar, stacked area, bars, heatmap, gauge),
glossary with accessible "?" tooltips, self-explanatory category names
(nonexistent page 404), page loader through pages.json.

Unique visitors fixed: real browsers opening a page, one per IP per day,
deduplicated across domains, with non-human clients counted apart.
Anonymous legitimate visits on the Live page (?visits=1, no IP).
Traefik origin, service, protocol and TLS fields parsed. Paths and
Referer paths drop e-mail and token segments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 22:39:33 -03:00

72 lines
1.9 KiB
Go

package server
import (
"net/http"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
// Temporal analytics: GET /api/v1/evolution, /concentration and /survival.
// They accept range|from&to and host, study the event registry within the
// study budget and deadline (cached 30 s) and share the user-side study slots.
func init() {
registerUserRoute("GET /api/v1/evolution", func(s *Server) http.HandlerFunc { return s.analytics("evolution") })
registerUserRoute("GET /api/v1/concentration", func(s *Server) http.HandlerFunc { return s.analytics("concentration") })
registerUserRoute("GET /api/v1/survival", func(s *Server) http.HandlerFunc { return s.analytics("survival") })
}
func (s *Server) analytics(kind string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if q.Get("range") == "" && q.Get("from") == "" {
if kind == "evolution" {
q.Set("range", "30d")
} else {
q.Set("range", "7d")
}
r.URL.RawQuery = q.Encode()
}
rg, ok := s.rangeOf(w, r)
if !ok {
return
}
host := q.Get("host")
if len(host) > 64 {
badRequest(w, "invalid host")
return
}
if host == "all" {
host = ""
}
if host != "" && host != s.opt.Store.Name() && (s.opt.Hub == nil || !s.opt.Hub.Has(host)) {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "unknown host"})
return
}
dim := q.Get("dim")
if kind == "concentration" {
if dim == "" {
dim = "ip"
}
if !store.ConcentrationDims[dim] {
badRequest(w, "dim must be ip, asn, country, path or domain")
return
}
}
if !s.userSlot(w) {
return
}
defer func() { <-s.user }()
from, to := rg.Bounds(s.opt.Now().Unix())
st := s.opt.Store
switch kind {
case "evolution":
writeJSON(w, 200, st.Evolution(from, to, host))
case "concentration":
writeJSON(w, 200, st.Concentration(from, to, host, dim))
default:
writeJSON(w, 200, st.Survival(from, to, host))
}
}
}