Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions crates/crates_io_api_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ pub struct EncodableCrate {
#[schema(example = 456_789)]
pub recent_downloads: Option<i64>,

/// The total number of downloads for this crate in the last month.
#[schema(example = 123_456)]
pub monthly_downloads: Option<i64>,

/// The total number of downloads for this crate in the last week.
#[schema(example = 56_789)]
pub weekly_downloads: Option<i64>,

/// The "default" version of this crate.
///
/// This version will be displayed by default on the crate's page.
Expand Down Expand Up @@ -380,6 +388,8 @@ impl EncodableCrate {
exact_match: bool,
downloads: i64,
recent_downloads: Option<i64>,
monthly_downloads: Option<i64>,
weekly_downloads: Option<i64>,
) -> Self {
let Crate {
name,
Expand Down Expand Up @@ -440,6 +450,8 @@ impl EncodableCrate {
created_at,
downloads,
recent_downloads,
monthly_downloads,
weekly_downloads,
versions,
keywords: keyword_ids,
categories: category_ids,
Expand Down Expand Up @@ -477,6 +489,8 @@ impl EncodableCrate {
exact_match: bool,
downloads: i64,
recent_downloads: Option<i64>,
monthly_downloads: Option<i64>,
weekly_downloads: Option<i64>,
) -> Self {
Self::from(
krate,
Expand All @@ -490,6 +504,8 @@ impl EncodableCrate {
exact_match,
downloads,
recent_downloads,
monthly_downloads,
weekly_downloads,
)
}
}
Expand Down Expand Up @@ -1187,6 +1203,8 @@ mod tests {
.and_utc(),
downloads: 0,
recent_downloads: None,
monthly_downloads: None,
weekly_downloads: None,
default_version: None,
num_versions: 0,
yanked: false,
Expand Down
16 changes: 12 additions & 4 deletions crates/crates_io_database/src/schema.patch
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/// The `id` column of the `dependencies` table.
///
/// Its SQL type is `Int4`.
@@ -773,6 +767,24 @@
@@ -773,6 +767,32 @@
}

diesel::table! {
Expand All @@ -58,14 +58,22 @@
+ ///
+ /// Its SQL type is `BigInt`.
+ downloads -> BigInt,
+ /// The `monthly` column of the `recent_crate_downloads` table.
+ ///
+ /// Its SQL type is `BigInt`.
+ monthly -> BigInt,
+ /// The `weekly` column of the `recent_crate_downloads` table.
+ ///
+ /// Its SQL type is `BigInt`.
+ weekly -> BigInt,
+ }
+}
+
+diesel::table! {
use diesel::sql_types::*;
use diesel_full_text_search::Tsvector;

@@ -1214,7 +1226,8 @@
@@ -1214,7 +1234,8 @@
diesel::joinable!(crate_downloads -> crates (crate_id));
diesel::joinable!(crate_owner_invitations -> crates (crate_id));
diesel::joinable!(crate_owners -> crates (crate_id));
Expand All @@ -75,15 +83,15 @@
diesel::joinable!(crates_categories -> categories (category_id));
diesel::joinable!(crates_categories -> crates (crate_id));
diesel::joinable!(crates_keywords -> crates (crate_id));
@@ -1230,6 +1243,7 @@
@@ -1230,6 +1251,7 @@
diesel::joinable!(publish_limit_buckets -> users (user_id));
diesel::joinable!(publish_rate_overrides -> users (user_id));
diesel::joinable!(readme_renderings -> versions (version_id));
+diesel::joinable!(recent_crate_downloads -> crates (crate_id));
diesel::joinable!(trustpub_configs_github -> crates (crate_id));
diesel::joinable!(trustpub_configs_gitlab -> crates (crate_id));
diesel::joinable!(version_downloads -> versions (version_id));
@@ -1262,6 +1276,7 @@
@@ -1262,6 +1284,7 @@
publish_limit_buckets,
publish_rate_overrides,
readme_renderings,
Expand Down
8 changes: 8 additions & 0 deletions crates/crates_io_database/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,14 @@ diesel::table! {
///
/// Its SQL type is `BigInt`.
downloads -> BigInt,
/// The `monthly` column of the `recent_crate_downloads` table.
///
/// Its SQL type is `BigInt`.
monthly -> BigInt,
/// The `weekly` column of the `recent_crate_downloads` table.
///
/// Its SQL type is `BigInt`.
weekly -> BigInt,
}
}

Expand Down
8 changes: 8 additions & 0 deletions migrations/2025-11-30-121230-add_monthly_downloads/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP MATERIALIZED VIEW recent_crate_downloads;
CREATE MATERIALIZED VIEW recent_crate_downloads (crate_id, downloads) AS
SELECT crate_id, SUM(version_downloads.downloads) FROM version_downloads
INNER JOIN versions
ON version_downloads.version_id = versions.id
WHERE version_downloads.date > date(CURRENT_TIMESTAMP - INTERVAL '90 days')
GROUP BY crate_id;
CREATE UNIQUE INDEX recent_crate_downloads_crate_id ON recent_crate_downloads (crate_id);
13 changes: 13 additions & 0 deletions migrations/2025-11-30-121230-add_monthly_downloads/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DROP MATERIALIZED VIEW recent_crate_downloads;
CREATE MATERIALIZED VIEW recent_crate_downloads (crate_id, downloads, monthly, weekly) AS
SELECT
crate_id,
SUM(version_downloads.downloads),
SUM(version_downloads.downloads) FILTER (WHERE version_downloads.date > CURRENT_DATE - 30),
SUM(version_downloads.downloads) FILTER (WHERE version_downloads.date > CURRENT_DATE - 7)
FROM version_downloads
INNER JOIN versions
ON version_downloads.version_id = versions.id
WHERE version_downloads.date > CURRENT_DATE - 90
GROUP BY crate_id;
CREATE UNIQUE INDEX recent_crate_downloads_crate_id ON recent_crate_downloads (crate_id);
12 changes: 9 additions & 3 deletions src/controllers/krate/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ pub async fn find_crate(
cats.as_deref(),
false,
downloads,
recent_downloads,
recent_downloads.map(|(d, _, _)| d),
recent_downloads.map(|(_, m, _)| m),
recent_downloads.map(|(_, _, w)| w),
);

let encodable_versions = versions_publishers_and_audit_actions.map(|vpa| {
Expand Down Expand Up @@ -289,14 +291,18 @@ fn load_recent_downloads(
conn: &mut AsyncPgConnection,
crate_id: i32,
includes: bool,
) -> BoxFuture<'_, AppResult<Option<i64>>> {
) -> BoxFuture<'_, AppResult<Option<(i64, i64, i64)>>> {
if !includes {
return always_ready(|| Ok(None)).boxed();
}

let fut = recent_crate_downloads::table
.filter(recent_crate_downloads::crate_id.eq(crate_id))
.select(recent_crate_downloads::downloads)
.select((
recent_crate_downloads::downloads,
recent_crate_downloads::monthly,
recent_crate_downloads::weekly,
))
.get_result(conn);
async move { Ok(fut.await.optional()?) }.boxed()
}
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,8 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
false,
downloads,
None,
None,
None,
),
warnings,
}))
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ pub async fn list_crates(
record.exact_match,
record.downloads,
Some(record.recent_downloads.unwrap_or(0)),
None,
None,
)
})
.collect::<Vec<_>>();
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/krate/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ async fn update_inner(
false,
downloads,
recent_downloads,
None,
None,
);

