Files
urutau/internal/server/api_integrity.go
T
Lucas RayanandClaude Opus 5.5 0dc3268b29
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m46s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 24s
CI / Tests (Linux, race detector) (push) Successful in 1m22s
CI / Dependency audit (push) Successful in 27s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
fix: 1.4.4 read Traefik text logs, degraded source alert, mobile menu
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 01:20:03 -03:00

134 lines
4.1 KiB
Go

package server
import (
"net/http"
"sort"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
type integritySource struct {
Name string `json:"name"`
State string `json:"state"`
Lines uint64 `json:"lines"`
Malformed uint64 `json:"malformed"`
Last int64 `json:"last"`
Error string `json:"error,omitempty"`
}
type integrityHost struct {
Name string `json:"name"`
Online bool `json:"online"`
LastEvent int64 `json:"last_event"`
LagS int64 `json:"lag_s"`
Hourly []uint32 `json:"hourly"`
Gaps []store.Gap `json:"gaps"`
Sources []integritySource `json:"sources"`
ParseErrorRate float64 `json:"parse_error_rate"`
}
type integrityPeer struct {
Name string `json:"name"`
Online bool `json:"online"`
Live bool `json:"live"`
LastSync int64 `json:"last_sync"`
}
type integrityGeo struct {
CityBuild int64 `json:"city_build"`
ASNBuild int64 `json:"asn_build"`
State string `json:"state"`
LastUpdate int64 `json:"last_update"`
Error string `json:"error,omitempty"`
}
type integrityOut struct {
Range store.RangeInfo `json:"range"`
FromHour int64 `json:"from_hour"`
Hosts []integrityHost `json:"hosts"`
Peers []integrityPeer `json:"peers"`
EventLog store.LogStats `json:"eventlog"`
Geo integrityGeo `json:"geo"`
}
func init() {
registerUserRoute("GET /api/v1/integrity", func(s *Server) http.HandlerFunc { return s.integrity })
}
// integrity answers "is the data complete?": events ingested per hour and
// host, silences, source and peer states, parse errors, event log and geo base.
func (s *Server) integrity(w http.ResponseWriter, r *http.Request) {
rg, ok := s.rangeOf(w, r)
if !ok {
return
}
host := r.URL.Query().Get("host")
if host == "" {
host = "all"
}
if !s.knownHost(w, host) {
return
}
now := s.opt.Now().Unix()
info := rg.Info(now)
parts, all := s.parts(r.Context(), rg, host)
fromH, toH := info.From/3600, info.To/3600
if toH-fromH > 24*31-1 {
fromH = toH - (24*31 - 1)
}
out := integrityOut{Range: info, FromHour: fromH, Hosts: []integrityHost{}, Peers: []integrityPeer{}}
seen := map[string]bool{}
for _, p := range parts {
name := p.Host
if name == "" && len(p.Hosts) > 0 {
name = p.Hosts[0].Name
}
seen[name] = true
hh := integrityHost{Name: name, Online: true, Sources: []integritySource{}}
hh.Hourly = store.HourlyFromTimeline(p.Timeline, fromH, toH)
hh.Gaps = store.FindGaps(hh.Hourly, fromH)
var lines, bad uint64
for _, src := range p.Sources {
if src.Name == "geo" {
if name == s.opt.Store.Name() {
out.Geo = integrityGeo{State: src.State, LastUpdate: src.LastEvent, Error: src.Error}
if store.GeoBuilds != nil {
out.Geo.CityBuild, out.Geo.ASNBuild = store.GeoBuilds()
}
}
continue
}
hh.Sources = append(hh.Sources, integritySource{Name: src.Name, State: src.State, Lines: src.Lines, Malformed: src.Malformed, Last: src.LastEvent, Error: src.Error})
lines += src.Lines
bad += src.Malformed
if src.LastEvent > hh.LastEvent {
hh.LastEvent = src.LastEvent
}
}
if lines+bad > 0 {
hh.ParseErrorRate = float64(bad) / float64(lines+bad)
}
if hh.LastEvent > 0 {
hh.LagS = now - hh.LastEvent
if hh.LagS < 0 {
hh.LagS = 0
}
}
out.Hosts = append(out.Hosts, hh)
}
for _, hi := range all {
if hi.Self {
continue
}
out.Peers = append(out.Peers, integrityPeer{Name: hi.Name, Online: hi.Online, Live: hi.Live, LastSync: hi.LastSeen})
if !seen[hi.Name] && (host == "all" || host == hi.Name) {
out.Hosts = append(out.Hosts, integrityHost{Name: hi.Name, Online: hi.Online, LastEvent: hi.LastSeen,
Hourly: make([]uint32, toH-fromH+1), Gaps: []store.Gap{}, Sources: []integritySource{}})
}
}
sort.SliceStable(out.Hosts, func(i, j int) bool { return out.Hosts[i].Name < out.Hosts[j].Name })
sort.SliceStable(out.Peers, func(i, j int) bool { return out.Peers[i].Name < out.Peers[j].Name })
out.EventLog = s.opt.Store.LogStats()
writeJSON(w, 200, out)
}