Skip to content

Commit fc4548f

Browse files
authored
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
1 parent b3f3151 commit fc4548f

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

.changeset/gorgeous-owls-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/common-utils": patch
3+
---
4+
5+
feat: add alert schema + types

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ jobs:
2222
id: changesets
2323
uses: changesets/action@v1
2424
with:
25-
# This expects you to have a script called release which does a build for your packages and calls changeset publish
2625
publish: yarn release
26+
version: yarn run version
2727
env:
2828
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2929
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"prepare": "husky install",
2828
"app:lint": "nx run @hyperdx/app:ci:lint",
2929
"lint": "npx nx run-many -t ci:lint",
30-
"release": "npx nx run-many --target=build --projects=@hyperdx/common-utils && yarn changeset publish",
30+
"version": "npx changeset version --ignore @hyperdx/api --ignore @hyperdx/app",
31+
"release": "npx nx run-many --target=build --projects=@hyperdx/common-utils && npx changeset publish",
3132
"dev": "docker compose -f docker-compose.dev.yml up"
3233
},
3334
"lint-staged": {

packages/common-utils/src/types.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,86 @@ export type SelectSQLStatement = {
129129
limit?: Limit;
130130
};
131131

132+
// -------------------------
133+
// ALERTS
134+
// -------------------------
135+
export enum AlertThresholdType {
136+
ABOVE = 'above',
137+
BELOW = 'below',
138+
}
139+
140+
export enum AlertState {
141+
ALERT = 'ALERT',
142+
DISABLED = 'DISABLED',
143+
INSUFFICIENT_DATA = 'INSUFFICIENT_DATA',
144+
OK = 'OK',
145+
}
146+
147+
export enum AlertSource {
148+
SAVED_SEARCH = 'saved_search',
149+
TILE = 'tile',
150+
}
151+
152+
export const AlertIntervalSchema = z.union([
153+
z.literal('1m'),
154+
z.literal('5m'),
155+
z.literal('15m'),
156+
z.literal('30m'),
157+
z.literal('1h'),
158+
z.literal('6h'),
159+
z.literal('12h'),
160+
z.literal('1d'),
161+
]);
162+
163+
export type AlertInterval = z.infer<typeof AlertIntervalSchema>;
164+
165+
export const zAlertChannel = z.object({
166+
type: z.literal('webhook'),
167+
webhookId: z.string().nonempty("Webhook ID can't be empty"),
168+
});
169+
170+
export const zSavedSearchAlert = z.object({
171+
source: z.literal(AlertSource.SAVED_SEARCH),
172+
groupBy: z.string().optional(),
173+
savedSearchId: z.string().min(1),
174+
});
175+
176+
export const zTileAlert = z.object({
177+
source: z.literal(AlertSource.TILE),
178+
tileId: z.string().min(1),
179+
dashboardId: z.string().min(1),
180+
});
181+
182+
export const AlertSchema = z
183+
.object({
184+
id: z.string().optional(),
185+
interval: AlertIntervalSchema,
186+
threshold: z.number().int().min(1),
187+
thresholdType: z.nativeEnum(AlertThresholdType),
188+
channel: zAlertChannel,
189+
state: z.nativeEnum(AlertState).optional(),
190+
name: z.string().min(1).max(512).nullish(),
191+
message: z.string().min(1).max(4096).nullish(),
192+
source: z.nativeEnum(AlertSource),
193+
silenced: z
194+
.object({
195+
by: z.string(),
196+
at: z.string(),
197+
until: z.string(),
198+
})
199+
.optional(),
200+
})
201+
.and(zSavedSearchAlert.or(zTileAlert));
202+
203+
export type Alert = z.infer<typeof AlertSchema>;
204+
205+
export type AlertHistory = {
206+
counts: number;
207+
createdAt: string;
208+
lastValues: { startTime: string; count: number }[];
209+
state: AlertState;
210+
};
211+
132212
// --------------------------
133213
// SAVED SEARCH
134214
// --------------------------

0 commit comments

Comments
 (0)