Skip to content

Commit bf99068

Browse files
committed
Remove tables result set from getRevenue. Fix min/max date for revenuechart
1 parent 1b54ba3 commit bf99068

File tree

3 files changed

+26
-88
lines changed

3 files changed

+26
-88
lines changed

src/app/(main)/websites/[websiteId]/(reports)/revenue/Revenue.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
import { useState } from 'react';
2-
import { Grid, Row, Text } from '@umami/react-zen';
3-
import classNames from 'classnames';
4-
import { colord } from 'colord';
51
import { BarChart } from '@/components/charts/BarChart';
2+
import { LoadingPanel } from '@/components/common/LoadingPanel';
3+
import { Panel } from '@/components/common/Panel';
64
import { TypeIcon } from '@/components/common/TypeIcon';
75
import { useCountryNames, useLocale, useMessages, useResultQuery } from '@/components/hooks';
6+
import { CurrencySelect } from '@/components/input/CurrencySelect';
87
import { ListTable } from '@/components/metrics/ListTable';
98
import { MetricCard } from '@/components/metrics/MetricCard';
109
import { MetricsBar } from '@/components/metrics/MetricsBar';
1110
import { renderDateLabels } from '@/lib/charts';
1211
import { CHART_COLORS } from '@/lib/constants';
12+
import { generateTimeSeries } from '@/lib/date';
1313
import { formatLongCurrency, formatLongNumber } from '@/lib/format';
14-
import { useCallback, useMemo } from 'react';
15-
import { Panel } from '@/components/common/Panel';
16-
import { Column } from '@umami/react-zen';
17-
import { LoadingPanel } from '@/components/common/LoadingPanel';
18-
import { getMinimumUnit } from '@/lib/date';
19-
import { CurrencySelect } from '@/components/input/CurrencySelect';
14+
import { Column, Grid, Row, Text } from '@umami/react-zen';
15+
import classNames from 'classnames';
16+
import { colord } from 'colord';
17+
import { useCallback, useMemo, useState } from 'react';
2018

2119
export interface RevenueProps {
2220
websiteId: string;
23-
startDate: Date;
24-
endDate: Date;
21+
minDate: Date;
22+
maxDate: Date;
23+
unit: string;
2524
}
2625

27-
export function Revenue({ websiteId, startDate, endDate }: RevenueProps) {
26+
export function Revenue({ websiteId, minDate, maxDate, unit }: RevenueProps) {
2827
const [currency, setCurrency] = useState('USD');
2928
const { formatMessage, labels } = useMessages();
30-
const { locale } = useLocale();
29+
const { locale, dateLocale } = useLocale();
3130
const { countryNames } = useCountryNames(locale);
32-
const unit = getMinimumUnit(startDate, endDate);
3331
const { data, error, isLoading } = useResultQuery<any>('revenue', {
3432
websiteId,
35-
startDate,
36-
endDate,
33+
minDate,
34+
maxDate,
3735
currency,
3836
});
3937

@@ -65,15 +63,15 @@ export function Revenue({ websiteId, startDate, endDate }: RevenueProps) {
6563
const color = colord(CHART_COLORS[index % CHART_COLORS.length]);
6664
return {
6765
label: key,
68-
data: map[key],
66+
data: generateTimeSeries(map[key], minDate, maxDate, unit, dateLocale),
6967
lineTension: 0,
7068
backgroundColor: color.alpha(0.6).toRgbString(),
7169
borderColor: color.alpha(0.7).toRgbString(),
7270
borderWidth: 1,
7371
};
7472
}),
7573
};
76-
}, [data, startDate, endDate, unit]);
74+
}, [data, minDate, maxDate, unit]);
7775

7876
const metrics = useMemo(() => {
7977
if (!data) return [];
@@ -104,6 +102,8 @@ export function Revenue({ websiteId, startDate, endDate }: RevenueProps) {
104102
] as any;
105103
}, [data, locale]);
106104

