-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathutils.ts
More file actions
307 lines (282 loc) · 9.17 KB
/
utils.ts
File metadata and controls
307 lines (282 loc) · 9.17 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { omit, pick } from 'lodash';
import { Path, UseFormSetError } from 'react-hook-form';
import { validateRawSqlForAlert } from '@hyperdx/common-utils/dist/core/utils';
import {
isBuilderSavedChartConfig,
isRawSqlSavedChartConfig,
} from '@hyperdx/common-utils/dist/guards';
import {
BuilderSavedChartConfig,
ChartConfigWithDateRange,
DisplayType,
getSampleWeightExpression,
isLogSource,
isMetricSource,
isTraceSource,
RawSqlChartConfig,
RawSqlSavedChartConfig,
SavedChartConfig,
SourceKind,
TSource,
} from '@hyperdx/common-utils/dist/types';
import { getStoredLanguage } from '../SearchInput';
import { ChartEditorFormState } from './types';
function normalizeChartConfig<
C extends Pick<
BuilderSavedChartConfig,
'select' | 'having' | 'orderBy' | 'displayType' | 'metricTables'
>,
>(config: C, source: TSource): C {
const isMetricSource = source.kind === SourceKind.Metric;
return {
...config,
// Strip out metric-specific fields for non-metric sources
select:
!isMetricSource && Array.isArray(config.select)
? config.select.map(s => omit(s, ['metricName', 'metricType']))
: config.select,
metricTables: isMetricSource ? config.metricTables : undefined,
// Order By and Having can only be set by the user for table charts
having:
config.displayType === DisplayType.Table ? config.having : undefined,
orderBy:
config.displayType === DisplayType.Table ? config.orderBy : undefined,
};
}
export const isRawSqlDisplayType = (
displayType: DisplayType | undefined,
): displayType is
| DisplayType.Table
| DisplayType.Line
| DisplayType.StackedBar
| DisplayType.Pie
| DisplayType.Number =>
displayType === DisplayType.Table ||
displayType === DisplayType.Line ||
displayType === DisplayType.StackedBar ||
displayType === DisplayType.Pie ||
displayType === DisplayType.Number;
export function convertFormStateToSavedChartConfig(
form: ChartEditorFormState,
source: TSource | undefined,
): SavedChartConfig | undefined {
if (form.configType === 'sql' && isRawSqlDisplayType(form.displayType)) {
const rawSqlConfig: RawSqlSavedChartConfig = {
configType: 'sql',
...pick(form, [
'name',
'displayType',
'numberFormat',
'granularity',
'compareToPreviousPeriod',
'fillNulls',
'alignDateRangeToGranularity',
'alert',
]),
sqlTemplate: form.sqlTemplate ?? '',
connection: form.connection ?? '',
source: form.source || undefined,
};
return rawSqlConfig;
}
if (source) {
// Merge the series and select fields back together, and prevent the series field from being submitted
const config: BuilderSavedChartConfig = {
...omit(form, ['series', 'configType', 'sqlTemplate']),
// If the chart type is search, we need to ensure the select is a string
select:
form.displayType === DisplayType.Search
? typeof form.select === 'string'
? form.select
: ''
: form.series,
where: form.where ?? '',
source: source.id,
};
return normalizeChartConfig(config, source);
}
}
export function convertFormStateToChartConfig(
form: ChartEditorFormState,
dateRange: ChartConfigWithDateRange['dateRange'],
source: TSource | undefined,
): ChartConfigWithDateRange | undefined {
if (form.configType === 'sql' && isRawSqlDisplayType(form.displayType)) {
const rawSqlConfig: RawSqlChartConfig = {
configType: 'sql',
...pick(form, [
'name',
'displayType',
'numberFormat',
'granularity',
'compareToPreviousPeriod',
'fillNulls',
'alignDateRangeToGranularity',
]),
sqlTemplate: form.sqlTemplate ?? '',
connection: form.connection ?? '',
source: form.source || undefined,
from: source?.from,
implicitColumnExpression:
source && (isLogSource(source) || isTraceSource(source))
? source.implicitColumnExpression
: undefined,
metricTables:
source && isMetricSource(source) ? source.metricTables : undefined,
};
return { ...rawSqlConfig, dateRange };
}
if (source) {
// Merge the series and select fields back together, and prevent the series field from being submitted
const mergedSelect =
form.displayType === DisplayType.Search ? form.select : form.series;
const isSelectEmpty = !mergedSelect || mergedSelect.length === 0;
const newConfig: ChartConfigWithDateRange = {
...omit(form, ['series', 'configType', 'sqlTemplate']),
from: source.from,
timestampValueExpression: source.timestampValueExpression,
dateRange,
connection: source.connection,
implicitColumnExpression:
isLogSource(source) || isTraceSource(source)
? source.implicitColumnExpression
: undefined,
sampleWeightExpression: getSampleWeightExpression(source),
metricTables: isMetricSource(source) ? source.metricTables : undefined,
where: form.where ?? '',
select: isSelectEmpty
? ((isLogSource(source) || isTraceSource(source)) &&
source.defaultTableSelectExpression) ||
''
: mergedSelect,
};
return structuredClone(normalizeChartConfig(newConfig, source));
}
}
export function convertSavedChartConfigToFormState(
config: SavedChartConfig,
): ChartEditorFormState {
return {
...config,
configType: isRawSqlSavedChartConfig(config) ? 'sql' : 'builder',
series:
isBuilderSavedChartConfig(config) && Array.isArray(config.select)
? config.select.map(s => ({
...s,
aggConditionLanguage:
s.aggConditionLanguage ?? getStoredLanguage() ?? 'lucene',
}))
: [],
};
}
export const validateChartForm = (
form: ChartEditorFormState,
source: TSource | undefined,
setError: UseFormSetError<ChartEditorFormState>,
) => {
const errors: { path: Path<ChartEditorFormState>; message: string }[] = [];
const isRawSqlChart =
form.configType === 'sql' && isRawSqlDisplayType(form.displayType);
// Validate connection is selected for raw SQL charts
if (isRawSqlChart && !form.connection) {
errors.push({ path: `connection`, message: 'Connection is required' });
}
// Validate SQL is provided for raw SQL charts
if (isRawSqlChart && !form.sqlTemplate) {
errors.push({ path: `sqlTemplate`, message: 'SQL query is required' });
}
// Validate source is selected for builder charts
if (
!isRawSqlChart &&
form.displayType !== DisplayType.Markdown &&
(!form.source || !source)
) {
errors.push({ path: `source`, message: 'Source is required' });
}
// Validate that valueExpressions are specified for each series
if (
!isRawSqlChart &&
Array.isArray(form.series) &&
source?.kind !== SourceKind.Metric &&
form.displayType !== DisplayType.Markdown &&
form.displayType !== DisplayType.Search
) {
form.series.forEach((s, index) => {
if (s.aggFn && s.aggFn !== 'count' && !s.valueExpression) {
errors.push({
path: `series.${index}.valueExpression`,
message: `Expression is required for series ${index + 1}`,
});
}
});
}
// Validate metric names for metric sources
if (
source?.kind === SourceKind.Metric &&
Array.isArray(form.series) &&
form.displayType !== DisplayType.Markdown &&
form.displayType !== DisplayType.Search &&
!isRawSqlChart
) {
form.series.forEach((s, index) => {
if (s.metricType && !s.metricName) {
errors.push({
path: `series.${index}.metricName`,
message: `Metric is required`,
});
}
});
}
// Validate raw SQL alert has required time filters and interval parameters
if (isRawSqlChart && form.alert) {
const config = {
configType: 'sql',
sqlTemplate: form.sqlTemplate ?? '',
connection: form.connection ?? '',
from: source?.from,
displayType: form.displayType,
} satisfies RawSqlChartConfig;
const { errors: alertErrors } = validateRawSqlForAlert(config);
if (alertErrors.length > 0) {
errors.push({
path: `sqlTemplate`,
message: alertErrors.join('. '),
});
}
}
// Validate number, pie, and heatmap charts only have one series
if (
!isRawSqlChart &&
Array.isArray(form.series) &&
(form.displayType === DisplayType.Number ||
form.displayType === DisplayType.Pie ||
form.displayType === DisplayType.Heatmap) &&
form.series.length > 1
) {
errors.push({
path: `series`,
message: `Only one series is allowed for ${form.displayType} charts`,
});
}
// Validate heatmap requires a value expression
if (
!isRawSqlChart &&
form.displayType === DisplayType.Heatmap &&
Array.isArray(form.series) &&
form.series.length > 0 &&
!form.series[0]?.valueExpression
) {
errors.push({
path: `series.0.valueExpression`,
message: 'Value expression is required for heatmap charts',
});
}
for (const error of errors) {
console.warn(`Validation error in field ${error.path}: ${error.message}`);
setError(error.path, {
type: 'manual',
message: error.message,
});
}
return errors;
};