Serve /api/v1/stats counts from realtime in-memory counters (closes #27)
check / check (push) Successful in 2m20s

The stats endpoints recomputed every count with a COUNT(*) over each
table plus a MIN/MAX union scan on every request, which took the whole
timeout and returned 500 once the database passed a few GiB.

The row counts (ASNs, prefixes, peerings, peers, live routes) now live
in memory: seeded once at startup and adjusted on each write as routes
and records are inserted, updated and removed, so a read serves them
with no scan and no timer. A live-route upsert is an UPDATE, then an
INSERT only when no row matched, so its rows-affected count tells a new
route from a re-announced one. Oldest/newest timestamps read one row
from each end of the last_updated index, which also fixes the per-call
scan warning. The distinct-prefix histogram stays a single indexed
query.

JSON shape of /api/v1/stats and the status page is unchanged.

Model: opus-4-8
This commit is contained in:
2026-09-22 07:23:43 +00:00
parent 3898daad4e
commit 698390f66c
6 changed files with 812 additions and 222 deletions
+4 -18
View File
@@ -219,13 +219,6 @@ func (s *Server) handleStatusJSON() http.HandlerFunc {
const bitsPerMegabit = 1000000.0
// Get route counts from database
ipv4Routes, ipv6Routes, err := s.db.GetLiveRouteCountsContext(ctx)
if err != nil {
s.logger.Warn("Failed to get live route counts", "error", err)
// Continue with zero counts
}
// Get route update metrics
routeMetrics := s.streamer.GetMetricsTracker().GetRouteMetrics()
@@ -259,8 +252,8 @@ func (s *Server) handleStatusJSON() http.HandlerFunc {
Peers: dbStats.Peers,
DatabaseSizeBytes: dbStats.FileSizeBytes,
LiveRoutes: dbStats.LiveRoutes,
IPv4Routes: ipv4Routes,
IPv6Routes: ipv6Routes,
IPv4Routes: dbStats.IPv4Routes,
IPv6Routes: dbStats.IPv6Routes,
OldestRoute: dbStats.OldestRoute,
NewestRoute: dbStats.NewestRoute,
IPv4UpdatesPerSec: routeMetrics.IPv4UpdatesPerSec,
@@ -439,13 +432,6 @@ func (s *Server) handleStats() http.HandlerFunc {
const bitsPerMegabit = 1000000.0
// Get route counts from database
ipv4Routes, ipv6Routes, err := s.db.GetLiveRouteCountsContext(ctx)
if err != nil {
s.logger.Warn("Failed to get live route counts", "error", err)
// Continue with zero counts
}
// Get route update metrics
routeMetrics := s.streamer.GetMetricsTracker().GetRouteMetrics()
@@ -537,8 +523,8 @@ func (s *Server) handleStats() http.HandlerFunc {
Peers: dbStats.Peers,
DatabaseSizeBytes: dbStats.FileSizeBytes,
LiveRoutes: dbStats.LiveRoutes,
IPv4Routes: ipv4Routes,
IPv6Routes: ipv6Routes,
IPv4Routes: dbStats.IPv4Routes,
IPv6Routes: dbStats.IPv6Routes,
OldestRoute: dbStats.OldestRoute,
NewestRoute: dbStats.NewestRoute,
IPv4UpdatesPerSec: routeMetrics.IPv4UpdatesPerSec,