-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
MetricsSummaryChart.tsx
81 lines (74 loc) · 2.73 KB
/
MetricsSummaryChart.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { useMemo, type VFC } from 'react';
import 'chartjs-adapter-date-fns';
import type { InstanceInsightsSchema } from 'openapi';
import {
fillGradientPrimary,
LineChart,
NotEnoughData,
} from 'component/insights/components/LineChart/LineChart';
import { MetricsSummaryTooltip } from './MetricsChartTooltip/MetricsChartTooltip';
import { useMetricsSummary } from 'component/insights/hooks/useMetricsSummary';
import { usePlaceholderData } from 'component/insights/hooks/usePlaceholderData';
import type { GroupedDataByProject } from 'component/insights/hooks/useGroupedProjectTrends';
import { useTheme } from '@mui/material';
import { aggregateDataPerDate } from './MetricsChartTooltip/aggregate-metrics-by-day';
interface IMetricsSummaryChartProps {
metricsSummaryTrends: GroupedDataByProject<
InstanceInsightsSchema['metricsSummaryTrends']
>;
isAggregate?: boolean;
}
export const MetricsSummaryChart: VFC<IMetricsSummaryChartProps> = ({
metricsSummaryTrends,
isAggregate,
}) => {
const theme = useTheme();
const metricsSummary = useMetricsSummary(metricsSummaryTrends);
const notEnoughData = useMemo(
() => !metricsSummary.datasets.some((d) => d.data.length > 1),
[metricsSummary],
);
const placeholderData = usePlaceholderData();
const aggregatedPerDay = useMemo(() => {
const result = aggregateDataPerDate(
Object.values(metricsSummary.datasets).flatMap((item) => item.data),
);
const data = Object.entries(result)
.map(([date, trends]) => ({ date, ...trends }))
.sort(
(a, b) =>
new Date(a.date).getTime() - new Date(b.date).getTime(),
);
return {
datasets: [
{
label: 'Total Requests',
data: data,
borderColor: theme.palette.primary.light,
backgroundColor: fillGradientPrimary,
fill: true,
order: 3,
},
],
};
}, [JSON.stringify(metricsSummaryTrends), theme]);
const data = isAggregate ? aggregatedPerDay : metricsSummary;
return (
<LineChart
data={notEnoughData ? placeholderData : data}
isLocalTooltip
TooltipComponent={MetricsSummaryTooltip}
overrideOptions={
notEnoughData
? {}
: {
parsing: {
yAxisKey: 'totalRequests',
xAxisKey: 'date',
},
}
}
cover={notEnoughData ? <NotEnoughData /> : false}
/>
);
};