Fix analytics routes + add revenue route (#734)

This commit is contained in:
Geometrically
2023-10-19 09:42:49 -07:00
committed by GitHub
parent 9d0e762f36
commit 9a8f3d7bad
3 changed files with 196 additions and 98 deletions

View File

@@ -4,12 +4,12 @@ use crate::{
models::ids::{ProjectId, VersionId},
routes::ApiError,
};
use chrono::NaiveDate;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnPlaytimes {
pub time: u64,
pub time: u32,
pub id: u64,
pub total_seconds: u64,
}
@@ -24,14 +24,14 @@ pub struct ReturnCountry {
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnViews {
pub time: u64,
pub time: u32,
pub id: u64,
pub total_views: u64,
}
#[derive(clickhouse::Row, Serialize, Deserialize, Clone, Debug)]
pub struct ReturnDownloads {
pub time: u64,
pub time: u32,
pub id: u64,
pub total_downloads: u64,
}
@@ -41,8 +41,8 @@ pub struct ReturnDownloads {
pub async fn fetch_playtimes(
projects: Option<Vec<ProjectId>>,
versions: Option<Vec<VersionId>>,
start_date: NaiveDate,
end_date: NaiveDate,
start_date: DateTime<Utc>,
end_date: DateTime<Utc>,
resolution_minute: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnPlaytimes>, ApiError> {
@@ -60,21 +60,20 @@ pub async fn fetch_playtimes(
.query(&format!(
"
SELECT
toYYYYMMDDhhmmss(toStartOfInterval(recorded, toIntervalMinute(?)) AS time),
{project_or_version},
toUnixTimestamp(toStartOfInterval(recorded, toIntervalMinute(?))) AS time,
{project_or_version} AS id,
SUM(seconds) AS total_seconds
FROM playtime
WHERE time >= toDate(?) AND time <= toDate(?)
WHERE recorded BETWEEN ? AND ?
AND {project_or_version} IN ?
GROUP BY
time,
project_id,
{project_or_version}
"
))
.bind(resolution_minute)
.bind(start_date)
.bind(end_date);
.bind(start_date.timestamp())
.bind(end_date.timestamp());
if let Some(projects) = projects {
query = query.bind(projects.iter().map(|x| x.0).collect::<Vec<_>>());
@@ -89,8 +88,8 @@ pub async fn fetch_playtimes(
pub async fn fetch_views(
projects: Option<Vec<ProjectId>>,
versions: Option<Vec<VersionId>>,
start_date: NaiveDate,
end_date: NaiveDate,
start_date: DateTime<Utc>,
end_date: DateTime<Utc>,
resolution_minutes: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnViews>, ApiError> {
@@ -108,20 +107,19 @@ pub async fn fetch_views(
.query(&format!(
"
SELECT
toYYYYMMDDhhmmss((toStartOfInterval(recorded, toIntervalMinute(?)) AS time)),
{project_or_version},
count(id) AS total_views
toUnixTimestamp(toStartOfInterval(recorded, toIntervalMinute(?))) AS time,
{project_or_version} AS id,
count(views.id) AS total_views
FROM views
WHERE time >= toDate(?) AND time <= toDate(?)
AND {project_or_version} IN ?
WHERE recorded BETWEEN ? AND ?
AND {project_or_version} IN ?
GROUP BY
time,
{project_or_version}
"
time, {project_or_version}
"
))
.bind(resolution_minutes)
.bind(start_date)
.bind(end_date);
.bind(start_date.timestamp())
.bind(end_date.timestamp());
if let Some(projects) = projects {
query = query.bind(projects.iter().map(|x| x.0).collect::<Vec<_>>());
@@ -136,8 +134,8 @@ pub async fn fetch_views(
pub async fn fetch_downloads(
projects: Option<Vec<ProjectId>>,
versions: Option<Vec<VersionId>>,
start_date: NaiveDate,
end_date: NaiveDate,
start_date: DateTime<Utc>,
end_date: DateTime<Utc>,
resolution_minutes: u32,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnDownloads>, ApiError> {
@@ -155,20 +153,18 @@ pub async fn fetch_downloads(
.query(&format!(
"
SELECT
toYYYYMMDDhhmmss((toStartOfInterval(recorded, toIntervalMinute(?)) AS time)),
{project_or_version},
count(id) AS total_downloads
toUnixTimestamp(toStartOfInterval(recorded, toIntervalMinute(?))) AS time,
{project_or_version} as id,
count(downloads.id) AS total_downloads
FROM downloads
WHERE time >= toDate(?) AND time <= toDate(?)
AND {project_or_version} IN ?
GROUP BY
time,
{project_or_version}
"
WHERE recorded BETWEEN ? AND ?
AND {project_or_version} IN ?
GROUP BY time, {project_or_version}
"
))
.bind(resolution_minutes)
.bind(start_date)
.bind(end_date);
.bind(start_date.timestamp())
.bind(end_date.timestamp());
if let Some(projects) = projects {
query = query.bind(projects.iter().map(|x| x.0).collect::<Vec<_>>());
@@ -183,8 +179,8 @@ pub async fn fetch_downloads(
pub async fn fetch_countries(
projects: Option<Vec<ProjectId>>,
versions: Option<Vec<VersionId>>,
start_date: NaiveDate,
end_date: NaiveDate,
start_date: DateTime<Utc>,
end_date: DateTime<Utc>,
client: Arc<clickhouse::Client>,
) -> Result<Vec<ReturnCountry>, ApiError> {
let project_or_version = if projects.is_some() && versions.is_none() {
@@ -205,7 +201,7 @@ pub async fn fetch_countries(
{project_or_version},
count(id) AS total_views
FROM views
WHERE toYYYYMMDDhhmmss(recorded) >= toYYYYMMDDhhmmss(toDate(?)) AND toYYYYMMDDhhmmss(recorded) <= toYYYYMMDDhhmmss(toDate(?))
WHERE recorded BETWEEN ? AND ?
GROUP BY
country,
{project_or_version}
@@ -216,7 +212,7 @@ pub async fn fetch_countries(
{project_or_version},
count(id) AS total_downloads
FROM downloads
WHERE toYYYYMMDDhhmmss(recorded) >= toYYYYMMDDhhmmss(toDate(?)) AND toYYYYMMDDhhmmss(recorded) <= toYYYYMMDDhhmmss(toDate(?))
WHERE recorded BETWEEN ? AND ?
GROUP BY
country,
{project_or_version}
@@ -231,7 +227,7 @@ pub async fn fetch_countries(
LEFT JOIN download_grouping AS d ON (v.country = d.country) AND (v.{project_or_version} = d.{project_or_version})
WHERE {project_or_version} IN ?
"
)).bind(start_date).bind(end_date).bind(start_date).bind(end_date);
)).bind(start_date.timestamp()).bind(end_date.timestamp()).bind(start_date.timestamp()).bind(end_date.timestamp());
if let Some(projects) = projects {
query = query.bind(projects.iter().map(|x| x.0).collect::<Vec<_>>());