seperate revenue queries for better api/performance. Fix realtime css/ui issues.
This commit is contained in:
1 parent
c6ec8385c9
commit
b7e1d902f4
18 files changed
+755
-355
No files matched your search
@@ -42,6 +42,9 @@ export * from './queries/useReportQuery';
|
||||
export * from './queries/useReportsQuery';
|
||||
export * from './queries/useResultQuery';
|
||||
export * from './queries/useRevenueSessionsQuery';
|
||||
export * from './queries/useRevenueMetricsQuery';
|
||||
export * from './queries/useRevenueChartQuery';
|
||||
export * from './queries/useRevenueStatsQuery';
|
||||
export * from './queries/useSavedReplaysQuery';
|
||||
export * from './queries/useSessionActivityQuery';
|
||||
export * from './queries/useSessionDataPropertiesQuery';
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { keepPreviousData } from '@tanstack/react-query';
|
||||
import type { ReactQueryOptions } from '@/lib/types';
|
||||
import { useApi } from '../useApi';
|
||||
import { useDateParameters } from '../useDateParameters';
|
||||
import { useFilterParameters } from '../useFilterParameters';
|
||||
|
||||
export interface RevenueChartData {
|
||||
chart: { x: string; t: string; y: number; count: number }[];
|
||||
}
|
||||
|
||||
export function useRevenueChartQuery(
|
||||
websiteId: string,
|
||||
currency: string,
|
||||
options?: ReactQueryOptions<RevenueChartData>,
|
||||
) {
|
||||
const { get, useQuery } = useApi();
|
||||
const { startAt, endAt, timezone, unit } = useDateParameters();
|
||||
const filters = useFilterParameters({ includePagination: false });
|
||||
|
||||
return useQuery<RevenueChartData>({
|
||||
queryKey: [
|
||||
'websites:revenue:chart',
|
||||
{
|
||||
websiteId,
|
||||
currency,
|
||||
startAt,
|
||||
endAt,
|
||||
timezone,
|
||||
unit,
|
||||
...filters,
|
||||
},
|
||||
],
|
||||
queryFn: async () =>
|
||||
get(`/websites/${websiteId}/revenue/chart`, {
|
||||
currency,
|
||||
startAt,
|
||||
endAt,
|
||||
timezone,
|
||||
unit,
|
||||
...filters,
|
||||
}),
|
||||
enabled: !!(websiteId && currency),
|
||||
placeholderData: keepPreviousData,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { keepPreviousData } from '@tanstack/react-query';
|
||||
import type { ReactQueryOptions } from '@/lib/types';
|
||||
import { useApi } from '../useApi';
|
||||
import { useDateParameters } from '../useDateParameters';
|
||||
import { useFilterParameters } from '../useFilterParameters';
|
||||
|
||||
export type RevenueMetricType = 'country' | 'region' | 'referrer' | 'channel';
|
||||
|
||||
export type RevenueMetricsData = {
|
||||
name: string;
|
||||
value: number;
|
||||
country?: string;
|
||||
}[];
|
||||
|
||||
export function useRevenueMetricsQuery(
|
||||
websiteId: string,
|
||||
params: { type: RevenueMetricType; currency: string },
|
||||
options?: ReactQueryOptions<RevenueMetricsData>,
|
||||
) {
|
||||
const { get, useQuery } = useApi();
|
||||
const { startAt, endAt } = useDateParameters();
|
||||
const filters = useFilterParameters({ includePagination: false });
|
||||
|
||||
return useQuery<RevenueMetricsData>({
|
||||
queryKey: [
|
||||
'websites:revenue:metrics',
|
||||
{
|
||||
websiteId,
|
||||
startAt,
|
||||
endAt,
|
||||
...filters,
|
||||
...params,
|
||||
},
|
||||
],
|
||||
queryFn: async () =>
|
||||
get(`/websites/${websiteId}/revenue/metrics`, {
|
||||
startAt,
|
||||
endAt,
|
||||
...filters,
|
||||
...params,
|
||||
}),
|
||||
enabled: !!(websiteId && params.currency && params.type),
|
||||
placeholderData: keepPreviousData,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { keepPreviousData, type UseQueryOptions } from '@tanstack/react-query';
|
||||
import { useDateParameters } from '@/components/hooks/useDateParameters';
|
||||
import { useApi } from '../useApi';
|
||||
import { useFilterParameters } from '../useFilterParameters';
|
||||
|
||||
export interface RevenueStatsData {
|
||||
sum: number;
|
||||
count: number;
|
||||
average: number;
|
||||
unique_count: number;
|
||||
arpu: number;
|
||||
comparison: {
|
||||
sum: number;
|
||||
count: number;
|
||||
average: number;
|
||||
unique_count: number;
|
||||
arpu: number;
|
||||
};
|
||||
}
|
||||
|
||||
export function useRevenueStatsQuery(
|
||||
{
|
||||
websiteId,
|
||||
currency,
|
||||
compare,
|
||||
}: {
|
||||
websiteId: string;
|
||||
currency: string;
|
||||
compare?: string;
|
||||
},
|
||||
options?: UseQueryOptions<RevenueStatsData, Error, RevenueStatsData>,
|
||||
) {
|
||||
const { get, useQuery } = useApi();
|
||||
const { startAt, endAt } = useDateParameters();
|
||||
const filters = useFilterParameters({ includePagination: false });
|
||||
|
||||
return useQuery<RevenueStatsData>({
|
||||
queryKey: ['websites:revenue:stats', { websiteId, currency, compare, startAt, endAt, ...filters }],
|
||||
queryFn: () =>
|
||||
get(`/websites/${websiteId}/revenue/stats`, {
|
||||
currency,
|
||||
compare,
|
||||
startAt,
|
||||
endAt,
|
||||
...filters,
|
||||
}),
|
||||
enabled: !!(websiteId && currency),
|
||||
placeholderData: keepPreviousData,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
import { colord } from 'colord';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { BarChart } from '@/components/charts/BarChart';
|
||||
import { useLocale, useMessages } from '@/components/hooks';
|
||||
import { renderDateLabels } from '@/lib/charts';
|
||||
import { CHART_COLORS } from '@/lib/constants';
|
||||
import { DATE_FORMATS, DATE_FUNCTIONS, formatDate, generateTimeSeries } from '@/lib/date';
|
||||
|
||||
export interface RevenueChartProps {
|
||||
data: { x: string; t: string; y: number; count: number }[];
|
||||
mode?: 'period' | 'cumulative';
|
||||
unit: string;
|
||||
minDate: Date;
|
||||
maxDate: Date;
|
||||
currency: string;
|
||||
}
|
||||
|
||||
export function RevenueChart({
|
||||
data,
|
||||
mode = 'period',
|
||||
unit,
|
||||
minDate,
|
||||
maxDate,
|
||||
currency,
|
||||
}: RevenueChartProps) {
|
||||
const { t, labels } = useMessages();
|
||||
const { locale, dateLocale } = useLocale();
|
||||
const isCumulative = mode === 'cumulative';
|
||||
|
||||
const chartData: any = useMemo(() => {
|
||||
if (!data?.length) return { datasets: [] };
|
||||
|
||||
if (isCumulative) {
|
||||
const cumulativeMaxDate = maxDate > new Date() ? new Date() : maxDate;
|
||||
const cutoff = formatDate(cumulativeMaxDate, DATE_FORMATS[unit], dateLocale);
|
||||
const bucketDates = getBucketDates(minDate, maxDate, unit, dateLocale);
|
||||
const totals = data.reduce(
|
||||
(obj, { t, y }) => {
|
||||
obj[t] = (obj[t] || 0) + y;
|
||||
return obj;
|
||||
},
|
||||
{} as Record<string, number>,
|
||||
);
|
||||
|
||||
let total = 0;
|
||||
const timeseries = generateTimeSeries(
|
||||
Object.entries(totals).map(([x, y]) => ({ x, y })),
|
||||
minDate,
|
||||
maxDate,
|
||||
unit,
|
||||
dateLocale,
|
||||
).map(point => {
|
||||
const d = point.d ?? bucketDates.get(point.x);
|
||||
|
||||
if (point.x > cutoff) {
|
||||
return {
|
||||
...point,
|
||||
d,
|
||||
y: null,
|
||||
};
|
||||
}
|
||||
|
||||
total += point.y || 0;
|
||||
|
||||
return {
|
||||
...point,
|
||||
d,
|
||||
y: total,
|
||||
};
|
||||
});
|
||||
|
||||
const color = colord(CHART_COLORS[0]);
|
||||
|
||||
return {
|
||||
datasets: [
|
||||
{
|
||||
type: 'line',
|
||||
label: t(labels.revenue),
|
||||
data: timeseries,
|
||||
backgroundColor: (context: any) => {
|
||||
const { chart } = context;
|
||||
const { ctx, chartArea } = chart;
|
||||
|
||||
if (!chartArea) {
|
||||
return color.alpha(0.18).toRgbString();
|
||||
}
|
||||
|
||||
const gradient = ctx.createLinearGradient(0, chartArea.top, 0, chartArea.bottom);
|
||||
gradient.addColorStop(0, color.alpha(0.22).toRgbString());
|
||||
gradient.addColorStop(1, color.alpha(0.02).toRgbString());
|
||||
|
||||
return gradient;
|
||||
},
|
||||
borderColor: color.alpha(0.95).toRgbString(),
|
||||
borderWidth: 2,
|
||||
pointRadius: 2,
|
||||
tension: 0.25,
|
||||
fill: true,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const map = data.reduce(
|
||||
(obj, { x, t, y }) => {
|
||||
if (!obj[x]) obj[x] = [];
|
||||
obj[x].push({ x: t, y });
|
||||
return obj;
|
||||
},
|
||||
{} as Record<string, { x: string; y: number }[]>,
|
||||
);
|
||||
|
||||
return {
|
||||
datasets: Object.keys(map).map((key, index) => {
|
||||
const color = colord(CHART_COLORS[index % CHART_COLORS.length]);
|
||||
return {
|
||||
label: key,
|
||||
data: generateTimeSeries(map[key], minDate, maxDate, unit, dateLocale),
|
||||
backgroundColor: color.alpha(0.6).toRgbString(),
|
||||
borderColor: color.alpha(0.7).toRgbString(),
|
||||
borderWidth: 1,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}, [data, minDate, maxDate, unit, dateLocale, isCumulative, t, labels.revenue]);
|
||||
|
||||
const renderXLabel = useCallback(renderDateLabels(unit, locale), [unit, locale]);
|
||||
|
||||
return (
|
||||
<BarChart
|
||||
chartData={chartData}
|
||||
unit={unit}
|
||||
minDate={minDate}
|
||||
maxDate={maxDate}
|
||||
currency={currency}
|
||||
stacked={!isCumulative}
|
||||
renderXLabel={renderXLabel}
|
||||
height="400px"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function getBucketDates(minDate: Date, maxDate: Date, unit: string, locale: string) {
|
||||
const add = DATE_FUNCTIONS[unit].add;
|
||||
const start = DATE_FUNCTIONS[unit].start;
|
||||
const fmt = DATE_FORMATS[unit];
|
||||
const dates = new Map<string, Date>();
|
||||
|
||||
let current = start(minDate);
|
||||
const end = start(maxDate);
|
||||
|
||||
while (current <= end) {
|
||||
dates.set(formatDate(current, fmt, locale), current);
|
||||
current = add(current, 1);
|
||||
}
|
||||
|
||||
return dates;
|
||||
}
|
||||
Reference in new issue
Block a user