Ok(Json(PatchResponse {
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ fn encode_crates(
false,
record.total_downloads,
record.recent_downloads,
None,
None,
))
})
.collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "1.0.0",
"max_version": "1.0.0",
"monthly_downloads": null,
"name": "foo_new",
"newest_version": "1.0.0",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "1.0.0",
"max_version": "1.0.0",
"monthly_downloads": null,
"name": "foo_new",
"newest_version": "1.0.0",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "2.0.0",
"max_version": "2.0.0",
"monthly_downloads": null,
"name": "foo_twice",
"newest_version": "2.0.0",
"num_versions": 2,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "2.0.0",
"max_version": "2.0.0",
"monthly_downloads": null,
"name": "foo_twice",
"newest_version": "0.99.0",
"num_versions": 2,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": null,
"max_version": "0.0.0-pre",
"monthly_downloads": null,
"name": "foo_weird",
"newest_version": "0.0.0-pre",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "1.0.0",
"max_version": "1.0.0",
"monthly_downloads": null,
"name": "foo_new",
"newest_version": "1.0.0",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "1.0.0+foo",
"max_version": "1.0.0+foo",
"monthly_downloads": null,
"name": "foo",
"newest_version": "1.0.0+foo",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": null,
"max_version": "1.0.0-beta.1",
"monthly_downloads": null,
"name": "foo",
"newest_version": "1.0.0-beta.1",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "1.0.0+foo",
"max_version": "1.0.0+foo",
"monthly_downloads": null,
"name": "foo",
"newest_version": "1.0.0+foo",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "1.0.0",
"max_version": "1.0.0",
"monthly_downloads": null,
"name": "foo_good_cat",
"newest_version": "1.0.0",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: response.json()
},
"max_stable_version": "1.0.0",
"max_version": "1.0.0",
"monthly_downloads": null,
"name": "foo",
"newest_version": "1.0.0",
"num_versions": 1,
Expand All @@ -33,6 +34,7 @@ expression: response.json()
"trustpub_only": false,
"updated_at": "[datetime]",
"versions": null,
"weekly_downloads": null,
"yanked": false
},
"warnings": {
Expand Down
Loading