Files
urutau/internal/server/api_services.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

209 lines
5.7 KiB
Go

package server
import (
"math"
"net/http"
"sort"
"strings"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
const sloTarget = 0.995
func init() {
registerUserRoute("GET /api/v1/services", func(s *Server) http.HandlerFunc { return s.servicesAPI })
registerUserRoute("GET /api/v1/services/{svc}", func(s *Server) http.HandlerFunc { return s.serviceAPI })
registerUserRoute("GET /api/v1/protocols", func(s *Server) http.HandlerFunc { return s.protocolsAPI })
}
type tri struct {
Total float64 `json:"total"`
Origin float64 `json:"origin"`
Overhead float64 `json:"overhead"`
}
func errRate(h store.SvcHourOut) float64 {
if h.Legit == 0 {
return 0
}
return float64(h.Cls[store.ClsApp]+h.Cls[store.ClsProxy]) / float64(h.Legit)
}
func round(f float64, d int) float64 {
p := math.Pow(10, float64(d))
return math.Round(f*p) / p
}
func serviceItem(v store.SvcView, to int64) map[string]any {
t := v.Tot
rate := errRate(t)
var peak uint32
for _, h := range v.Hours {
if h.Req > peak {
peak = h.Req
}
}
burn := func(hours int64) float64 {
var sum store.SvcHourOut
for _, h := range v.Hours {
if h.Hour > to/3600-hours {
sum.Legit += h.Legit
sum.Cls[store.ClsApp] += h.Cls[store.ClsApp]
sum.Cls[store.ClsProxy] += h.Cls[store.ClsProxy]
}
}
return round(errRate(sum)/(1-sloTarget), 2)
}
apdex := 1.0
if t.ApN > 0 {
apdex = (float64(t.ApSat) + float64(t.ApTol)/2) / float64(t.ApN)
}
var retries float64
if t.Req > 0 {
retries = float64(t.Retries) / float64(t.Req) * 100
}
spark := make([]uint32, 0, 48)
for _, h := range v.Hours {
spark = append(spark, h.Req)
}
if len(spark) > 48 {
spark = spark[len(spark)-48:]
}
mix := map[string]uint32{}
for i, n := range store.ClsNames {
mix[n] = t.Cls[i]
}
mix["ok"] = t.Req - errCount(t.Cls)
q := func(h [13]uint32, p float64) float64 { return round(store.Quantile(h, p), 1) }
return map[string]any{
"svc": v.Key, "alias": v.Alias, "req": t.Req, "rps_peak": round(float64(peak)/3600, 3),
"sli": round(1-rate, 5), "slo": sloTarget, "budget_left": round(math.Max(0, 1-rate/(1-sloTarget)), 4),
"burn_1h": burn(1), "burn_6h": burn(6), "apdex": round(apdex, 3),
"p50": tri{q(t.Dur, .5), q(t.Org, .5), q(t.Ov, .5)},
"p95": tri{q(t.Dur, .95), q(t.Org, .95), q(t.Ov, .95)},
"p99": tri{q(t.Dur, .99), q(t.Org, .99), q(t.Ov, .99)},
"retries_pct": round(retries, 3), "mix": mix, "spark": spark,
}
}
func (s *Server) servicesAPI(w http.ResponseWriter, r *http.Request) {
from, to, _, host, ok := s.svcWindow(w, r)
if !ok {
return
}
views := s.opt.Store.Services(from, to, "", host)
out := make([]map[string]any, 0, len(views))
for _, v := range views {
out = append(out, serviceItem(v, to))
}
writeJSON(w, 200, out)
}
func (s *Server) serviceAPI(w http.ResponseWriter, r *http.Request) {
from, to, _, host, ok := s.svcWindow(w, r)
if !ok {
return
}
views := s.opt.Store.Services(from, to, r.PathValue("svc"), host)
if len(views) == 0 {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "unknown service"})
return
}
v := views[0]
n := len(v.Hours)
req, errs, p95 := make([]uint32, n), make([]uint32, n), make([]float64, n)
hours := make([]int64, n)
for i, h := range v.Hours {
hours[i], req[i] = h.Hour*3600, h.Req
errs[i] = h.Cls[store.ClsApp] + h.Cls[store.ClsProxy]
p95[i] = round(store.Quantile(h.Dur, .95), 1)
}
type routeOut struct {
Route string `json:"route"`
Class string `json:"class"`
N uint32 `json:"n"`
}
var routes []routeOut
for _, e := range topKV(v.ErrRoutes, 50) {
cl, rt, _ := strings.Cut(e.Key, "|")
routes = append(routes, routeOut{rt, cl, e.N})
}
bounds := store.SvcBounds
writeJSON(w, 200, map[string]any{
"summary": serviceItem(v, to),
"timeline": map[string]any{"hours": hours, "req": req, "errors": errs, "p95": p95},
"histograms": map[string]any{"bounds": bounds[:], "total": v.Tot.Dur, "origin": v.Tot.Org, "overhead": v.Tot.Ov},
"top_routes": routes,
})
}
func (s *Server) protocolsAPI(w http.ResponseWriter, r *http.Request) {
from, to, svc, host, ok := s.svcWindow(w, r)
if !ok {
return
}
views := s.opt.Store.Services(from, to, svc, host)
protos, tls, methods := map[string]map[string]uint32{}, map[string]map[string]uint32{}, map[string]map[string]uint32{}
ciphers := map[string]uint32{}
add := func(dst map[string]map[string]uint32, src map[string]uint32) {
for k, n := range src {
a, b, _ := strings.Cut(k, "|")
if dst[a] == nil {
dst[a] = map[string]uint32{}
}
dst[a][b] += n
}
}
for _, v := range views {
add(protos, v.ProtoCls)
add(tls, v.TLSCls)
add(methods, v.Methods)
for k, n := range v.Ciphers {
ciphers[k] += n
}
}
// methods: the standard ones first, anomalous ones flagged
std := map[string]bool{"GET": true, "HEAD": true, "POST": true, "PUT": true, "PATCH": true, "DELETE": true, "OPTIONS": true}
type mOut struct {
Method string `json:"method"`
Odd bool `json:"odd"`
N uint32 `json:"n"`
Classes map[string]uint32 `json:"classes"`
}
var ms []mOut
for m, c := range methods {
var n uint32
for _, v := range c {
n += v
}
ms = append(ms, mOut{m, !std[m], n, c})
}
sort.Slice(ms, func(a, b int) bool { return ms[a].N > ms[b].N })
if len(ms) > 30 {
ms = ms[:30]
}
type cOut struct {
Name string `json:"name"`
N uint32 `json:"n"`
}
var cs []cOut
for _, e := range topKV(ciphers, 50) {
cs = append(cs, cOut{e.Key, e.N})
}
var https, plain uint32
for k, c := range tls {
for _, n := range c {
if k == "none" {
plain += n
} else {
https += n
}
}
}
writeJSON(w, 200, map[string]any{
"protocols": protos, "tls": tls, "ciphers": cs, "methods": ms,
"scheme": map[string]uint32{"https": https, "http": plain},
})
}