Skip to content

Commit

Permalink
update avg aggregate for revenue report
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscao633 committed Oct 15, 2024
1 parent 9d0da45 commit 065499a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
22 changes: 11 additions & 11 deletions src/app/(main)/console/TestConsole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,35 @@ export function TestConsole({ websiteId }: { websiteId: string }) {
referrer: 'https://www.google.com',
}));
window['umami'].track('checkout-cart', {
revenue: parseFloat((Math.random() * 100000).toFixed(2)),
currency: 'SHIBA',
revenue: parseFloat((Math.random() * 1000).toFixed(2)),
currency: 'USD',
});
window['umami'].track('affiliate-link', {
revenue: parseFloat((Math.random() * 100000).toFixed(2)),
currency: 'ETH',
revenue: parseFloat((Math.random() * 1000).toFixed(2)),
currency: 'USD',
});
window['umami'].track('promotion-link', {
revenue: parseFloat((Math.random() * 100000).toFixed(2)),
revenue: parseFloat((Math.random() * 1000).toFixed(2)),
currency: 'USD',
});
window['umami'].track('checkout-cart', {
revenue: parseFloat((Math.random() * 100000).toFixed(2)),
revenue: parseFloat((Math.random() * 1000).toFixed(2)),
currency: 'EUR',
});
window['umami'].track('promotion-link', {
revenue: parseFloat((Math.random() * 100000).toFixed(2)),
revenue: parseFloat((Math.random() * 1000).toFixed(2)),
currency: 'EUR',
});
window['umami'].track('affiliate-link', {
item1: {
productIdentity: 'ABC424',
revenue: parseFloat((Math.random() * 100000).toFixed(2)),
currency: 'MXN',
revenue: parseFloat((Math.random() * 10000).toFixed(2)),
currency: 'JPY',
},
item2: {
productIdentity: 'ZYW684',
revenue: parseFloat((Math.random() * 100000).toFixed(2)),
currency: 'MXN',
revenue: parseFloat((Math.random() * 10000).toFixed(2)),
currency: 'JPY',
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/reports/revenue/RevenueTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function RevenueTable() {
{row => formatLongCurrency(row.sum, row.currency)}
</GridColumn>
<GridColumn name="currency" label={formatMessage(labels.average)} alignment="end">
{row => formatLongCurrency(row.avg, row.currency)}
{row => formatLongCurrency(row.count ? row.sum / row.count : 0, row.currency)}
</GridColumn>
<GridColumn name="currency" label={formatMessage(labels.transactions)} alignment="end">
{row => row.count}
Expand Down
4 changes: 2 additions & 2 deletions src/app/(main)/reports/revenue/RevenueView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function RevenueView({ isLoading }: RevenueViewProps) {
const metricData = useMemo(() => {
if (!data) return [];

const { sum, avg, count, unique_count } = data.total;
const { sum, count, unique_count } = data.total;

return [
{
Expand All @@ -96,7 +96,7 @@ export function RevenueView({ isLoading }: RevenueViewProps) {
formatValue: n => formatLongCurrency(n, currency),
},
{
value: avg,
value: count ? sum / count : 0,
label: formatMessage(labels.average),
formatValue: n => formatLongCurrency(n, currency),
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ export function formatLongCurrency(value: number, currency: string, locale = 'en
return `${formatCurrency(n / 1000, currency, locale)}k`;
}

return formatCurrency(n / 1000, currency, locale);
return formatCurrency(n, currency, locale);
}
10 changes: 2 additions & 8 deletions src/queries/analytics/reports/getRevenue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ async function relationalQuery(
): Promise<{
chart: { x: string; t: string; y: number }[];
country: { name: string; value: number }[];
total: { sum: number; avg: number; count: number; unique_count: number };
total: { sum: number; count: number; unique_count: number };
table: {
currency: string;
sum: number;
avg: number;
count: number;
unique_count: number;
}[];
Expand Down Expand Up @@ -96,7 +95,6 @@ async function relationalQuery(
`
select
sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) as sum,
avg(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) as avg,
count(distinct event_id) as count,
count(distinct session_id) as unique_count
from event_data ed
Expand All @@ -119,7 +117,6 @@ async function relationalQuery(
select
c.currency,
sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) as sum,
avg(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) as avg,
count(distinct ed.website_event_id) as count,
count(distinct we.session_id) as unique_count
from event_data ed
Expand Down Expand Up @@ -153,11 +150,10 @@ async function clickhouseQuery(
): Promise<{
chart: { x: string; t: string; y: number }[];
country: { name: string; value: number }[];
total: { sum: number; avg: number; count: number; unique_count: number };
total: { sum: number; count: number; unique_count: number };
table: {
currency: string;
sum: number;
avg: number;
count: number;
unique_count: number;
}[];
Expand Down Expand Up @@ -230,7 +226,6 @@ async function clickhouseQuery(
`
select
sum(coalesce(toDecimal64(number_value, 2), toDecimal64(string_value, 2))) as sum,
avg(coalesce(toDecimal64(number_value, 2), toDecimal64(string_value, 2))) as avg,
uniqExact(event_id) as count,
uniqExact(session_id) as unique_count
from event_data
Expand Down Expand Up @@ -259,7 +254,6 @@ async function clickhouseQuery(
select
c.currency,
sum(coalesce(toDecimal64(ed.number_value, 2), toDecimal64(ed.string_value, 2))) as sum,
avg(coalesce(toDecimal64(ed.number_value, 2), toDecimal64(ed.string_value, 2))) as avg,
uniqExact(ed.event_id) as count,
uniqExact(ed.session_id) as unique_count
from event_data ed
Expand Down

0 comments on commit 065499a

Please sign in to comment.