You've already forked AstralRinth
forked from didirus/AstralRinth
downloads route (#713)
This commit is contained in:
@@ -29,6 +29,13 @@ pub struct ReturnViews {
|
|||||||
pub total_views: u64,
|
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
|
// Only one of project_id or version_id should be used
|
||||||
// Fetches playtimes as a Vec of ReturnPlaytimes
|
// Fetches playtimes as a Vec of ReturnPlaytimes
|
||||||
pub async fn fetch_playtimes(
|
pub async fn fetch_playtimes(
|
||||||
@@ -125,6 +132,53 @@ pub async fn fetch_views(
|
|||||||
Ok(query.fetch_all().await?)
|
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
|
// Fetches countries as a Vec of ReturnCountry
|
||||||
pub async fn fetch_countries(
|
pub async fn fetch_countries(
|
||||||
projects: Option<Vec<ProjectId>>,
|
projects: Option<Vec<ProjectId>>,
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
|
|||||||
web::scope("analytics")
|
web::scope("analytics")
|
||||||
.service(playtimes_get)
|
.service(playtimes_get)
|
||||||
.service(views_get)
|
.service(views_get)
|
||||||
|
.service(downloads_get)
|
||||||
.service(countries_downloads_get)
|
.service(countries_downloads_get)
|
||||||
.service(countries_views_get),
|
.service(countries_views_get),
|
||||||
);
|
);
|
||||||
@@ -204,6 +205,81 @@ pub async fn views_get(
|
|||||||
Ok(HttpResponse::Ok().json(hm))
|
Ok(HttpResponse::Ok().json(hm))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get download data for a set of projects or versions
|
||||||
|
/// Data is returned as a hashmap of project/version ids to a hashmap of days to downloads
|
||||||
|
/// eg:
|
||||||
|
/// {
|
||||||
|
/// "4N1tEhnO": {
|
||||||
|
/// "20230824": 32
|
||||||
|
/// }
|
||||||
|
///}
|
||||||
|
/// Either a list of project_ids or version_ids can be used, but not both. Unauthorized projects/versions will be filtered out.
|
||||||
|
#[get("downloads")]
|
||||||
|
pub async fn downloads_get(
|
||||||
|
req: HttpRequest,
|
||||||
|
clickhouse: web::Data<clickhouse::Client>,
|
||||||
|
data: web::Json<GetData>,
|
||||||
|
session_queue: web::Data<AuthQueue>,
|
||||||
|
pool: web::Data<PgPool>,
|
||||||
|
redis: web::Data<deadpool_redis::Pool>,
|
||||||
|
) -> Result<HttpResponse, ApiError> {
|
||||||
|
let user_option = get_user_from_headers(
|
||||||
|
&req,
|
||||||
|
&**pool,
|
||||||
|
&redis,
|
||||||
|
&session_queue,
|
||||||
|
Some(&[Scopes::ANALYTICS]),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map(|x| x.1)
|
||||||
|
.ok();
|
||||||
|
|
||||||
|
let project_ids = data.project_ids.clone();
|
||||||
|
let version_ids = data.version_ids.clone();
|
||||||
|
|
||||||
|
if project_ids.is_some() && version_ids.is_some() {
|
||||||
|
return Err(ApiError::InvalidInput(
|
||||||
|
"Only one of 'project_ids' or 'version_ids' should be used.".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let start_date = data
|
||||||
|
.start_date
|
||||||
|
.unwrap_or(Utc::now().naive_utc().date() - Duration::weeks(2));
|
||||||
|
let end_date = data.end_date.unwrap_or(Utc::now().naive_utc().date());
|
||||||
|
let resolution_minutes = data.resolution_minutes.unwrap_or(60 * 24);
|
||||||
|
|
||||||
|
// Convert String list to list of ProjectIds or VersionIds
|
||||||
|
// - Filter out unauthorized projects/versions
|
||||||
|
// - If no project_ids or version_ids are provided, we default to all projects the user has access to
|
||||||
|
let (project_ids, version_ids) =
|
||||||
|
filter_allowed_ids(project_ids, version_ids, user_option, &pool, &redis).await?;
|
||||||
|
|
||||||
|
// Get the downloads
|
||||||
|
let downloads = crate::clickhouse::fetch_downloads(
|
||||||
|
project_ids,
|
||||||
|
version_ids,
|
||||||
|
start_date,
|
||||||
|
end_date,
|
||||||
|
resolution_minutes,
|
||||||
|
clickhouse.into_inner(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut hm = HashMap::new();
|
||||||
|
for downloads in downloads {
|
||||||
|
let id_string = to_base62(downloads.id);
|
||||||
|
if !hm.contains_key(&id_string) {
|
||||||
|
hm.insert(id_string.clone(), HashMap::new());
|
||||||
|
}
|
||||||
|
if let Some(hm) = hm.get_mut(&id_string) {
|
||||||
|
hm.insert(downloads.time.to_string(), downloads.total_downloads);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok().json(hm))
|
||||||
|
}
|
||||||
|
|
||||||
/// Get country data for a set of projects or versions
|
/// Get country data for a set of projects or versions
|
||||||
/// Data is returned as a hashmap of project/version ids to a hashmap of coutnry to downloads.
|
/// Data is returned as a hashmap of project/version ids to a hashmap of coutnry to downloads.
|
||||||
/// Unknown countries are labeled "".
|
/// Unknown countries are labeled "".
|
||||||
|
|||||||
Reference in New Issue
Block a user