Skip to content

controllers/krate/downloads: Perform queries in parallel #10577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Changes from all commits
Commits
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
44 changes: 24 additions & 20 deletions src/controllers/krate/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use axum_extra::response::ErasedJson;
use crates_io_diesel_helpers::to_char;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use futures_util::FutureExt;
use std::cmp;

/// Get the download counts for a crate.
Expand Down Expand Up @@ -46,30 +47,33 @@ pub async fn get_crate_downloads(state: AppState, path: CratePath) -> AppResult<
versions.sort_unstable_by(|a, b| b.num.cmp(&a.num));
let (latest_five, rest) = versions.split_at(cmp::min(5, versions.len()));

let downloads = VersionDownload::belonging_to(latest_five)
.filter(version_downloads::date.gt(date(now - 90.days())))
.order((
version_downloads::date.asc(),
version_downloads::version_id.desc(),
))
.load(&mut conn)
.await?
let sum_downloads = sql::<BigInt>("SUM(version_downloads.downloads)");
let (downloads, extra) = tokio::try_join!(
VersionDownload::belonging_to(latest_five)
.filter(version_downloads::date.gt(date(now - 90.days())))
.order((
version_downloads::date.asc(),
version_downloads::version_id.desc(),
))
.load(&mut conn)
.boxed(),
VersionDownload::belonging_to(rest)
.select((
to_char(version_downloads::date, "YYYY-MM-DD"),
sum_downloads,
))
.filter(version_downloads::date.gt(date(now - 90.days())))
.group_by(version_downloads::date)
.order(version_downloads::date.asc())
.load::<ExtraDownload>(&mut conn)
.boxed(),
)?;

let downloads = downloads
.into_iter()
.map(VersionDownload::into)
.collect::<Vec<EncodableVersionDownload>>();

let sum_downloads = sql::<BigInt>("SUM(version_downloads.downloads)");
let extra: Vec<ExtraDownload> = VersionDownload::belonging_to(rest)
.select((
to_char(version_downloads::date, "YYYY-MM-DD"),
sum_downloads,
))
.filter(version_downloads::date.gt(date(now - 90.days())))
.group_by(version_downloads::date)
.order(version_downloads::date.asc())
.load(&mut conn)
.await?;

#[derive(Serialize, Queryable)]
struct ExtraDownload {
date: String,
Expand Down