feat: add alert schema and types to common utils (#564)

@ernestii You should be able to import alert relates types from the `common-utils` after this PR
This commit is contained in:
Warren 2025-01-21 23:36:08 -08:00 committed by GitHub
parent b3f3151a12
commit fc4548fdab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 88 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"@hyperdx/common-utils": patch
---
feat: add alert schema + types

View file

@ -22,8 +22,8 @@ jobs:
id: changesets
uses: changesets/action@v1
with:
# This expects you to have a script called release which does a build for your packages and calls changeset publish
publish: yarn release
version: yarn run version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

View file

@ -27,7 +27,8 @@
"prepare": "husky install",
"app:lint": "nx run @hyperdx/app:ci:lint",
"lint": "npx nx run-many -t ci:lint",
"release": "npx nx run-many --target=build --projects=@hyperdx/common-utils && yarn changeset publish",
"version": "npx changeset version --ignore @hyperdx/api --ignore @hyperdx/app",
"release": "npx nx run-many --target=build --projects=@hyperdx/common-utils && npx changeset publish",
"dev": "docker compose -f docker-compose.dev.yml up"
},
"lint-staged": {

View file

@ -129,6 +129,86 @@ export type SelectSQLStatement = {
limit?: Limit;
};
// -------------------------
// ALERTS
// -------------------------
export enum AlertThresholdType {
ABOVE = 'above',
BELOW = 'below',
}
export enum AlertState {
ALERT = 'ALERT',
DISABLED = 'DISABLED',
INSUFFICIENT_DATA = 'INSUFFICIENT_DATA',
OK = 'OK',
}
export enum AlertSource {
SAVED_SEARCH = 'saved_search',
TILE = 'tile',
}
export const AlertIntervalSchema = z.union([
z.literal('1m'),
z.literal('5m'),
z.literal('15m'),
z.literal('30m'),
z.literal('1h'),
z.literal('6h'),
z.literal('12h'),
z.literal('1d'),
]);
export type AlertInterval = z.infer<typeof AlertIntervalSchema>;
export const zAlertChannel = z.object({
type: z.literal('webhook'),
webhookId: z.string().nonempty("Webhook ID can't be empty"),
});
export const zSavedSearchAlert = z.object({
source: z.literal(AlertSource.SAVED_SEARCH),
groupBy: z.string().optional(),
savedSearchId: z.string().min(1),
});
export const zTileAlert = z.object({
source: z.literal(AlertSource.TILE),
tileId: z.string().min(1),
dashboardId: z.string().min(1),
});
export const AlertSchema = z
.object({
id: z.string().optional(),
interval: AlertIntervalSchema,
threshold: z.number().int().min(1),
thresholdType: z.nativeEnum(AlertThresholdType),
channel: zAlertChannel,
state: z.nativeEnum(AlertState).optional(),
name: z.string().min(1).max(512).nullish(),
message: z.string().min(1).max(4096).nullish(),
source: z.nativeEnum(AlertSource),
silenced: z
.object({
by: z.string(),
at: z.string(),
until: z.string(),
})
.optional(),
})
.and(zSavedSearchAlert.or(zTileAlert));
export type Alert = z.infer<typeof AlertSchema>;
export type AlertHistory = {
counts: number;
createdAt: string;
lastValues: { startTime: string; count: number }[];
state: AlertState;
};
// --------------------------
// SAVED SEARCH
// --------------------------