mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-10-16 11:39:53 +00:00
Stats endpoints read hourly rollup tables filled once per closed hour by one advisory-locked task, instead of materialized views that re-scanned every observation on each refresh. IATA-set tables give exact distinct counts for any IATA set or region. Cleanup holds raw rows for unrolled hours (up to 24h). Adds /stats/series; ingest keeps packet observation counts, observer last IATA, channel message counts and trace tag summaries. 001_baseline.sql is rewritten, so pre-release 2.0 databases must be recreated. See docs/historical-stats.md.
30 lines
1.6 KiB
SQL
30 lines
1.6 KiB
SQL
-- name: GetSignalStats :many
|
|
-- Read the hourly rollup, never observations on an HTTP request.
|
|
-- Weight averages by sample counts instead of averaging regional/hourly means.
|
|
WITH readings AS (
|
|
SELECT iata, hour, kind, snr_bin, rssi_bin, receptions, snr_samples, snr_sum, rssi_samples, rssi_sum FROM analytics_hourly_signal
|
|
WHERE hour >= @since::timestamptz AND hour < @until::timestamptz
|
|
AND COALESCE(cardinality(@iatas::bpchar[]), 0) = 0
|
|
UNION ALL
|
|
SELECT iata, hour, kind, snr_bin, rssi_bin, receptions, snr_samples, snr_sum, rssi_samples, rssi_sum FROM analytics_hourly_signal
|
|
WHERE hour >= @since::timestamptz AND hour < @until::timestamptz
|
|
AND cardinality(@iatas::bpchar[]) > 0 AND iata = ANY(@iatas::bpchar[])
|
|
)
|
|
SELECT (3 + 4 * grouping(hour))::integer AS kind,
|
|
COALESCE(hour, 'epoch'::timestamptz)::timestamptz AS hour,
|
|
-1::integer AS snr_bin, -1::integer AS rssi_bin,
|
|
COALESCE(sum(receptions), 0)::bigint AS receptions,
|
|
COALESCE(sum(snr_samples), 0)::bigint AS snr_samples,
|
|
COALESCE(sum(snr_sum) / NULLIF(sum(snr_samples), 0), 0)::double precision AS snr_average,
|
|
COALESCE(sum(rssi_samples), 0)::bigint AS rssi_samples,
|
|
COALESCE(sum(rssi_sum) / NULLIF(sum(rssi_samples), 0), 0)::double precision AS rssi_average
|
|
FROM readings WHERE kind = 3
|
|
GROUP BY GROUPING SETS ((), (hour))
|
|
UNION ALL
|
|
SELECT (kind + 4)::integer, 'epoch'::timestamptz, snr_bin, rssi_bin,
|
|
sum(receptions)::bigint, 0::bigint, 0::double precision, 0::bigint, 0::double precision
|
|
FROM readings WHERE kind IN (1, 2)
|
|
GROUP BY kind, snr_bin, rssi_bin
|
|
ORDER BY kind, hour, snr_bin, rssi_bin;
|
|
|