diff --git a/api/.sqlx/query-5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43.json b/api/.sqlx/query-5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43.json deleted file mode 100644 index 777adfb..0000000 --- a/api/.sqlx/query-5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n WITH bounds AS (\n SELECT make_interval(mins => $1) AS bucket,\n date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current\n ), buckets AS (\n SELECT generate_series(current - $2 * bucket, current - bucket, bucket) AS at FROM bounds\n ), per_server AS (\n SELECT date_bin(bounds.bucket, h.received_at, TIMESTAMPTZ '2000-01-01') AS at,\n h.ip, h.port, avg(h.players) AS players\n FROM server_heartbeat_log h\n CROSS JOIN bounds\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at >= bounds.current - $2 * bounds.bucket AND h.received_at < bounds.current\n GROUP BY 1, 2, 3\n )\n SELECT b.at AS \"at!\", count(p.ip)::int AS \"servers!\", coalesce(round(sum(p.players)), 0)::int AS \"players!\"\n FROM buckets b\n LEFT JOIN per_server p ON p.at = b.at\n GROUP BY b.at\n ORDER BY b.at\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "at!", - "type_info": "Timestamptz", - "origin": "Expression" - }, - { - "ordinal": 1, - "name": "servers!", - "type_info": "Int4", - "origin": "Expression" - }, - { - "ordinal": 2, - "name": "players!", - "type_info": "Int4", - "origin": "Expression" - } - ], - "parameters": { - "Left": [ - "Int4", - "Float8" - ] - }, - "nullable": [ - null, - null, - null - ] - }, - "hash": "5b6cb1e375d768e19f444b9f407d80ab686ec4309e28f33e98af9653e0510e43" -} diff --git a/api/.sqlx/query-93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003.json b/api/.sqlx/query-93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003.json new file mode 100644 index 0000000..90db3ff --- /dev/null +++ b/api/.sqlx/query-93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT coalesce(max(players), 0) AS \"players!\" FROM server_activity_peaks\n WHERE day >= (now() AT TIME ZONE 'UTC')::date - $1::int", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "players!", + "type_info": "Int4", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Int4" + ] + }, + "nullable": [ + null + ] + }, + "hash": "93eea59cfcf180c2a844d37d091ea617589d12ed2e0a83bdd21fadded3c3d003" +} diff --git a/api/.sqlx/query-a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017.json b/api/.sqlx/query-a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017.json new file mode 100644 index 0000000..e45a363 --- /dev/null +++ b/api/.sqlx/query-a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017.json @@ -0,0 +1,12 @@ +{ + "db_name": "PostgreSQL", + "query": "DELETE FROM server_activity_peaks WHERE day < current_date - 400", + "describe": { + "columns": [], + "parameters": { + "Left": [] + }, + "nullable": [] + }, + "hash": "a973e3651cccc5eda8cb8d30757435a668d6e76642ce55d5040addaaddf5c017" +} diff --git a/api/.sqlx/query-c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3.json b/api/.sqlx/query-c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3.json new file mode 100644 index 0000000..33b215e --- /dev/null +++ b/api/.sqlx/query-c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3.json @@ -0,0 +1,54 @@ +{ + "db_name": "PostgreSQL", + "query": "\n WITH bounds AS (\n SELECT make_interval(mins => $1) AS bucket,\n date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current,\n date_bin(make_interval(mins => $3), now(), TIMESTAMPTZ '2000-01-01') AS latest\n ), buckets AS (\n SELECT generate_series(current - $2 * bucket, current - bucket, bucket) AS at FROM bounds\n ), per_server AS (\n SELECT date_bin(bounds.bucket, h.received_at, TIMESTAMPTZ '2000-01-01') AS at,\n h.ip, h.port, avg(h.players) AS players\n FROM server_heartbeat_log h\n CROSS JOIN bounds\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at >= bounds.current - $2 * bounds.bucket AND h.received_at < bounds.current\n GROUP BY 1, 2, 3\n ), online AS (\n SELECT m.at, sum(latest.players) AS players\n FROM bounds\n CROSS JOIN generate_series(\n bounds.current - $2 * bounds.bucket,\n bounds.latest,\n make_interval(mins => $3)\n ) AS m(at)\n CROSS JOIN LATERAL (\n SELECT DISTINCT ON (h.ip, h.port) h.players\n FROM server_heartbeat_log h\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at > m.at - make_interval(mins => $4) AND h.received_at <= m.at\n ORDER BY h.ip, h.port, h.received_at DESC\n ) latest\n GROUP BY m.at\n ), peaks AS (\n SELECT date_bin(bounds.bucket, online.at, TIMESTAMPTZ '2000-01-01') AS at, max(online.players) AS players\n FROM online CROSS JOIN bounds\n GROUP BY 1\n ), whole AS (\n SELECT coalesce(max(online.players), 0)::int AS players\n FROM online CROSS JOIN bounds\n WHERE online.at >= bounds.current - $2 * bounds.bucket\n )\n SELECT b.at AS \"at!\", count(p.ip)::int AS \"servers!\", coalesce(round(sum(p.players)), 0)::int AS \"players!\",\n coalesce(pk.players, 0)::int AS \"peak_players!\", whole.players AS \"range_peak!\"\n FROM buckets b\n CROSS JOIN whole\n LEFT JOIN per_server p ON p.at = b.at\n LEFT JOIN peaks pk ON pk.at = b.at\n GROUP BY b.at, pk.players, whole.players\n ORDER BY b.at\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "at!", + "type_info": "Timestamptz", + "origin": "Expression" + }, + { + "ordinal": 1, + "name": "servers!", + "type_info": "Int4", + "origin": "Expression" + }, + { + "ordinal": 2, + "name": "players!", + "type_info": "Int4", + "origin": "Expression" + }, + { + "ordinal": 3, + "name": "peak_players!", + "type_info": "Int4", + "origin": "Expression" + }, + { + "ordinal": 4, + "name": "range_peak!", + "type_info": "Int4", + "origin": "Expression" + } + ], + "parameters": { + "Left": [ + "Int4", + "Float8", + "Int4", + "Int4" + ] + }, + "nullable": [ + null, + null, + null, + null, + null + ] + }, + "hash": "c105f82668f6dffc122fd260c3000f3b398402da30de05d54339d0b2a1d852c3" +} diff --git a/api/.sqlx/query-e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597.json b/api/.sqlx/query-e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597.json new file mode 100644 index 0000000..56c3130 --- /dev/null +++ b/api/.sqlx/query-e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n INSERT INTO server_activity_peaks AS p (day, players)\n SELECT (now() AT TIME ZONE 'UTC')::date, coalesce(sum(latest.players), 0)\n FROM (\n SELECT DISTINCT ON (h.ip, h.port) h.players\n FROM server_heartbeat_log h\n JOIN server_listings l ON l.ip = h.ip AND l.port = h.port\n WHERE h.received_at > now() - make_interval(mins => $1)\n ORDER BY h.ip, h.port, h.received_at DESC\n ) latest\n ON CONFLICT (day) DO UPDATE SET players = greatest(p.players, excluded.players)\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int4" + ] + }, + "nullable": [] + }, + "hash": "e9012bc9223b6610b7c31d60ea297dabf0f4666650de0b0bb02053016bbae597" +} diff --git a/api/.sqlx/query-acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4.json b/api/.sqlx/query-fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d.json similarity index 62% rename from api/.sqlx/query-acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4.json rename to api/.sqlx/query-fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d.json index 3fabd8b..8bf4ecc 100644 --- a/api/.sqlx/query-acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4.json +++ b/api/.sqlx/query-fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH days AS (\n SELECT generate_series((today - $1::int)::timestamp, (today - 1)::timestamp, interval '1 day')::date AS day\n FROM (SELECT (now() AT TIME ZONE 'UTC')::date AS today) t\n )\n SELECT days.day::timestamp AT TIME ZONE 'UTC' AS \"at!\",\n count(d.ip)::int AS \"servers!\",\n coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS \"players!\"\n FROM days\n LEFT JOIN server_activity_daily d ON d.day = days.day\n AND EXISTS (SELECT 1 FROM server_listings l WHERE l.ip = d.ip AND l.port = d.port)\n GROUP BY days.day\n ORDER BY days.day\n ", + "query": "\n WITH days AS (\n SELECT generate_series((today - $1::int)::timestamp, (today - 1)::timestamp, interval '1 day')::date AS day\n FROM (SELECT (now() AT TIME ZONE 'UTC')::date AS today) t\n )\n SELECT days.day::timestamp AT TIME ZONE 'UTC' AS \"at!\",\n count(d.ip)::int AS \"servers!\",\n coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS \"players!\",\n coalesce(pk.players, 0) AS \"peak_players!\"\n FROM days\n LEFT JOIN server_activity_daily d ON d.day = days.day\n AND EXISTS (SELECT 1 FROM server_listings l WHERE l.ip = d.ip AND l.port = d.port)\n LEFT JOIN server_activity_peaks pk ON pk.day = days.day\n GROUP BY days.day, pk.players\n ORDER BY days.day\n ", "describe": { "columns": [ { @@ -20,6 +20,12 @@ "name": "players!", "type_info": "Int4", "origin": "Expression" + }, + { + "ordinal": 3, + "name": "peak_players!", + "type_info": "Int4", + "origin": "Expression" } ], "parameters": { @@ -28,10 +34,11 @@ ] }, "nullable": [ + null, null, null, null ] }, - "hash": "acb722e75d97bd75a675a6e0153070345c0ef081f30d9e5efd1307f4b74008b4" + "hash": "fd5b8736cae460ef4bcd20932b9e4543feced1f02ad9e893892c36eed2e9a69d" } diff --git a/api/crates/api/migrations/0053_server_activity_peaks.sql b/api/crates/api/migrations/0053_server_activity_peaks.sql new file mode 100644 index 0000000..44ee529 --- /dev/null +++ b/api/crates/api/migrations/0053_server_activity_peaks.sql @@ -0,0 +1,29 @@ +-- The most players online at once across listed servers per UTC day, for the activity chart's year view. +-- Per-server daily averages can't give a cross-server "at once" peak, so heartbeats keep this (see +-- `servers/heartbeat.rs`), and it's kept for 400 days like `server_activity_daily`. +CREATE TABLE server_activity_peaks ( + day date PRIMARY KEY, + players integer NOT NULL CHECK (players >= 0) +); + +-- Back-filled from the 30-day log by the rule `servers/activity.rs` uses up to a month: every 5 minutes, each +-- listed server's latest heartbeat within the last 15 minutes, summed; the day's highest sum. +INSERT INTO server_activity_peaks (day, players) +SELECT (online.at AT TIME ZONE 'UTC')::date, max(online.players) +FROM ( + SELECT m.at, sum(latest.players) AS players + FROM generate_series( + date_bin(interval '5 minutes', (SELECT min(received_at) FROM server_heartbeat_log), TIMESTAMPTZ '2000-01-01'), + now(), + interval '5 minutes' + ) AS m(at) + CROSS JOIN LATERAL ( + SELECT DISTINCT ON (h.ip, h.port) h.players + FROM server_heartbeat_log h + JOIN server_listings l ON l.ip = h.ip AND l.port = h.port + WHERE h.received_at > m.at - interval '15 minutes' AND h.received_at <= m.at + ORDER BY h.ip, h.port, h.received_at DESC + ) latest + GROUP BY m.at +) online +GROUP BY 1; diff --git a/api/crates/api/src/servers/activity.rs b/api/crates/api/src/servers/activity.rs index 6095e0b..3967d4c 100644 --- a/api/crates/api/src/servers/activity.rs +++ b/api/crates/api/src/servers/activity.rs @@ -1,6 +1,6 @@ //! `GET /v1/servers/activity`: players and servers online over time, across listed servers, for the chart on //! the Servers page. Up to a month is built from `server_heartbeat_log`, which keeps 30 days; a year from -//! `server_activity_daily`. +//! `server_activity_daily` and `server_activity_peaks`. use axum::{ Json, @@ -14,6 +14,16 @@ use sqlx::PgPool; use crate::{app::AppState, error::ApiError}; +/// A listed server counts as online at a moment if it beat within this long before it: the heartbeat interval +/// plus five minutes of grace, so a late or missed beat doesn't drop it from the count. Also used where +/// heartbeats keep `server_activity_peaks`, and written out in its migration (0053). +pub const ONLINE_WINDOW_MINUTES: i32 = 15; +/// How often "online at once" is sampled for a peak up to a month. Half the heartbeat interval, so every +/// regular beat is seen at least once; month (30 days) is 8640 samples, each one short index range scan. The +/// grid is anchored like the buckets, so a shorter range's samples are a subset of a longer range's, and its +/// `peak_players` can never exceed a longer one's. +const SAMPLE_MINUTES: i32 = 5; + #[derive(Debug, Clone, Copy, Deserialize, Serialize, ToSchema)] #[serde(rename_all = "snake_case")] pub enum ActivityRange { @@ -46,6 +56,10 @@ pub struct ActivityQuery { pub struct ServerActivity { pub range: ActivityRange, pub bucket_minutes: i32, + /// The most players online at once across listed servers over the whole range, from the start of its + /// first point up to now, the bucket in progress included: no point reads higher, and a shorter range never + /// reads higher than a longer one. + pub peak_players: i32, /// Oldest first, one per bucket, empty buckets included. The bucket still in progress is left out, so the /// last point isn't a partial count. pub points: Vec, @@ -59,6 +73,10 @@ pub struct ActivityPoint { pub servers: i32, /// The average player count of each of those servers during the bucket, summed. pub players: i32, + /// The most players online at once during the bucket, across listed servers: each server's latest + /// heartbeat count, summed at the same moment. Up to a month, sampled every 5 minutes; for a year, the + /// day's highest total at any heartbeat. + pub peak_players: i32, } /// Players and servers online over the last day, week, month or year. @@ -75,30 +93,37 @@ pub async fn server_activity( ) -> Result, ApiError> { let range = query.range.unwrap_or(ActivityRange::Day); let (bucket_minutes, count) = range.buckets(); - let points = if let ActivityRange::Year = range { - by_day(&state.db, count).await? + let (points, peak_players) = if let ActivityRange::Year = range { + let (points, peak) = by_day(&state.db, count).await?; + // The daily peaks are kept at every beat and the month at 5-minute samples, so either can read higher + // (a spike between samples, or a server since unlisted); taking both keeps the year at least the month. + let (month_minutes, month_count) = ActivityRange::Month.buckets(); + let (_, month_peak) = by_bucket(&state.db, month_minutes, month_count).await?; + (points, peak.max(month_peak)) } else { by_bucket(&state.db, bucket_minutes, count).await? }; Ok(Json(ServerActivity { range, bucket_minutes, + peak_players, points, })) } -/// Buckets of `minutes` from the heartbeat log. +/// Buckets of `minutes` from the heartbeat log, and the range's peak: samples run on through the bucket in +/// progress to the latest whole sample moment, and the peak takes those from `count` buckets' length ago. async fn by_bucket( db: &PgPool, minutes: i32, count: i32, -) -> Result, sqlx::Error> { - sqlx::query_as!( - ActivityPoint, +) -> Result<(Vec, i32), sqlx::Error> { + let rows = sqlx::query!( r#" WITH bounds AS ( SELECT make_interval(mins => $1) AS bucket, - date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current + date_bin(make_interval(mins => $1), now(), TIMESTAMPTZ '2000-01-01') AS current, + date_bin(make_interval(mins => $3), now(), TIMESTAMPTZ '2000-01-01') AS latest ), buckets AS ( SELECT generate_series(current - $2 * bucket, current - bucket, bucket) AS at FROM bounds ), per_server AS ( @@ -109,23 +134,72 @@ async fn by_bucket( JOIN server_listings l ON l.ip = h.ip AND l.port = h.port WHERE h.received_at >= bounds.current - $2 * bounds.bucket AND h.received_at < bounds.current GROUP BY 1, 2, 3 + ), online AS ( + SELECT m.at, sum(latest.players) AS players + FROM bounds + CROSS JOIN generate_series( + bounds.current - $2 * bounds.bucket, + bounds.latest, + make_interval(mins => $3) + ) AS m(at) + CROSS JOIN LATERAL ( + SELECT DISTINCT ON (h.ip, h.port) h.players + FROM server_heartbeat_log h + JOIN server_listings l ON l.ip = h.ip AND l.port = h.port + WHERE h.received_at > m.at - make_interval(mins => $4) AND h.received_at <= m.at + ORDER BY h.ip, h.port, h.received_at DESC + ) latest + GROUP BY m.at + ), peaks AS ( + SELECT date_bin(bounds.bucket, online.at, TIMESTAMPTZ '2000-01-01') AS at, max(online.players) AS players + FROM online CROSS JOIN bounds + GROUP BY 1 + ), whole AS ( + SELECT coalesce(max(online.players), 0)::int AS players + FROM online CROSS JOIN bounds + WHERE online.at >= bounds.current - $2 * bounds.bucket ) - SELECT b.at AS "at!", count(p.ip)::int AS "servers!", coalesce(round(sum(p.players)), 0)::int AS "players!" + SELECT b.at AS "at!", count(p.ip)::int AS "servers!", coalesce(round(sum(p.players)), 0)::int AS "players!", + coalesce(pk.players, 0)::int AS "peak_players!", whole.players AS "range_peak!" FROM buckets b + CROSS JOIN whole LEFT JOIN per_server p ON p.at = b.at - GROUP BY b.at + LEFT JOIN peaks pk ON pk.at = b.at + GROUP BY b.at, pk.players, whole.players ORDER BY b.at "#, minutes, - f64::from(count) + f64::from(count), + SAMPLE_MINUTES, + ONLINE_WINDOW_MINUTES ) .fetch_all(db) - .await + .await?; + let peak = rows.first().map_or(0, |r| r.range_peak); + let points = rows + .into_iter() + .map(|r| ActivityPoint { + at: r.at, + servers: r.servers, + players: r.players, + peak_players: r.peak_players, + }) + .collect(); + Ok((points, peak)) } -/// The last `count` whole UTC days from the daily totals; today is still in progress, so it's left out. -async fn by_day(db: &PgPool, count: i32) -> Result, sqlx::Error> { - sqlx::query_as!( +/// The last `count` whole UTC days from the daily totals and peaks; today is still in progress, so it's left +/// out. And the highest daily peak from the first of those days through today, so the range's peak is never +/// below a day the chart shows, nor misses today. +async fn by_day(db: &PgPool, count: i32) -> Result<(Vec, i32), sqlx::Error> { + let peak = sqlx::query_scalar!( + r#"SELECT coalesce(max(players), 0) AS "players!" FROM server_activity_peaks + WHERE day >= (now() AT TIME ZONE 'UTC')::date - $1::int"#, + count + ) + .fetch_one(db) + .await?; + let points = sqlx::query_as!( ActivityPoint, r#" WITH days AS ( @@ -134,17 +208,20 @@ async fn by_day(db: &PgPool, count: i32) -> Result, sqlx::Err ) SELECT days.day::timestamp AT TIME ZONE 'UTC' AS "at!", count(d.ip)::int AS "servers!", - coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS "players!" + coalesce(round(sum(d.players_sum::float8 / d.heartbeats)), 0)::int AS "players!", + coalesce(pk.players, 0) AS "peak_players!" FROM days LEFT JOIN server_activity_daily d ON d.day = days.day AND EXISTS (SELECT 1 FROM server_listings l WHERE l.ip = d.ip AND l.port = d.port) - GROUP BY days.day + LEFT JOIN server_activity_peaks pk ON pk.day = days.day + GROUP BY days.day, pk.players ORDER BY days.day "#, count ) .fetch_all(db) - .await + .await?; + Ok((points, peak)) } #[cfg(test)] @@ -179,6 +256,10 @@ mod tests { .execute(&db) .await .unwrap(); + sqlx::query("UPDATE server_heartbeat_log SET received_at = received_at + interval '1 minute' WHERE players = 20") + .execute(&db) + .await + .unwrap(); let activity: ServerActivity = test_client::get(&db, "/v1/servers/activity").await.json(); @@ -188,10 +269,179 @@ mod tests { .points .iter() .filter(|p| p.servers > 0) - .map(|p| (p.servers, p.players)) + .map(|p| (p.servers, p.players, p.peak_players)) .collect(); - // .1 averaged (12 + 20) / 2 = 16, .2 had 12; .3 isn't listed. - assert_eq!(busy, [(2, 28)]); + // .1 averaged (12 + 20) / 2 = 16, .2 had 12; .3 isn't listed. At once: .1's latest 20, and .2's 12. + assert_eq!(busy, [(2, 28, 32)]); + } + + /// Lists the servers, then has each send a beat of `players` at `minutes` before the start of the + /// current 8-hour bin: a finished bucket for the day, week and month, and within the last day. + async fn beats_at(db: &PgPool, beats: &[(&str, i64, i64)]) { + for (ip, players, minutes) in beats { + listing::list(db, format!("{ip}:27015").parse().unwrap(), None) + .await + .unwrap(); + let mut body = report(27015); + body["players"] = serde_json::json!(players); + beat(db, ip, None, &body).await; + sqlx::query( + "UPDATE server_heartbeat_log SET received_at = date_bin('8 hours', now(), '2000-01-01') - make_interval(mins => $1::int) + WHERE received_at > now() - interval '1 minute'", + ) + .bind(minutes) + .execute(db) + .await + .unwrap(); + } + } + + async fn activity(db: &PgPool, range: &str) -> ServerActivity { + test_client::get(db, &format!("/v1/servers/activity?range={range}")) + .await + .json() + } + + fn peak(activity: &ServerActivity) -> i32 { + activity + .points + .iter() + .map(|p| p.peak_players) + .max() + .unwrap() + } + + #[sqlx::test] + async fn the_peak_is_players_online_at_the_same_moment(db: PgPool) { + // .1 has 10, then 2; .2 joins with 6 five minutes after .1's first beat, while .1 still counts. + beats_at( + &db, + &[ + ("203.0.113.1", 10, 180), + ("203.0.113.2", 6, 175), + ("203.0.113.1", 2, 170), + ], + ) + .await; + + let day = activity(&db, "day").await; + let busy: Vec<_> = day.points.iter().filter(|p| p.servers > 0).collect(); + // One 20-minute bucket: averages (10 + 2) / 2 + 6 = 12, but 10 + 6 were online at once. + assert_eq!(busy.len(), 1); + assert_eq!((busy[0].players, busy[0].peak_players), (12, 16)); + } + + #[sqlx::test] + async fn a_spike_the_average_flattens_still_peaks_and_longer_ranges_keep_it(db: PgPool) { + // One 2-hour bucket of beats every 10 minutes with 2 players, and one with 30 between them. + let quiet: Vec<_> = (0..11).map(|i| ("203.0.113.1", 2, 119 - i * 10)).collect(); + beats_at(&db, &quiet).await; + beats_at(&db, &[("203.0.113.1", 30, 64)]).await; + + let (day, week, month) = ( + activity(&db, "day").await, + activity(&db, "week").await, + activity(&db, "month").await, + ); + assert_eq!(peak(&day), 30); + // The 2-hour bucket's average is (11 × 2 + 30) / 12 = 4, but the spike is its peak. + let bucket = week.points.iter().find(|p| p.servers > 0).unwrap(); + assert_eq!((bucket.players, bucket.peak_players), (4, 30)); + // Longer ranges sample the same moments and more, so they never peak lower. + assert!(peak(&week) >= peak(&day) && peak(&month) >= peak(&day)); + } + + /// Has a listed server beat with `players` at the latest 5-minute moment: within every range's bucket in + /// progress, and its last sample. + async fn beat_now(db: &PgPool, ip: &str, players: i64) { + listing::list(db, format!("{ip}:27015").parse().unwrap(), None) + .await + .unwrap(); + let mut body = report(27015); + body["players"] = serde_json::json!(players); + beat(db, ip, None, &body).await; + sqlx::query( + "UPDATE server_heartbeat_log SET received_at = date_bin('5 minutes', now(), '2000-01-01') + WHERE received_at > now() - interval '1 minute'", + ) + .execute(db) + .await + .unwrap(); + } + + #[sqlx::test] + async fn the_range_peak_includes_the_bucket_in_progress(db: PgPool) { + beats_at(&db, &[("203.0.113.1", 2, 60)]).await; + beat_now(&db, "203.0.113.1", 30).await; + + let mut left_out = false; + for range in ["day", "week", "month", "year"] { + let activity = activity(&db, range).await; + assert_eq!(activity.peak_players, 30, "{range}"); + left_out |= peak(&activity) < 30; + } + // The points leave the bucket in progress out, so they can't show the spike yet. + assert!(left_out); + } + + #[sqlx::test] + async fn a_longer_range_never_peaks_lower(db: PgPool) { + // 50 at once 20 days ago, 30 three days ago, 10 now; 70 on a day 100 days ago. + beats_at( + &db, + &[ + ("203.0.113.1", 25, 20 * 1440), + ("203.0.113.2", 25, 20 * 1440), + ("203.0.113.1", 30, 3 * 1440), + ], + ) + .await; + beat_now(&db, "203.0.113.2", 10).await; + // Beats kept today's peak as sent, all at once; clear it to test the windows alone. + sqlx::query("DELETE FROM server_activity_peaks") + .execute(&db) + .await + .unwrap(); + let peaks = || async { + let mut peaks = vec![]; + for range in ["day", "week", "month", "year"] { + peaks.push(activity(&db, range).await.peak_players); + } + peaks + }; + + // With no daily peaks, a year is still at least the month. + assert_eq!(peaks().await, [10, 30, 50, 50]); + sqlx::query( + "INSERT INTO server_activity_peaks (day, players) VALUES (current_date - 100, 70)", + ) + .execute(&db) + .await + .unwrap(); + assert_eq!(peaks().await, [10, 30, 50, 70]); + } + + #[sqlx::test] + async fn heartbeats_keep_the_days_peak(db: PgPool) { + listing::list(&db, "203.0.113.1:27015".parse().unwrap(), None) + .await + .unwrap(); + listing::list(&db, "203.0.113.2:27015".parse().unwrap(), None) + .await + .unwrap(); + let mut quiet = report(27015); + quiet["players"] = serde_json::json!(4); + beat(&db, "203.0.113.1", None, &report(27015)).await; + beat(&db, "203.0.113.2", None, &report(27015)).await; + beat(&db, "203.0.113.1", None, &quiet).await; + // An unlisted server doesn't count. + beat(&db, "203.0.113.3", None, &report(27015)).await; + let today: i32 = sqlx::query_scalar("SELECT players FROM server_activity_peaks") + .fetch_one(&db) + .await + .unwrap(); + // 12 + 12 at the second beat; .1 dropping to 4 doesn't lower the day's peak. + assert_eq!(today, 24); } #[sqlx::test] @@ -205,10 +455,12 @@ mod tests { beat(&db, "203.0.113.1", None, &quiet).await; beat(&db, "203.0.113.2", None, &report(27015)).await; // Today is left out of the year, so move today's totals to yesterday. - sqlx::query("UPDATE server_activity_daily SET day = day - 1") - .execute(&db) - .await - .unwrap(); + for sql in [ + "UPDATE server_activity_daily SET day = day - 1", + "UPDATE server_activity_peaks SET day = day - 1", + ] { + sqlx::query(sql).execute(&db).await.unwrap(); + } let year: ServerActivity = test_client::get(&db, "/v1/servers/activity?range=year") .await @@ -216,8 +468,15 @@ mod tests { assert_eq!((year.bucket_minutes, year.points.len()), (1440, 365)); let last = year.points.last().unwrap(); - // (12 + 4) / 2 players on average, on the one listed server. - assert_eq!((last.servers, last.players), (1, 8)); + // (12 + 4) / 2 players on average, on the one listed server; at most 12 at once. + assert_eq!((last.servers, last.players, last.peak_players), (1, 8, 12)); + assert!( + year.points + .iter() + .rev() + .skip(1) + .all(|p| p.peak_players == 0) + ); } #[sqlx::test] @@ -226,7 +485,11 @@ mod tests { .await .json(); assert_eq!((week.bucket_minutes, week.points.len()), (120, 84)); - assert!(week.points.iter().all(|p| p.servers == 0 && p.players == 0)); + assert!( + week.points + .iter() + .all(|p| p.servers == 0 && p.players == 0 && p.peak_players == 0) + ); let status = test_client::get(&db, "/v1/servers/activity?range=decade") .await .status; diff --git a/api/crates/api/src/servers/heartbeat.rs b/api/crates/api/src/servers/heartbeat.rs index 1d9cd6b..c570811 100644 --- a/api/crates/api/src/servers/heartbeat.rs +++ b/api/crates/api/src/servers/heartbeat.rs @@ -18,7 +18,10 @@ use sqlx::{PgPool, types::Json as JsonColumn}; use utoipa::ToSchema; use utoipa_axum::{router::OpenApiRouter, routes}; -use super::keys::{self, KeyLookup}; +use super::{ + activity, + keys::{self, KeyLookup}, +}; use crate::{ app::AppState, client_address::{ClientAddress, ClientCountry}, @@ -174,7 +177,7 @@ async fn heartbeat( // Only listed servers feed the activity chart, which is all it shows; it also keeps unlisted // senders, which anyone can be, from growing the log. The log serves up to a month, the daily totals // a year. - sqlx::query!( + let logged = sqlx::query!( r#" WITH listed AS ( SELECT ip, port FROM server_listings WHERE ip = $1::inet AND port = $2 @@ -192,6 +195,27 @@ async fn heartbeat( ) .execute(&state.db) .await?; + // A beat is the only time the total online can rise, so the day's peak is kept here. Its own statement: + // within the one above, the log row it just wrote wouldn't be visible yet. + if logged.rows_affected() > 0 { + sqlx::query!( + r#" + INSERT INTO server_activity_peaks AS p (day, players) + SELECT (now() AT TIME ZONE 'UTC')::date, coalesce(sum(latest.players), 0) + FROM ( + SELECT DISTINCT ON (h.ip, h.port) h.players + FROM server_heartbeat_log h + JOIN server_listings l ON l.ip = h.ip AND l.port = h.port + WHERE h.received_at > now() - make_interval(mins => $1) + ORDER BY h.ip, h.port, h.received_at DESC + ) latest + ON CONFLICT (day) DO UPDATE SET players = greatest(p.players, excluded.players) + "#, + activity::ONLINE_WINDOW_MINUTES + ) + .execute(&state.db) + .await?; + } Ok(Json(HeartbeatResponse { key, @@ -211,7 +235,7 @@ fn clean_hostname(hostname: &str) -> String { } /// Prunes every hour, for as long as the API runs. Not on each heartbeat: anyone can send those, and each -/// would cost three deletes. +/// would cost four deletes. pub async fn run(db: PgPool) { let mut ticks = tokio::time::interval(PRUNE_INTERVAL); ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); @@ -223,8 +247,8 @@ pub async fn run(db: PgPool) { } } -/// Forgets unlisted servers silent for a week, logged heartbeats after 30 days and daily totals after 400 -/// days. +/// Forgets unlisted servers silent for a week, logged heartbeats after 30 days and daily totals and peaks +/// after 400 days. async fn prune(db: &PgPool) -> Result<(), sqlx::Error> { sqlx::query!("DELETE FROM server_heartbeat_log WHERE received_at < now() - interval '30 days'") .execute(db) @@ -232,6 +256,9 @@ async fn prune(db: &PgPool) -> Result<(), sqlx::Error> { sqlx::query!("DELETE FROM server_activity_daily WHERE day < current_date - 400") .execute(db) .await?; + sqlx::query!("DELETE FROM server_activity_peaks WHERE day < current_date - 400") + .execute(db) + .await?; sqlx::query!( r#" DELETE FROM server_heartbeats h diff --git a/api/openapi.json b/api/openapi.json index 64eb706..679725f 100644 --- a/api/openapi.json +++ b/api/openapi.json @@ -5930,7 +5930,8 @@ "required": [ "at", "servers", - "players" + "players", + "peak_players" ], "properties": { "at": { @@ -5938,6 +5939,11 @@ "format": "date-time", "description": "Where the bucket starts." }, + "peak_players": { + "type": "integer", + "format": "int32", + "description": "The most players online at once during the bucket, across listed servers: each server's latest\nheartbeat count, summed at the same moment. Up to a month, sampled every 5 minutes; for a year, the\nday's highest total at any heartbeat." + }, "players": { "type": "integer", "format": "int32", @@ -9950,6 +9956,7 @@ "required": [ "range", "bucket_minutes", + "peak_players", "points" ], "properties": { @@ -9957,6 +9964,11 @@ "type": "integer", "format": "int32" }, + "peak_players": { + "type": "integer", + "format": "int32", + "description": "The most players online at once across listed servers over the whole range, from the start of its\nfirst point up to now, the bucket in progress included: no point reads higher, and a shorter range never\nreads higher than a longer one." + }, "points": { "type": "array", "items": { diff --git a/docs/site-api.md b/docs/site-api.md index 29531dd..2168dae 100644 --- a/docs/site-api.md +++ b/docs/site-api.md @@ -1,5 +1,5 @@ # Site API -What the endpoints behind the website do: sign-in, players, maps, the server list and community posts; staff tools: `staff-api.md`. Rules for every client (errors, retries, SteamIDs) and game servers' endpoints: `api.md`. +The website's endpoints: sign-in, players, maps, server list, community posts; staff tools: `staff-api.md`. Rules for every client (errors, retries, SteamIDs) and game servers' endpoints: `api.md`. ## Sign-in and sessions Players sign in with Steam (OpenID 2.0); the endpoints answer browsers (redirects, cookies) via the site's origin (Caddy in production, Next.js locally). @@ -20,8 +20,8 @@ Players sign in with Steam (OpenID 2.0); the endpoints answer browsers (redirect ## Maps and leaderboards - The map list holds every map with a run (`maps.last_run_at`, set by the ingest); whether one is ranked is open. Sorts: name, players, last/first run (`listed_at`), tier, main-track record; `tier` keeps one tier. `player` (any SteamID form) with `completed=true|false` keeps maps whose main track they have or haven't finished in the list's style; `completed` counts them. `stats`: tiers, 5 newest, 5 most players this week. - Leaderboards rank personal bests per map, style and track; equal times share a place, the earlier run listed first. A tie's world record belongs to whoever set it first. -- `GET /v1/maps/{map}` carries every style and track's top 25, `next_cursor` and WR history, so a map page switches between them without a request. `…/leaderboard?cursor=` pages on from there; cursors are opaque. -- WR history is every run faster than all runs that finished before it on that board, by `finished_at`. Ties don't count. +- `GET /v1/maps/{map}` carries every style and track's top 25, `next_cursor` and WR history, so a map page switches between them locally. `…/leaderboard?cursor=` pages on from there; cursors are opaque. +- WR history: every run faster than all runs finished before it on that board, by `finished_at`. Ties don't count. - Tiers: the `tiers` policy holds the tier count, `map_tiers` the tier per map and track (append-only, newest in force). The API serves no tier until a count is published. - Any run opens by ID with stats, server and place (while a personal best); compliance and keys stay private. - Pre-2.0 runs (`legacy_*` tables, `legacy.rs`) count toward no 2.0 standing: not WRs, WR histories, placements or `…/records`. The normal style's main track has two more views, `view=all` (each player's faster of 2.0 and legacy) and `view=legacy`, on `…/leaderboard` and `…/personal-bests`; entries carry `legacy`; a legacy `run_id` is the old site's, at `GET /v1/legacy-runs/{id}` (no run page). A map's `legacy_record_history` charts the Pre-2.0 view. Banned players' legacy runs are left out. Legacy-only maps and players get pages and show in search. @@ -41,15 +41,15 @@ Players sign in with Steam (OpenID 2.0); the endpoints answer browsers (redirect - `GET /v1/daily-totals/players|runs?range=month|year|all` (`daily_totals.rs`, hourly): players with a time (incl. pre-2.0), 2.0 runs and WRs per UTC day. ## Server list -- `GET /v1/servers` shows only addresses in `server_listings`, kept by staff (lead, 2026-10-20), heard from within the `servers` policy's `listed_hours` (24); `online` means within `online_minutes` (25). Without the policy the list is empty. *Connected*: the latest heartbeat had a still-valid key. Compliance and versions aren't public. `whitelist` (staff-set: only approved players join) puts it under the Whitelist tab. -- Listed servers' heartbeats also go to `server_heartbeat_log` (players, 30 days) and `server_activity_daily` (per-server UTC-day totals, 400 days). `…/activity?range=day|week|month` buckets the log (20 min, 2 h, 8 h); `year` reads the daily totals. Per bucket: servers heard from, and their average players summed. -- Staff with `manage_servers` see every server on `/servers`: unlisted ones dimmed, toggles to list or unlist and to mark whitelist, and a dialog with its last report (versions, plugins, compliance, reported IP, key), loaded in the browser, never in the server render. The CLI's `servers seen`, `servers list ` and `servers unlist ` do the same. +- `GET /v1/servers` shows only addresses in `server_listings`, kept by staff (lead, 2026-10-20), heard from within the `servers` policy's `listed_hours` (24); `online` means within `online_minutes` (25). No policy, no list. *Connected*: the latest heartbeat had a still-valid key. Compliance and versions aren't public. `whitelist` (staff-set: only approved players join) puts it under the Whitelist tab. +- Listed servers' heartbeats also go to `server_heartbeat_log` (players, 30 days) and, per UTC day for 400 days, `server_activity_daily` (per-server totals) and `server_activity_peaks`. `…/activity?range=day|week|month` buckets the log (20 min, 2 h, 8 h), `year` the daily tables. Per bucket: servers heard from, their average players summed, and `peak_players`, most online at once; the top-level one, the whole range through now (never above a longer range's). +- Staff with `manage_servers` see every server on `/servers`: unlisted ones dimmed, toggles for listing and whitelist, and a dialog with its last report (versions, plugins, compliance, reported IP, key), browser-loaded, never server-rendered. The CLI's `servers seen` and `servers list|unlist ` do the same. ## Community posts -- Articles (Markdown) and YouTube videos (a link with an optional note; the site links out and embeds nothing, lead 2026-10-20). Players with `post_community` post and change or remove their own; `manage_posts` edits anyone's, with a `reason` and logged (`edit_post`, fields changed) when it's someone else's and changes something. Every write needs a session and our `Origin`. +- Articles (Markdown) and YouTube videos (a link with an optional note; the site links out and embeds nothing, lead 2026-10-20). Players with `post_community` post and change or remove their own; `manage_posts` edits anyone's, with a `reason`, logged (`edit_post`, fields changed) if someone else's and changed. Every write needs a session and our `Origin`. - `body_html` is rendered on each read by `posts/markdown.rs`, the only place deciding what HTML a post may contain: raw HTML becomes text, images their alt text, and links keep only web, mail and on-site addresses, opening `nofollow noopener`. -- A video link in any common form is stored as its YouTube ID (`posts/youtube.rs`), served back as a watch link. -- Pages link to `/community/{id}-{slug}`; only the ID counts, so a new title doesn't break links. Removing a post hides it and keeps the row. `DELETE /v1/posts/{post}` is for the author only. +- A video link in any common form is stored as its YouTube ID (`posts/youtube.rs`), served as a watch link. +- Pages link to `/community/{id}-{slug}`; only the ID counts, so retitling keeps links. Removal hides a post, keeping the row. `DELETE /v1/posts/{post}` is for the author only. ## Run comments - `/v1/runs/{id}/comments` (`runs/comments.rs`): players not banned from posting comment and reply one level deep (plain text, one per 30 s); the runner pins one; authors `DELETE` theirs (lead 2026-10-29). 25 threads a page, 3 replies each; more at `…/replies`. diff --git a/web/src/api/schema.ts b/web/src/api/schema.ts index 7a50b51..3444987 100644 --- a/web/src/api/schema.ts +++ b/web/src/api/schema.ts @@ -1446,6 +1446,13 @@ export interface components { * @description Where the bucket starts. */ at: string; + /** + * Format: int32 + * @description The most players online at once during the bucket, across listed servers: each server's latest + * heartbeat count, summed at the same moment. Up to a month, sampled every 5 minutes; for a year, the + * day's highest total at any heartbeat. + */ + peak_players: number; /** * Format: int32 * @description The average player count of each of those servers during the bucket, summed. @@ -3147,6 +3154,13 @@ export interface components { ServerActivity: { /** Format: int32 */ bucket_minutes: number; + /** + * Format: int32 + * @description The most players online at once across listed servers over the whole range, from the start of its + * first point up to now, the bucket in progress included: no point reads higher, and a shorter range never + * reads higher than a longer one. + */ + peak_players: number; /** * @description Oldest first, one per bucket, empty buckets included. The bucket still in progress is left out, so the * last point isn't a partial count. diff --git a/web/src/features/players/PlayerStyles.tsx b/web/src/features/players/PlayerStyles.tsx index 1952134..f4a985e 100644 --- a/web/src/features/players/PlayerStyles.tsx +++ b/web/src/features/players/PlayerStyles.tsx @@ -579,8 +579,8 @@ function PersonalBests({ {/* As on a map's board: Place centered over its medals, the play button close beside the time. On a - phone the style goes under the map, the gap beside the time and the date becomes an age under it, - so the row fits without scrolling. */} + phone the style goes under the map and the date becomes an age under the time, so the row fits + without scrolling. */} Replay - - {heading("gap", "Gap")} - )} - {/* On a phone, the Gap column's content, as on a map's board. */} - + {/* The gap to the board's record, beside the time at every width. */} + {pb.finished_at && ( // Counted from now, which differs between the server's render and the browser's. @@ -717,9 +710,6 @@ function PersonalBests({ {/* Replays open by 2.0 run ID; a pre-2.0 run's ID is the old site's. */} {!pb.legacy && } - - - {pb.finished_at ? (