105+
const renderXLabel = useCallback(renderDateLabels(unit, locale), [unit, locale]);
106+
107107
return (
108108
<Column gap>
109109
<Grid columns="280px" gap>
@@ -122,12 +122,12 @@ export function Revenue({ websiteId, startDate, endDate }: RevenueProps) {
122122
<Panel>
123123
<BarChart
124124
chartData={chartData}
125-
minDate={startDate}
126-
maxDate={endDate}
125+
minDate={minDate}
126+
maxDate={maxDate}
127127
unit={unit}
128128
stacked={true}
129129
currency={currency}
130-
renderXLabel={renderDateLabels(unit, locale)}
130+
renderXLabel={renderXLabel}
131131
height="400px"
132132
/>
133133
</Panel>

src/app/(main)/websites/[websiteId]/(reports)/revenue/RevenuePage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { useDateRange } from '@/components/hooks';
66

77
export function RevenuePage({ websiteId }: { websiteId: string }) {
88
const {
9-
dateRange: { startDate, endDate },
9+
dateRange: { startDate, endDate, unit },
1010
} = useDateRange(websiteId);
1111

1212
return (
1313
<Column gap>
1414
<WebsiteControls websiteId={websiteId} />
15-
<Revenue websiteId={websiteId} startDate={startDate} endDate={endDate} />
15+
<Revenue websiteId={websiteId} minDate={startDate} maxDate={endDate} unit={unit} />
1616
</Column>
1717
);
1818
}

src/queries/sql/reports/getRevenue.ts

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ export interface RevenueResult {
1414
chart: { x: string; t: string; y: number }[];
1515
country: { name: string; value: number }[];
1616
total: { sum: number; count: number; average: number; unique_count: number };
17-
table: {
18-
currency: string;
19-
sum: number;
20-
count: number;
21-
unique_count: number;
22-
}[];
2317
}
2418

2519
export async function getRevenue(
@@ -121,32 +115,7 @@ async function relationalQuery(
121115

122116
total.average = total.count > 0 ? total.sum / total.count : 0;
123117

124-
const table = await rawQuery(
125-
`
126-
select
127-
revenue.currency,
128-
sum(revenue.revenue) as sum,
129-
count(distinct revenue.event_id) as count,
130-
count(distinct revenue.session_id) as unique_count
131-
from revenue
132-
join website_event
133-
on website_event.website_id = revenue.website_id
134-
and website_event.session_id = revenue.session_id
135-
and website_event.event_id = revenue.event_id
136-
and website_event.website_id = {{websiteId::uuid}}
137-
and website_event.created_at between {{startDate}} and {{endDate}}
138-
${cohortQuery}
139-
${joinSessionQuery}
140-
where revenue.website_id = {{websiteId::uuid}}
141-
and revenue.created_at between {{startDate}} and {{endDate}}
142-
${filterQuery}
143-
group by revenue.currency
144-
order by sum desc
145-
`,
146-
queryParams,
147-
);
148-
149-
return { chart, country, table, total };
118+
return { chart, country, total };
150119
}
151120

152121
async function clickhouseQuery(
@@ -250,36 +219,5 @@ async function clickhouseQuery(
250219

251220
total.average = total.count > 0 ? total.sum / total.count : 0;
252221

253-
const table = await rawQuery<
254-
{
255-
currency: string;
256-
sum: number;
257-
count: number;
258-
unique_count: number;
259-
}[]
260-
>(
261-
`
262-
select
263-
website_revenue.currency,
264-
sum(website_revenue.revenue) as sum,
265-
uniqExact(website_revenue.event_id) as count,
266-
uniqExact(website_revenue.session_id) as unique_count
267-
from website_revenue
268-
join website_event
269-
on website_event.website_id = website_revenue.website_id
270-
and website_event.session_id = website_revenue.session_id
271-
and website_event.event_id = website_revenue.event_id
272-
and website_event.website_id = {websiteId:UUID}
273-
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
274-
${cohortQuery}
275-
where website_revenue.website_id = {websiteId:UUID}
276-
and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}
277-
${filterQuery}
278-
group by website_revenue.currency
279-
order by sum desc
280-
`,
281-
queryParams,
282-
);
283-
284-
return { chart, country, table, total };
222+
return { chart, country, total };
285223
}

0 commit comments

Comments
 (0)