downloads route (#713)

This commit is contained in:
Wyatt Verchere
2023-09-20 08:11:09 -07:00
committed by GitHub
parent 4bf030993a
commit 3380f4d11c
2 changed files with 130 additions and 0 deletions

View File

@@ -29,6 +29,13 @@ pub struct ReturnViews {
pub total_views: u64,
}
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnDownloads {
pub time: u64,
pub id: u64,
pub total_downloads: u64,
}
// Only one of project_id or version_id should be used
// Fetches playtimes as a Vec of ReturnPlaytimes
pub async fn fetch_playtimes(
@@ -125,6 +132,53 @@ pub async fn fetch_views(
Ok(query.fetch_all().await?)
}
// Fetches downloads as a Vec of ReturnDownloads
pub async fn fetch_downloads(
projects: Option<Vec<ProjectId>>,
versions: Option<Vec<VersionId>>,
start_date: NaiveDate,
end_date: NaiveDate,
resolution_minutes: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnDownloads>, ApiError> {
let project_or_version = if projects.is_some() && versions.is_none() {
"project_id"
} else if versions.is_some() {
"version_id"
} else {
return Err(ApiError::InvalidInput(
"Only one of 'project_id' or 'version_id' should be used.".to_string(),
));
};
let mut query = client
.query(&format!(
"
SELECT
toYYYYMMDDhhmmss((toStartOfInterval(recorded, toIntervalMinute(?)) AS time)),
{project_or_version},
count(id) AS total_downloads
FROM downloads
WHERE time >= toDate(?) AND time <= toDate(?)
AND {project_or_version} IN ?
GROUP BY
time,
{project_or_version}
"
))
.bind(resolution_minutes)
.bind(start_date)
.bind(end_date);
if projects.is_some() {
query = query.bind(projects.unwrap().iter().map(|x| x.0).collect::<Vec<_>>());
} else if versions.is_some() {
query = query.bind(versions.unwrap().iter().map(|x| x.0).collect::<Vec<_>>());
}
Ok(query.fetch_all().await?)
}
// Fetches countries as a Vec of ReturnCountry
pub async fn fetch_countries(
projects: Option<Vec<ProjectId>>,