Skip to content

controllers/user/other: Read downloads from crate_downloads table #8250

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
Mar 6, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ aws-sdk-sqs = "=1.15.0"
axum = { version = "=0.7.4", features = ["macros", "matched-path"] }
axum-extra = { version = "=0.9.2", features = ["cookie-signed", "typed-header"] }
base64 = "=0.22.0"
bigdecimal = "=0.4.3"
bigdecimal = { version = "=0.4.3", features = ["serde"] }
cargo-manifest = "=0.13.0"
crates_io_cdn_logs = { path = "crates_io_cdn_logs" }
crates_io_env_vars = { path = "crates_io_env_vars" }
Expand Down
11 changes: 7 additions & 4 deletions src/controllers/user/other.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::controllers::frontend_prelude::*;
use bigdecimal::{BigDecimal, ToPrimitive};

use crate::models::{CrateOwner, OwnerKind, User};
use crate::schema::{crate_owners, crates, users};
use crate::schema::{crate_downloads, crate_owners, crates, users};
use crate::sql::lower;
use crate::views::EncodablePublicUser;

Expand Down Expand Up @@ -29,11 +30,13 @@ pub async fn stats(state: AppState, Path(user_id): Path<i32>) -> AppResult<Json<

let conn = &mut *state.db_read_prefer_primary()?;

let data: i64 = CrateOwner::by_owner_kind(OwnerKind::User)
let data = CrateOwner::by_owner_kind(OwnerKind::User)
.inner_join(crates::table)
.inner_join(crate_downloads::table.on(crates::id.eq(crate_downloads::crate_id)))
.filter(crate_owners::owner_id.eq(user_id))
.select(sum(crates::downloads))
.first::<Option<i64>>(conn)?
.select(sum(crate_downloads::downloads))
.first::<Option<BigDecimal>>(conn)?
.map(|d| d.to_u64().unwrap_or(u64::MAX))
.unwrap_or(0);
Comment on lines -32 to 40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leaving information here. Due to the fact that the sum function over bigint columns in PostgreSQL returns numeric (sum ( bigint ) → numeric), I believe this is the reason we use BigDecimal here instead of i64.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, exactly. Diesel has the same behavior built into the typesystem, which is why I had to change it like this


Ok(Json(json!({ "total_downloads": data })))
Expand Down
38 changes: 21 additions & 17 deletions src/tests/routes/users/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,37 @@ struct UserStats {
fn user_total_downloads() {
use crate::builders::CrateBuilder;
use crate::util::{RequestHelper, TestApp};
use diesel::{update, RunQueryDsl};
use crates_io::schema::crate_downloads;
use diesel::prelude::*;
use diesel::{update, QueryDsl, RunQueryDsl};

let (app, anon, user) = TestApp::init().with_user();
let user = user.as_model();
let another_user = app.db_new_user("bar");
let another_user = another_user.as_model();

app.db(|conn| {
let mut krate = CrateBuilder::new("foo_krate1", user.id).expect_build(conn);
krate.downloads = 10;
update(&krate).set(&krate).execute(conn).unwrap();

let mut krate2 = CrateBuilder::new("foo_krate2", user.id).expect_build(conn);
krate2.downloads = 20;
update(&krate2).set(&krate2).execute(conn).unwrap();

let mut another_krate = CrateBuilder::new("bar_krate1", another_user.id).expect_build(conn);
another_krate.downloads = 2;
update(&another_krate)
.set(&another_krate)
let krate = CrateBuilder::new("foo_krate1", user.id).expect_build(conn);
update(crate_downloads::table.filter(crate_downloads::crate_id.eq(krate.id)))
.set(crate_downloads::downloads.eq(10))
.execute(conn)
.unwrap();

let mut no_longer_my_krate = CrateBuilder::new("nacho", user.id).expect_build(conn);
no_longer_my_krate.downloads = 5;
update(&no_longer_my_krate)
.set(&no_longer_my_krate)
let krate2 = CrateBuilder::new("foo_krate2", user.id).expect_build(conn);
update(crate_downloads::table.filter(crate_downloads::crate_id.eq(krate2.id)))
.set(crate_downloads::downloads.eq(20))
.execute(conn)
.unwrap();

let another_krate = CrateBuilder::new("bar_krate1", another_user.id).expect_build(conn);
update(crate_downloads::table.filter(crate_downloads::crate_id.eq(another_krate.id)))
.set(crate_downloads::downloads.eq(2))
.execute(conn)
.unwrap();

let no_longer_my_krate = CrateBuilder::new("nacho", user.id).expect_build(conn);
update(crate_downloads::table.filter(crate_downloads::crate_id.eq(no_longer_my_krate.id)))
.set(crate_downloads::downloads.eq(5))
.execute(conn)
.unwrap();
no_longer_my_krate
Expand Down