Files
MrAlders0n ded8708fec Stats, adverts, traces and telemetry: bounded statements, same results (#243)
* feat(db): observer adverts on a partial index once observations carry their payload type

* fix(db): keep today's adverts statement unguarded and rate-limit the gate's read warning

* feat(background): batched, resumable payload-type backfill for untyped observations

* fix(background): cap each payload-type backfill statement at 50,000 rows

* fix(db): reject a non-positive backfill row cap

* perf(db): probe rollups per hour and IATA for region-sized stats lists

* fix(db): keep the stats probes inside each rollup table's hour range

* test(db): push the far-future until to year 9999 so a missing path-stats end bound times out

* perf(db): row-comparison trace cursor and a since-only telemetry statement

* test(db): pin the empty trace cursor tag and run the trace pagination tests in CI

* test(planguard): stats, adverts, traces and telemetry statements leave pending; diff cases

* test(planguard): window-bounded advert roles, short quiet page, per-observer telemetry cursors
2026-10-19 19:18:45 -04:00

37 lines
1.0 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package db
import (
"strings"
"time"
)
// probeIATALimit is the widest distinct IATA list the per-(hour, IATA) probe statements take; wider
// lists read today's statements, which scan the window once.
const probeIATALimit = 32
// distinctIATAs counts the codes as Postgres bpchar compares them: trailing blanks ignored, case kept.
func distinctIATAs(iatas []string) int {
seen := make(map[string]struct{}, len(iatas))
for _, s := range iatas {
seen[strings.TrimRight(s, " ")] = struct{}{}
}
return len(seen)
}
// useStatsProbe reports whether a rollup aggregate may use its probe statement: at most probeIATALimit
// distinct codes, and every bound on a UTC hour (the probes step hour by hour from since).
func useStatsProbe(iatas []string, bounds ...time.Time) bool {
if distinctIATAs(iatas) > probeIATALimit {
return false
}
for _, b := range bounds {
if !b.Equal(b.Truncate(time.Hour)) {
return false
}
}
return true
}