-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathworkflow-runs-trend-chart.tsx
More file actions
225 lines (203 loc) · 7.52 KB
/
workflow-runs-trend-chart.tsx
File metadata and controls
225 lines (203 loc) · 7.52 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { FeatureFlagsKeysEnum } from '@novu/shared';
import { useCallback, useMemo } from 'react';
import { Line, LineChart, XAxis } from 'recharts';
import { type WorkflowRunsTrendDataPoint } from '../../../api/activity';
import { useFeatureFlag } from '../../../hooks/use-feature-flag';
import { ChartConfig, ChartContainer, ChartTooltip, NovuTooltip } from '../../primitives/chart';
import { Skeleton } from '../../primitives/skeleton';
import { ANALYTICS_TOOLTIPS } from '../constants/analytics-tooltips';
import { createDateBasedHasDataChecker } from '../utils/chart-validation';
import { generateDummyWorkflowRunsData } from './chart-dummy-data';
import { type WorkflowRunsChartData } from './chart-types';
import { ChartWrapper } from './chart-wrapper';
const legacyChartConfig = {
success: {
label: 'Success',
color: '#34d399',
},
pending: {
label: 'Pending',
color: '#facc15',
},
error: {
label: 'Error',
color: '#ef4444',
},
} satisfies ChartConfig;
const finalStatusChartConfig = {
success: {
label: 'Success',
color: '#34d399',
},
error: {
label: 'Error',
color: '#ef4444',
},
} satisfies ChartConfig;
function WorkflowRunsTrendChartSkeleton() {
return (
<div className="h-[160px] w-full relative px-4">
<div className="absolute inset-0 flex items-end justify-between px-2">
{Array.from({ length: 35 }).map((_, i) => {
const baseHeight = 40;
const successHeight = baseHeight + Math.sin(i * 0.3) * 30 + Math.random() * 20;
return (
<div key={i} className="flex flex-col items-center flex-1 relative">
<div className="relative w-full flex justify-center">
<Skeleton
className="rounded-sm"
style={{
height: `${Math.max(successHeight, 20)}px`,
width: '15px',
}}
/>
</div>
</div>
);
})}
</div>
<div className="absolute bottom-6 left-4 right-4 h-px">
<Skeleton className="h-full w-full" />
</div>
</div>
);
}
type WorkflowRunsTrendChartProps = {
data?: WorkflowRunsTrendDataPoint[];
isLoading?: boolean;
error?: Error | null;
};
function LegacyWorkflowRunsTrendChart({ data, isLoading, error }: WorkflowRunsTrendChartProps) {
const chartData = useMemo(() => {
return data?.map((dataPoint) => ({
date: new Date(dataPoint.timestamp).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
}),
completed: dataPoint.completed,
processing: dataPoint.processing,
error: dataPoint.error,
timestamp: dataPoint.timestamp,
}));
}, [data]);
const hasDataChecker = useCallback(
createDateBasedHasDataChecker<WorkflowRunsChartData>(
(dataPoint: WorkflowRunsChartData) =>
(dataPoint.completed || 0) > 0 || (dataPoint.processing || 0) > 0 || (dataPoint.error || 0) > 0
),
[]
);
const renderChart = useCallback((chartDataToRender: WorkflowRunsChartData[], includeTooltip = true) => {
return (
<ChartContainer config={legacyChartConfig} className="h-[160px] w-full">
<LineChart accessibilityLayer data={chartDataToRender}>
<XAxis
dataKey="date"
axisLine={{ stroke: '#e5e7eb', strokeDasharray: '3 3', strokeWidth: 1 }}
tickLine={false}
tick={{ fontSize: 10, fill: '#99a0ae', textAnchor: 'middle' }}
tickFormatter={(value, index) => {
if (index % 2 === 0) return value;
return '';
}}
domain={['dataMin', 'dataMax']}
/>
{includeTooltip && <ChartTooltip cursor={false} content={<NovuTooltip showTotal={false} />} />}
<Line dataKey="completed" name="Completed" stroke="#34d399" strokeWidth={2} dot={false} type="monotone" />
<Line dataKey="processing" name="Processing" stroke="#facc15" strokeWidth={2} dot={false} type="monotone" />
<Line dataKey="error" name="Error" stroke="#ef4444" strokeWidth={2} dot={false} type="monotone" />
</LineChart>
</ChartContainer>
);
}, []);
const renderEmptyState = useCallback(
(dummyData: WorkflowRunsChartData[]) => renderChart(dummyData, false),
[renderChart]
);
return (
<ChartWrapper
title="Workflow runs"
data={chartData}
isLoading={isLoading}
error={error}
hasDataChecker={hasDataChecker}
loadingSkeleton={<WorkflowRunsTrendChartSkeleton />}
dummyDataGenerator={generateDummyWorkflowRunsData}
emptyStateRenderer={renderEmptyState}
infoTooltip={ANALYTICS_TOOLTIPS.WORKFLOW_RUNS_TREND}
emptyStateTitle="Not enough data to show"
emptyStateTooltip={ANALYTICS_TOOLTIPS.INSUFFICIENT_DATE_RANGE}
>
{renderChart}
</ChartWrapper>
);
}
function FinalStatusWorkflowRunsTrendChart({ data, isLoading, error }: WorkflowRunsTrendChartProps) {
const chartData = useMemo(() => {
return data?.map((dataPoint) => ({
date: new Date(dataPoint.timestamp).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
}),
completed: dataPoint.completed,
error: dataPoint.error,
timestamp: dataPoint.timestamp,
}));
}, [data]);
const hasDataChecker = useCallback(
createDateBasedHasDataChecker<WorkflowRunsChartData>(
(dataPoint: WorkflowRunsChartData) => (dataPoint.completed || 0) > 0 || (dataPoint.error || 0) > 0
),
[]
);
const renderChart = useCallback((chartDataToRender: WorkflowRunsChartData[], includeTooltip = true) => {
return (
<ChartContainer config={finalStatusChartConfig} className="h-[160px] w-full">
<LineChart accessibilityLayer data={chartDataToRender}>
<XAxis
dataKey="date"
axisLine={{ stroke: '#e5e7eb', strokeDasharray: '3 3', strokeWidth: 1 }}
tickLine={false}
tick={{ fontSize: 10, fill: '#99a0ae', textAnchor: 'middle' }}
tickFormatter={(value, index) => {
if (index % 2 === 0) return value;
return '';
}}
domain={['dataMin', 'dataMax']}
/>
{includeTooltip && <ChartTooltip cursor={false} content={<NovuTooltip showTotal={false} />} />}
<Line dataKey="completed" name="Completed" stroke="#34d399" strokeWidth={2} dot={false} type="monotone" />
<Line dataKey="error" name="Error" stroke="#ef4444" strokeWidth={2} dot={false} type="monotone" />
</LineChart>
</ChartContainer>
);
}, []);
const renderEmptyState = useCallback(
(dummyData: WorkflowRunsChartData[]) => renderChart(dummyData, false),
[renderChart]
);
return (
<ChartWrapper
title="Workflow runs"
data={chartData}
isLoading={isLoading}
error={error}
hasDataChecker={hasDataChecker}
loadingSkeleton={<WorkflowRunsTrendChartSkeleton />}
dummyDataGenerator={generateDummyWorkflowRunsData}
emptyStateRenderer={renderEmptyState}
infoTooltip={ANALYTICS_TOOLTIPS.WORKFLOW_RUNS_TREND}
emptyStateTitle="Not enough data to show"
emptyStateTooltip={ANALYTICS_TOOLTIPS.INSUFFICIENT_DATE_RANGE}
>
{renderChart}
</ChartWrapper>
);
}
export function WorkflowRunsTrendChart(props: WorkflowRunsTrendChartProps) {
const isFinalStatusOnly = useFeatureFlag(FeatureFlagsKeysEnum.IS_WORKFLOW_RUN_TREND_FROM_ROLLUP_ENABLED);
if (isFinalStatusOnly) {
return <FinalStatusWorkflowRunsTrendChart {...props} />;
}
return <LegacyWorkflowRunsTrendChart {...props} />;
}