snapshot+hugo: speedtest tab with recent table + per-node aggregates
This commit is contained in:
@@ -23,9 +23,52 @@ type nodeData struct {
|
||||
Recent []sample `json:"recent"`
|
||||
}
|
||||
|
||||
// SpeedtestSnapshot exposes recent speedtest rows + day/week/month median
|
||||
// aggregates per node. Recent uses storage.SpeedTest's capitalized field
|
||||
// names (no JSON tags on the struct); the frontend reads them as-is.
|
||||
type SpeedtestSnapshot struct {
|
||||
Recent []storage.SpeedTest `json:"recent"`
|
||||
AggDay map[string]storage.Agg `json:"agg_day"`
|
||||
AggWeek map[string]storage.Agg `json:"agg_week"`
|
||||
AggMonth map[string]storage.Agg `json:"agg_month"`
|
||||
GeneratedAt int64 `json:"generated_at"`
|
||||
}
|
||||
|
||||
type snapshot struct {
|
||||
GeneratedAt string `json:"generated_at"`
|
||||
Nodes map[string]nodeData `json:"nodes"`
|
||||
Speedtest *SpeedtestSnapshot `json:"speedtest,omitempty"`
|
||||
}
|
||||
|
||||
// buildSpeedtestSnapshot collects the last 200 speedtest rows + per-node
|
||||
// medians over 24h/7d/30d windows. Aggregate errors are logged but
|
||||
// non-fatal — the rest of the snapshot still ships.
|
||||
func buildSpeedtestSnapshot(ctx context.Context, st *storage.Store) (*SpeedtestSnapshot, error) {
|
||||
recent, err := st.ListRecentSpeedTests(ctx, 200)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
day, week, month := now-86400, now-7*86400, now-30*86400
|
||||
aggDay, errDay := st.AggregateSpeedTestsSince(ctx, day)
|
||||
if errDay != nil {
|
||||
log.Printf("aggregate day: %v", errDay)
|
||||
}
|
||||
aggWeek, errWeek := st.AggregateSpeedTestsSince(ctx, week)
|
||||
if errWeek != nil {
|
||||
log.Printf("aggregate week: %v", errWeek)
|
||||
}
|
||||
aggMonth, errMonth := st.AggregateSpeedTestsSince(ctx, month)
|
||||
if errMonth != nil {
|
||||
log.Printf("aggregate month: %v", errMonth)
|
||||
}
|
||||
return &SpeedtestSnapshot{
|
||||
Recent: recent,
|
||||
AggDay: aggDay,
|
||||
AggWeek: aggWeek,
|
||||
AggMonth: aggMonth,
|
||||
GeneratedAt: now,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -63,6 +106,12 @@ func main() {
|
||||
s.Nodes[node] = nd
|
||||
}
|
||||
|
||||
if stSnap, err := buildSpeedtestSnapshot(ctx, st); err != nil {
|
||||
log.Printf("build speedtest snapshot: %v", err)
|
||||
} else {
|
||||
s.Speedtest = stSnap
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(*out), 0755); err != nil {
|
||||
log.Fatalf("mkdir %s: %v", filepath.Dir(*out), err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user