Skip to content

Add query to display recent_downloads on krates::show #1023

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
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
19 changes: 11 additions & 8 deletions src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,6 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
use diesel::types::{Bool, BigInt, Nullable};
use diesel::expression::functions::date_and_time::{now, date};
use diesel::expression::sql_literal::sql;
use diesel::query_source::joins::LeftOuter;

let conn = req.db_conn()?;
let (offset, limit) = req.pagination(10, 100)?;
Expand All @@ -585,13 +584,11 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
let recent_downloads = sql::<Nullable<BigInt>>("SUM(crate_downloads.downloads)");

let mut query = crates::table
.join(
crate_downloads::table,
LeftOuter,
.left_join(crate_downloads::table.on(
crates::id.eq(crate_downloads::crate_id).and(
crate_downloads::date.gt(date(now - 90.days())),
),
)
))
.group_by(crates::id)
.select((
ALL_COLUMNS,
Expand Down Expand Up @@ -775,7 +772,7 @@ pub fn summary(req: &mut Request) -> CargoResult<Response> {
.map(|versions| Version::max(versions.into_iter().map(|v| v.num)))
.zip(krates)
.map(|(max_version, krate)| {
Ok(krate.minimal_encodable(max_version, None, false, Some(0)))
Ok(krate.minimal_encodable(max_version, None, false, None))
})
.collect()
};
Expand Down Expand Up @@ -833,6 +830,8 @@ pub fn summary(req: &mut Request) -> CargoResult<Response> {

/// Handles the `GET /crates/:crate_id` route.
pub fn show(req: &mut Request) -> CargoResult<Response> {
use diesel::expression::dsl::*;

let name = &req.params()["crate_id"];
let conn = req.db_conn()?;
let krate = Crate::by_name(name).first::<Crate>(&*conn)?;
Expand All @@ -849,6 +848,10 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
.inner_join(categories::table)
.select(categories::all_columns)
.load(&*conn)?;
let recent_downloads = CrateDownload::belonging_to(&krate)
.filter(crate_downloads::date.gt(date(now - 90.days())))
.select(sum(crate_downloads::downloads))
.get_result(&*conn)?;

let badges = badges::table.filter(badges::crate_id.eq(krate.id)).load(
&*conn,
Expand All @@ -872,7 +875,7 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
Some(&cats),
Some(badges),
false,
Some(0),
recent_downloads,
),
versions: versions
.into_iter()
Expand Down Expand Up @@ -1037,7 +1040,7 @@ pub fn new(req: &mut Request) -> CargoResult<Response> {
warnings: Warnings<'a>,
}
Ok(req.json(&R {
krate: krate.minimal_encodable(max_version, None, false, Some(0)),
krate: krate.minimal_encodable(max_version, None, false, None),
warnings: warnings,
}))
})
Expand Down
3 changes: 3 additions & 0 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ fn show() {
.version(::VersionBuilder::new("0.5.0"))
.version(::VersionBuilder::new("0.5.1"))
.keyword("kw1")
.downloads(20)
.recent_downloads(10)
.expect_build(&conn);
}

Expand All @@ -415,6 +417,7 @@ fn show() {
assert_eq!(json.krate.homepage, krate.homepage);
assert_eq!(json.krate.documentation, krate.documentation);
assert_eq!(json.krate.keywords, Some(vec!["kw1".into()]));
assert_eq!(json.krate.recent_downloads, Some(10));
let versions = json.krate.versions.as_ref().unwrap();
assert_eq!(versions.len(), 3);
assert_eq!(json.versions.len(), 3);
Expand Down