Fix random_projects route not returning the requested number of projects (#3758)

* Fix random_projects route not returning the requested number of projects

* fix(labrinth): further improve random project route SQL query

* chore: fix typo in comment

* tweak(labrinth): more apparent and fast randomness for `random_projects_get`

* tweak(labrinth): even better random projects query

* chore: address formatting review

---------

Co-authored-by: Alejandro González <me@alegon.dev>
This commit is contained in:
Emma Alexia
2025-06-08 19:49:39 -04:00
committed by GitHub
parent 3489771d2e
commit 06f1df1995
4 changed files with 42 additions and 27 deletions

View File

@@ -94,14 +94,17 @@ pub async fn random_projects_get(
})?;
let project_ids = sqlx::query!(
"
SELECT id FROM mods TABLESAMPLE SYSTEM_ROWS($1) WHERE status = ANY($2)
",
count.count as i32,
// IDs are randomly generated (see the `generate_ids` macro), so ID order is
// equivalent to a random order
"SELECT id FROM mods WHERE status = ANY($1)
ORDER BY id
LIMIT $2
OFFSET GREATEST(ROUND(RANDOM() * (SELECT COUNT(*) FROM mods WHERE status = ANY($1)))::int8 - $2, 0)",
&*crate::models::projects::ProjectStatus::iterator()
.filter(|x| x.is_searchable())
.map(|x| x.to_string())
.collect::<Vec<String>>(),
count.count as i32,
)
.fetch(&**pool)
.map_ok(|m| db_ids::DBProjectId(m.id))