Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
71 lines
1.9 KiB
Go
71 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 !hostListOK(host) {
|
|
badRequest(w, "invalid host")
|
|
return
|
|
}
|
|
sel, ok := s.parseHosts(host)
|
|
if !ok {
|
|
writeJSON(w, http.StatusNotFound, map[string]string{"error": "unknown host"})
|
|
return
|
|
}
|
|
host = sel.csv(s.opt.Store.Name())
|
|
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))
|
|
}
|
|
}
|
|
}
|