Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 79 additions & 10 deletions api/crates/api/src/runs/recent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct RecentRunsQuery {
records: Option<bool>,
/// 1 to 100. Default 50.
limit: Option<i64>,
/// Runs to skip, for going straight to a page. Default 0. Counted after `cursor` when both are given.
offset: Option<i64>,
/// Continue after this point: a `next_cursor` from an earlier page with the same filters.
cursor: Option<String>,
}
Expand All @@ -52,6 +54,8 @@ pub struct RecentRuns {
pub runs: Vec<RecentRun>,
/// Pass as `cursor` for the runs before these. None at the end.
pub next_cursor: Option<String>,
/// How many runs match the filters, across all pages.
pub total: i64,
/// Number of tiers in force; None while the community hasn't set tiers, and runs then have no tier.
pub tier_count: Option<i16>,
}
Expand Down Expand Up @@ -88,15 +92,16 @@ pub struct RecentRun {
params(RecentRunsQuery),
responses(
(status = OK, body = RecentRuns),
(status = UNPROCESSABLE_ENTITY, description = "Not a valid tier, limit or cursor", body = ErrorBody),
(status = UNPROCESSABLE_ENTITY, description = "Not a valid tier, limit, offset or cursor", body = ErrorBody),
),
)]
pub async fn recent_runs(
State(state): State<AppState>,
Query(query): Query<RecentRunsQuery>,
) -> Result<Json<RecentRuns>, ApiError> {
let limit = query.limit.unwrap_or(DEFAULT_LIMIT);
check_page(limit, 0, MAX_LIMIT).map_err(ApiError::Invalid)?;
let offset = query.offset.unwrap_or(0);
check_page(limit, offset, MAX_LIMIT).map_err(ApiError::Invalid)?;
if query.min_tier.is_some_and(|tier| tier < 1) {
return Err(ApiError::Invalid("min_tier must be 1 or more".to_owned()));
}
Expand All @@ -110,11 +115,16 @@ pub async fn recent_runs(
return Ok(Json(RecentRuns {
runs: Vec::new(),
next_cursor: None,
total: 0,
tier_count,
}));
}

let mut runs = page(&state.db, &query, after, limit + 1, tier_count.is_some()).await?;
let tiers_in_force = tier_count.is_some();
let (mut runs, total) = tokio::try_join!(
page(&state.db, &query, after, limit + 1, offset, tiers_in_force),
count(&state.db, &query, tiers_in_force),
)?;
let limit = usize::try_from(limit).expect("checked to be positive");
let next_cursor = (runs.len() > limit).then(|| {
runs.truncate(limit);
Expand All @@ -123,6 +133,7 @@ pub async fn recent_runs(
Ok(Json(RecentRuns {
runs,
next_cursor: next_cursor.flatten(),
total,
tier_count,
}))
}
Expand All @@ -140,17 +151,50 @@ async fn position(db: &PgPool, cursor: &str) -> Result<Position, ApiError> {
Ok((finished_at, run_id))
}

fn track_key(query: &RecentRunsQuery) -> Option<&'static str> {
query.track.map(|track| match track {
TrackKind::Main => "main",
TrackKind::Bonus => "bonus",
})
}

/// Every run the filters match, ignoring `cursor`, `offset` and `limit`. The same conditions as `page`'s.
async fn count(
db: &PgPool,
query: &RecentRunsQuery,
tiers_in_force: bool,
) -> Result<i64, sqlx::Error> {
sqlx::query_scalar!(
r#"
SELECT count(*) AS "total!"
FROM runs r
LEFT JOIN LATERAL (SELECT tier FROM map_tiers WHERE map_id = r.map_id AND track = r.track
ORDER BY assigned_at DESC LIMIT 1) tier ON $5
WHERE r.invalidated_at IS NULL
AND (NOT $4 OR r.was_record)
AND ($1::text IS NULL OR r.style = $1)
AND ($2::text IS NULL OR ($2 = 'main') = (r.track = 0))
AND ($3::smallint IS NULL OR tier.tier >= $3)
"#,
query.style,
track_key(query),
query.min_tier,
query.records.unwrap_or(false),
tiers_in_force,
)
.fetch_one(db)
.await
}

async fn page(
db: &PgPool,
query: &RecentRunsQuery,
after: Option<Position>,
limit: i64,
offset: i64,
tiers_in_force: bool,
) -> Result<Vec<RecentRun>, sqlx::Error> {
let track = query.track.map(|track| match track {
TrackKind::Main => "main",
TrackKind::Bonus => "bonus",
});
let track = track_key(query);
// "Before" a run means earlier on its board in WR history's order: finish time, then ID. Records only
// reads the stored `was_record` through its own index (`leaderboards/world_records.rs`). `prior`, the
// board's best before the run, and placements are worked out for the page's runs alone, reading the
Expand All @@ -169,7 +213,7 @@ async fn page(
AND ($3::smallint IS NULL OR tier.tier >= $3)
AND ($5::timestamptz IS NULL OR (r.finished_at, r.id) < ($5, $6::bigint))
ORDER BY r.finished_at DESC, r.id DESC
LIMIT $8
LIMIT $8 OFFSET $9
)
SELECT l.id, l.style, l.track, l.time, l.finished_at, l.tier AS "tier?", prior.best AS "best?",
m.name AS map, place.placement AS "placement?",
Expand Down Expand Up @@ -206,7 +250,8 @@ async fn page(
after.map(|(finished_at, _)| finished_at),
after.map(|(_, run_id)| run_id),
tiers_in_force,
limit
limit,
offset
)
.fetch_all(db)
.await?;
Expand Down Expand Up @@ -360,6 +405,29 @@ mod tests {
assert_eq!(test_client::get(&db, "/v1/runs?cursor=x").await.status, 422);
assert_eq!(test_client::get(&db, "/v1/runs?limit=0").await.status, 422);

// An offset goes straight to a page, and the total counts the whole filtered list, not the page.
let third = list(&db, "?limit=2&offset=2").await;
assert_eq!(
third,
[
row("ricochet", "bhop_badges", 0, 28.0, Some(1), true),
row("ricochet", "bhop_badges", 0, 33.0, Some(2), false),
]
);
let last: RecentRuns = test_client::get(&db, "/v1/runs?limit=2&offset=4")
.await
.json();
assert_eq!((last.runs.len(), last.total), (1, 5));
assert_eq!(last.next_cursor, None);
let main: RecentRuns = test_client::get(&db, "/v1/runs?track=main&limit=1")
.await
.json();
assert_eq!(main.total, 4);
assert_eq!(
test_client::get(&db, "/v1/runs?offset=-1").await.status,
422
);

// An invalidated record leaves the list, and the record it had beaten is the board's best again.
let run_id: i64 = sqlx::query_scalar("SELECT id FROM runs WHERE server_run_id = 'c'")
.fetch_one(&db)
Expand Down Expand Up @@ -387,7 +455,8 @@ mod tests {
] {
test_client::post(&db, "/v1/runs", Some(&key), &body).await;
}
assert_eq!(list(&db, "?min_tier=1").await.len(), 0);
let none: RecentRuns = test_client::get(&db, "/v1/runs?min_tier=1").await.json();
assert_eq!((none.runs.len(), none.total), (0, 0));
assert_eq!(
test_client::get(&db, "/v1/runs?min_tier=0").await.status,
422
Expand Down
23 changes: 21 additions & 2 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,19 @@
"format": "int64"
}
},
{
"name": "offset",
"in": "query",
"description": "Runs to skip, for going straight to a page. Default 0. Counted after `cursor` when both are given.",
"required": false,
"schema": {
"type": [
"integer",
"null"
],
"format": "int64"
}
},
{
"name": "cursor",
"in": "query",
Expand All @@ -2063,7 +2076,7 @@
}
},
"422": {
"description": "Not a valid tier, limit or cursor",
"description": "Not a valid tier, limit, offset or cursor",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -8858,7 +8871,8 @@
"RecentRuns": {
"type": "object",
"required": [
"runs"
"runs",
"total"
],
"properties": {
"next_cursor": {
Expand All @@ -8882,6 +8896,11 @@
],
"format": "int32",
"description": "Number of tiers in force; None while the community hasn't set tiers, and runs then have no tier."
},
"total": {
"type": "integer",
"format": "int64",
"description": "How many runs match the filters, across all pages."
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion docs/site-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Players sign in with Steam (OpenID 2.0). These endpoints answer browsers with re
- Stored, not computed per visit (lead, 2026-10-26): `personal_bests.points`, `standings` (per style, main and bonuses apart) and `legacy_standings`, recounted by `leaderboards/standings.rs` where they change: a track when a personal best or its tier changes there (a new WR moves all points); everything on a points or tiers policy, or at startup if the formula changed; legacy on bans, lifts, imports. One advisory lock serializes recounts. So are world records (`leaderboards/world_records.rs`): holders and `runs.was_record`, per board.

## Activity
- `GET /v1/runs?style=&track=main|bonus&min_tier=&records=&cursor=` (`runs/recent.rs`): valid runs newest first by `finished_at`, each with its board's tier, `placement` counted against the valid runs before it (null unless it beat the player's own best), `world_record` (as in WR history) and its `gap` to the record it faced. `min_tier` matches nothing until a tier count is published.
- `GET /v1/runs?style=&track=main|bonus&min_tier=&records=&cursor=&offset=` (`runs/recent.rs`, with `total`): valid runs newest first by `finished_at`, each with its board's tier, `placement` against the valid runs before it (null unless a personal best), `world_record` (as in WR history) and `gap` to the record it faced. `min_tier` matches nothing until a tier count is published.

## Daily totals
- `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.
Expand Down
9 changes: 8 additions & 1 deletion web/src/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2657,6 +2657,11 @@ export interface components {
* @description Number of tiers in force; None while the community hasn't set tiers, and runs then have no tier.
*/
tier_count?: number | null;
/**
* Format: int64
* @description How many runs match the filters, across all pages.
*/
total: number;
};
Record: {
/** Format: date-time */
Expand Down Expand Up @@ -4688,6 +4693,8 @@ export interface operations {
records?: boolean | null;
/** @description 1 to 100. Default 50. */
limit?: number | null;
/** @description Runs to skip, for going straight to a page. Default 0. Counted after `cursor` when both are given. */
offset?: number | null;
/** @description Continue after this point: a `next_cursor` from an earlier page with the same filters. */
cursor?: string | null;
};
Expand All @@ -4705,7 +4712,7 @@ export interface operations {
"application/json": components["schemas"]["RecentRuns"];
};
};
/** @description Not a valid tier, limit or cursor */
/** @description Not a valid tier, limit, offset or cursor */
422: {
headers: {
[name: string]: unknown;
Expand Down
32 changes: 17 additions & 15 deletions web/src/app/activity/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Activity page: runs and world records per day, then the latest runs, narrowed by style, track, lowest tier or
// world records only.
// Activity page: one board. Runs and world records per day, then the latest runs, narrowed by style, track,
// lowest tier or world records only.
import type { Metadata } from "next";
import { RecentRuns } from "@/features/activity/RecentRuns";
import { DailyTotals } from "@/features/dailyTotals/DailyTotals";
Expand All @@ -12,19 +12,21 @@ export const metadata: Metadata = {

export default function ActivityPage({ searchParams }: PageProps<"/activity">) {
return (
<Page className="gap-6">
<h1 className="text-3xl font-bold">Activity</h1>
<DailyTotals kind="runs" />
{searchParams.then(({ style, track, tier, show }) => (
<RecentRuns
params={{
style: typeof style === "string" ? style : undefined,
track: typeof track === "string" ? track : undefined,
tier: typeof tier === "string" ? tier : undefined,
show: typeof show === "string" ? show : undefined,
}}
/>
))}
<Page width="medium" className="gap-0">
<div className="border-border bg-surface overflow-hidden rounded-lg border">
<DailyTotals kind="runs" heading="Activity" plain />
{searchParams.then(({ style, track, tier, show, page }) => (
<RecentRuns
params={{
style: typeof style === "string" ? style : undefined,
track: typeof track === "string" ? track : undefined,
tier: typeof tier === "string" ? tier : undefined,
show: typeof show === "string" ? show : undefined,
page: typeof page === "string" ? page : undefined,
}}
/>
))}
</div>
</Page>
);
}
Loading