Skip to content

Commit 348759b

Browse files
committed
add total_mod_count field to /v1/stats and fix total_mod_downloads
1 parent 437d3c2 commit 348759b

6 files changed

+48
-29
lines changed

.sqlx/query-96c66a14f97bea3f82b28325d41ed5a299bd78e8f419388be04ac89e70603286.json

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-b79d69323170f1b7396fa5cb294d038bfde1d49cff29a903d1856e2121e522a7.json

-20
This file was deleted.

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "geode-index"
3-
version = "0.27.0"
3+
version = "0.28.0"
44
edition = "2021"
55

66
[dependencies]

src/types/models/mod_entity.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,15 @@ pub enum CheckExistingResult {
103103
ExistsWithRejected
104104
}
105105

106+
pub struct ModStats {
107+
pub total_count: i64,
108+
pub total_downloads: i64,
109+
}
110+
106111
impl Mod {
107-
pub async fn get_total_count(pool: &mut PgConnection) -> Result<i64, ApiError> {
108-
match sqlx::query_scalar!("
109-
SELECT COUNT(DISTINCT m.id)
112+
pub async fn get_stats(pool: &mut PgConnection) -> Result<ModStats, ApiError> {
113+
match sqlx::query!("
114+
SELECT COUNT(DISTINCT m.id), SUM(mv.download_count)
110115
FROM mods m
111116
INNER JOIN mod_versions mv ON mv.mod_id = m.id
112117
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
@@ -119,7 +124,12 @@ impl Mod {
119124
log::error!("{}", e);
120125
Err(ApiError::DbError)
121126
}
122-
Ok(r) => Ok(r.flatten().unwrap_or(0)),
127+
Ok(r) => if let Some((Some(total_count), Some(total_downloads))) = r.map(|o| (o.count, o.sum)) {
128+
Ok(ModStats { total_count, total_downloads })
129+
}
130+
else {
131+
Ok(ModStats { total_count: 0, total_downloads: 0 })
132+
}
123133
}
124134
}
125135

src/types/models/stats.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,29 @@ struct GithubReleaseWithAssets {
2424
#[derive(Deserialize, Serialize, Clone, Debug)]
2525
pub struct Stats {
2626
pub total_geode_downloads: i64,
27+
pub total_mod_count: i64,
2728
pub total_mod_downloads: i64,
2829
pub total_registered_developers: i64,
2930
}
3031

3132
impl Stats {
3233
pub async fn get_cached(pool: &mut PgConnection) -> Result<Stats, ApiError> {
34+
let mod_stats = Mod::get_stats(&mut *pool).await?;
3335
Ok(Stats {
34-
total_mod_downloads: Mod::get_total_count(&mut *pool).await?,
36+
total_mod_count: mod_stats.total_count,
37+
total_mod_downloads: mod_stats.total_downloads,
3538
total_registered_developers: Developer::get_total_count(&mut *pool).await?,
3639
total_geode_downloads: Self::get_latest_github_release_download_count(&mut *pool).await?,
3740
})
3841
}
3942

4043
async fn get_latest_github_release_download_count(pool: &mut PgConnection) -> Result<i64, ApiError> {
4144
// If release stats were fetched less than a day ago, just use cached stats
42-
if let Some((cache_time, total_download_count)) = sqlx::query!("
45+
if let Ok((cache_time, total_download_count)) = sqlx::query!("
4346
SELECT s.checked_at, s.total_download_count
4447
FROM github_loader_release_stats s
4548
ORDER BY s.checked_at DESC
46-
").fetch_one(&mut *pool).await.map(|d| (d.checked_at, d.total_download_count)).ok() {
49+
").fetch_one(&mut *pool).await.map(|d| (d.checked_at, d.total_download_count)) {
4750
if Utc::now().signed_duration_since(cache_time).num_days() < 1 {
4851
return Ok(total_download_count);
4952
}

0 commit comments

Comments
 (0)