Skip to content

Commit 8dc83c3

Browse files
authored
enable float threshold for chart alert (#773)
- add new schema to support float threshold for chart alert <img width="882" alt="image" src="https://github.com/user-attachments/assets/dd563bf2-9ae9-4c8f-ad63-731d8815518a" /> Tested alert: <img width="1256" alt="image" src="https://github.com/user-attachments/assets/92683d01-ada4-48e7-bd2b-567aba949e4b" /> Ref: hdx-1640
1 parent 14c2821 commit 8dc83c3

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

packages/app/src/components/DBEditTimeChartForm.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import z from 'zod';
1212
import { zodResolver } from '@hookform/resolvers/zod';
1313
import { tcFromSource } from '@hyperdx/common-utils/dist/metadata';
1414
import {
15-
AlertBaseSchema,
15+
ChartAlertBaseSchema,
1616
ChartConfigWithDateRange,
1717
DateRange,
1818
DisplayType,
@@ -80,6 +80,8 @@ const isQueryReady = (queriedConfig: ChartConfigWithDateRange | undefined) =>
8080
(queriedConfig?.from?.tableName || queriedConfig?.metricTables) &&
8181
queriedConfig?.timestampValueExpression;
8282

83+
const MINIMUM_THRESHOLD_VALUE = 0.0000000001; // to make alert input > 0
84+
8385
const NumberFormatInputControlled = ({
8486
control,
8587
onSubmit,
@@ -293,7 +295,7 @@ const defaultTimeRange = parseTimeQuery('Past 1h', false) as [Date, Date];
293295
const zSavedChartConfig = z
294296
.object({
295297
// TODO: Chart
296-
alert: AlertBaseSchema.optional(),
298+
alert: ChartAlertBaseSchema.optional(),
297299
})
298300
.passthrough();
299301

@@ -738,7 +740,7 @@ export default function EditTimeChartForm({
738740
control={control}
739741
/>
740742
<NumberInput
741-
min={1}
743+
min={MINIMUM_THRESHOLD_VALUE}
742744
size="xs"
743745
w={80}
744746
control={control}

packages/app/src/utils/alerts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
AlertInterval,
1414
AlertSource,
1515
AlertThresholdType,
16+
ChartAlertBaseSchema,
1617
} from '@hyperdx/common-utils/dist/types';
1718

1819
import { Granularity } from '@/ChartUtils';
@@ -111,7 +112,7 @@ export const ALERT_CHANNEL_OPTIONS: Record<AlertChannelType, string> = {
111112
webhook: 'Webhook',
112113
};
113114

114-
export const DEFAULT_TILE_ALERT: z.infer<typeof AlertBaseSchema> = {
115+
export const DEFAULT_TILE_ALERT: z.infer<typeof ChartAlertBaseSchema> = {
115116
threshold: 1,
116117
thresholdType: AlertThresholdType.ABOVE,
117118
interval: '5m',

packages/common-utils/src/types.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,14 @@ export const AlertBaseSchema = z.object({
268268
.optional(),
269269
});
270270

271-
export const AlertSchema = AlertBaseSchema.and(
272-
zSavedSearchAlert.or(zTileAlert),
273-
);
271+
export const ChartAlertBaseSchema = AlertBaseSchema.extend({
272+
threshold: z.number().positive(),
273+
});
274+
275+
export const AlertSchema = z.union([
276+
z.intersection(AlertBaseSchema, zSavedSearchAlert),
277+
z.intersection(ChartAlertBaseSchema, zTileAlert),
278+
]);
274279

275280
export type Alert = z.infer<typeof AlertSchema>;
276281

@@ -410,7 +415,10 @@ export const SavedChartConfigSchema = z.intersection(
410415
z.object({
411416
name: z.string(),
412417
source: z.string(),
413-
alert: AlertBaseSchema.optional(),
418+
alert: z.union([
419+
AlertBaseSchema.optional(),
420+
ChartAlertBaseSchema.optional(),
421+
]),
414422
}),
415423
_ChartConfigSchema.omit({
416424
connection: true,

0 commit comments

Comments
 (0)