Optimize country analytics (#782)

This commit is contained in:
Geometrically
2023-12-04 19:45:17 -07:00
committed by GitHub
parent 4630d175d7
commit e76b6c3bde
2 changed files with 48 additions and 62 deletions

View File

@@ -5,32 +5,17 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnPlaytimes {
pub struct ReturnIntervals {
pub time: u32,
pub id: u64,
pub total_seconds: u64,
pub total: u64,
}
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnCountry {
pub country: String,
pub id: u64,
pub total_views: u64,
pub total_downloads: u64,
}
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnViews {
pub time: u32,
pub id: u64,
pub total_views: u64,
}
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnDownloads {
pub time: u32,
pub id: u64,
pub total_downloads: u64,
pub total: u64,
}
// Only one of project_id or version_id should be used
@@ -41,14 +26,14 @@ pub async fn fetch_playtimes(
end_date: DateTime<Utc>,
resolution_minute: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnPlaytimes>, ApiError> {
) -> Result<Vec<ReturnIntervals>, ApiError> {
let query = client
.query(
"
SELECT
toUnixTimestamp(toStartOfInterval(recorded, toIntervalMinute(?))) AS time,
project_id AS id,
SUM(seconds) AS total_seconds
SUM(seconds) AS total
FROM playtime
WHERE recorded BETWEEN ? AND ?
AND project_id IN ?
@@ -72,14 +57,14 @@ pub async fn fetch_views(
end_date: DateTime<Utc>,
resolution_minutes: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnViews>, ApiError> {
) -> Result<Vec<ReturnIntervals>, ApiError> {
let query = client
.query(
"
SELECT
toUnixTimestamp(toStartOfInterval(recorded, toIntervalMinute(?))) AS time,
project_id AS id,
count(1) AS total_views
count(1) AS total
FROM views
WHERE recorded BETWEEN ? AND ?
AND project_id IN ?
@@ -102,14 +87,14 @@ pub async fn fetch_downloads(
end_date: DateTime<Utc>,
resolution_minutes: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnDownloads>, ApiError> {
) -> Result<Vec<ReturnIntervals>, ApiError> {
let query = client
.query(
"
SELECT
toUnixTimestamp(toStartOfInterval(recorded, toIntervalMinute(?))) AS time,
project_id as id,
count(1) AS total_downloads
count(1) AS total
FROM downloads
WHERE recorded BETWEEN ? AND ?
AND project_id IN ?
@@ -124,50 +109,51 @@ pub async fn fetch_downloads(
Ok(query.fetch_all().await?)
}
// Fetches countries as a Vec of ReturnCountry
pub async fn fetch_countries(
pub async fn fetch_countries_downloads(
projects: Vec<ProjectId>,
start_date: DateTime<Utc>,
end_date: DateTime<Utc>,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnCountry>, ApiError> {
let query = client.query(
"
WITH view_grouping AS (
"
SELECT
country,
project_id,
count(1) AS total_views
FROM views
WHERE recorded BETWEEN ? AND ?
GROUP BY
country,
project_id
),
download_grouping AS (
SELECT
country,
project_id,
count(1) AS total_downloads
count(1) AS total
FROM downloads
WHERE recorded BETWEEN ? AND ?
WHERE recorded BETWEEN ? AND ? AND project_id IN ?
GROUP BY
country,
project_id
)
SELECT
v.country,
v.project_id,
v.total_views,
d.total_downloads
FROM view_grouping AS v
LEFT JOIN download_grouping AS d ON (v.country = d.country) AND (v.project_id = d.project_id)
WHERE project_id IN ?
project_id;
"
)
.bind(start_date.timestamp())
.bind(end_date.timestamp())
)
.bind(start_date.timestamp())
.bind(end_date.timestamp())
.bind(projects.iter().map(|x| x.0).collect::<Vec<_>>());
Ok(query.fetch_all().await?)
}
pub async fn fetch_countries_views(
projects: Vec<ProjectId>,
start_date: DateTime<Utc>,
end_date: DateTime<Utc>,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnCountry>, ApiError> {
let query = client.query(
"
SELECT
country,
project_id,
count(1) AS total
FROM views
WHERE recorded BETWEEN ? AND ? AND project_id IN ?
GROUP BY
country,
project_id;
"
)
.bind(start_date.timestamp())
.bind(end_date.timestamp())
.bind(projects.iter().map(|x| x.0).collect::<Vec<_>>());