Merge remote-tracking branch 'origin/dev' into dev
Some checks are pending
Create docker images / Build, push, and deploy (push) Waiting to run
Node.js CI / build (postgresql, 18.18, 10) (push) Waiting to run

This commit is contained in:
Mike Cao 2025-09-30 16:18:48 -07:00
commit 581ddc0233
7 changed files with 45 additions and 103 deletions

View file

@ -7,6 +7,7 @@ export interface RevenuParameters {
startDate: Date;
endDate: Date;
unit: string;
timezone: string;
currency: string;
}
@ -14,12 +15,6 @@ export interface RevenueResult {
chart: { x: string; t: string; y: number }[];
country: { name: string; value: number }[];
total: { sum: number; count: number; average: number; unique_count: number };
table: {
currency: string;
sum: number;
count: number;
unique_count: number;
}[];
}
export async function getRevenue(
@ -36,7 +31,7 @@ async function relationalQuery(
parameters: RevenuParameters,
filters: QueryFilters,
): Promise<RevenueResult> {
const { startDate, endDate, currency, unit = 'day' } = parameters;
const { startDate, endDate, unit = 'day', timezone = 'utc', currency } = parameters;
const { getDateSQL, rawQuery, parseFilters } = prisma;
const { queryParams, filterQuery, cohortQuery, joinSessionQuery } = parseFilters({
...filters,
@ -50,7 +45,7 @@ async function relationalQuery(
`
select
revenue.event_name x,
${getDateSQL('revenue.created_at', unit)} t,
${getDateSQL('revenue.created_at', unit, timezone)} t,
sum(revenue.revenue) y
from revenue
join website_event
@ -121,32 +116,7 @@ async function relationalQuery(
total.average = total.count > 0 ? total.sum / total.count : 0;
const table = await rawQuery(
`
select
revenue.currency,
sum(revenue.revenue) as sum,
count(distinct revenue.event_id) as count,
count(distinct revenue.session_id) as unique_count
from revenue
join website_event
on website_event.website_id = revenue.website_id
and website_event.session_id = revenue.session_id
and website_event.event_id = revenue.event_id
and website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
${cohortQuery}
${joinSessionQuery}
where revenue.website_id = {{websiteId::uuid}}
and revenue.created_at between {{startDate}} and {{endDate}}
${filterQuery}
group by revenue.currency
order by sum desc
`,
queryParams,
);
return { chart, country, table, total };
return { chart, country, total };
}
async function clickhouseQuery(
@ -154,7 +124,7 @@ async function clickhouseQuery(
parameters: RevenuParameters,
filters: QueryFilters,
): Promise<RevenueResult> {
const { startDate, endDate, unit = 'day', currency } = parameters;
const { startDate, endDate, unit = 'day', timezone = 'utc', currency } = parameters;
const { getDateSQL, rawQuery, parseFilters } = clickhouse;
const { filterQuery, cohortQuery, queryParams } = parseFilters({
...filters,
@ -174,7 +144,7 @@ async function clickhouseQuery(
`
select
website_revenue.event_name x,
${getDateSQL('website_revenue.created_at', unit)} t,
${getDateSQL('website_revenue.created_at', unit, timezone)} t,
sum(website_revenue.revenue) y
from website_revenue
join website_event
@ -250,36 +220,5 @@ async function clickhouseQuery(
total.average = total.count > 0 ? total.sum / total.count : 0;
const table = await rawQuery<
{
currency: string;
sum: number;
count: number;
unique_count: number;
}[]
>(
`
select
website_revenue.currency,
sum(website_revenue.revenue) as sum,
uniqExact(website_revenue.event_id) as count,
uniqExact(website_revenue.session_id) as unique_count
from website_revenue
join website_event
on website_event.website_id = website_revenue.website_id
and website_event.session_id = website_revenue.session_id
and website_event.event_id = website_revenue.event_id
and website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
${cohortQuery}
where website_revenue.website_id = {websiteId:UUID}
and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}
${filterQuery}
group by website_revenue.currency
order by sum desc
`,
queryParams,
);
return { chart, country, table, total };
return { chart, country, total };
}