Skip to content

Commit

Permalink
feat: temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 6, 2025
1 parent da0fbe1 commit ba79ff1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ CREATE TABLE IF NOT EXISTS mods_download_count_snapshots(
FOREIGN KEY (mod_id)
REFERENCES mods(id)
ON DELETE CASCADE
)
);
14 changes: 10 additions & 4 deletions src/jobs/download_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ use sqlx::PgConnection;
pub async fn start(pool: &mut PgConnection) -> Result<(), String> {
// update mod_versions counts
if let Err(e) = sqlx::query!(
"UPDATE mod_versions mv SET download_count = (
"UPDATE mod_versions mv
USING
SET download_count = COALESCE((
SELECT COUNT(DISTINCT md.ip) FROM mod_downloads md
WHERE md.mod_version_id = mv.id
), last_download_cache_refresh = now()
), 0) + COALESCE((
SELECT download_count FROM mod_versions_download_count_snapshots
WHERE mod_version_id = mv.id
ORDER BY id DESC
), 0), last_download_cache_refresh = now()
FROM mod_version_statuses mvs
WHERE mv.status_id = mvs.id AND mvs.status = 'accepted'"
)
Expand All @@ -18,12 +24,12 @@ pub async fn start(pool: &mut PgConnection) -> Result<(), String> {
}

if let Err(e) = sqlx::query!(
"UPDATE mods m SET download_count = (
"UPDATE mods m SET download_count = COALESCE((
SELECT COUNT(DISTINCT md.ip) FROM mod_downloads md
INNER JOIN mod_versions mv ON md.mod_version_id = mv.id
INNER JOIN mod_version_statuses mvs ON mvs.mod_version_id = mv.id
WHERE mv.mod_id = m.id AND mvs.status = 'accepted'
), last_download_cache_refresh = now()
), 0), last_download_cache_refresh = now()
WHERE m.id IN (
SELECT DISTINCT mv.mod_id FROM mod_versions mv
INNER JOIN mod_version_statuses mvs ON mv.status_id = mvs.id
Expand Down
21 changes: 17 additions & 4 deletions src/jobs/snapshot_downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@ use sqlx::PgConnection;
pub async fn start(conn: &mut PgConnection) -> Result<(), String> {
sqlx::query!(
"INSERT INTO mod_versions_download_count_snapshots (mod_version_id, download_count)
SELECT id as mod_version_id, download_count FROM mod_versions"
SELECT mv.id as mod_version_id, mv.download_count FROM mod_versions mv
WHERE mv.download_count <> 0"
)
.execute(&mut *conn)
.await
.map_err(|err| format!("{}", err))?;
.map_err(|err| format!("Query error: {}", err))?;

sqlx::query!(
"INSERT INTO mods_download_count_snapshots (mod_id, download_count)
SELECT id as mod_id, download_count FROM mods"
SELECT id as mod_id, download_count FROM mods WHERE download_count <> 0"
)
.execute(&mut *conn)
.await
.map_err(|err| format!("{}", err))?;
.map_err(|err| format!("Query error: {}", err))?;

// sqlx::query!(
// "DELETE FROM mod_downloads md
// USING mod_versions mv, mod_version_statuses mvs
// WHERE md.mod_version_id = mv.id
// AND mv.status_id = mvs.id
// AND mvs.status = 'accepted'
// AND md.time_downloaded < CURRENT_TIMESTAMP"
// )
// .execute(&mut *conn)
// .await
// .map_err(|err| format!("Query error: {}", err))?;

Ok(())
}
9 changes: 6 additions & 3 deletions src/types/models/download.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use sqlx::{types::ipnetwork::IpNetwork, PgConnection, Acquire};
use sqlx::{types::ipnetwork::IpNetwork, Acquire, PgConnection};

use crate::types::api::ApiError;

pub async fn downloaded_version(mod_version_id: i32, pool: &mut PgConnection) -> Result<bool, ApiError> {
pub async fn downloaded_version(
mod_version_id: i32,
pool: &mut PgConnection,
) -> Result<bool, ApiError> {
match sqlx::query!(
r#"
SELECT mod_version_id FROM mod_downloads md
Expand Down Expand Up @@ -95,7 +98,7 @@ pub async fn create_download(
tx.commit().await.or(Err(ApiError::TransactionError))?;

Ok((true, !downloaded_mod))
},
}
Err(e) => {
log::error!("{}", e);
Err(ApiError::InternalError)
Expand Down

0 comments on commit ba79ff1

Please sign in to comment.