Skip to content

Commit

Permalink
fix: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestii committed Jan 27, 2025
1 parent 835cbc7 commit 1ea8af3
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .changeset/sharp-worms-impress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"@hyperdx/common-utils": patch
'@hyperdx/common-utils': patch
---

refactor: Extract base alert schema into AlertSchemaBase schema
refactor: Extract alert configuration schema into AlertBaseSchema
16 changes: 11 additions & 5 deletions packages/api/src/controllers/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,20 @@ export const getAlertById = async (
});
};

export const getDashboardAlerts = async (
dashboardIds: string[] | null,
export const getTeamDashboardAlertsByTile = async (teamId: ObjectId) => {
const alerts = await Alert.find({
source: 'tile',
team: teamId,
});
return groupBy(alerts, 'tileId');
};

export const getDashboardAlertsByTile = async (
teamId: ObjectId,
dashboardId: ObjectId | string,
) => {
const alerts = await Alert.find({
...(dashboardIds !== null && {
dashboard: { $in: dashboardIds },
}),
dashboard: dashboardId,
source: 'tile',
team: teamId,
});
Expand Down
7 changes: 4 additions & 3 deletions packages/api/src/controllers/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { z } from 'zod';
import {
createOrUpdateDashboardAlerts,
deleteDashboardAlerts,
getDashboardAlerts,
getDashboardAlertsByTile,
getTeamDashboardAlertsByTile,
} from '@/controllers/alerts';
import type { ObjectId } from '@/models';
import Dashboard from '@/models/dashboard';
Expand All @@ -25,7 +26,7 @@ function pickAlertsByTile(tiles: Tile[]) {
export async function getDashboards(teamId: ObjectId) {
const [_dashboards, alerts] = await Promise.all([
Dashboard.find({ team: teamId }),
getDashboardAlerts(null, teamId),
getTeamDashboardAlertsByTile(teamId),
]);

const dashboards = _dashboards
Expand All @@ -44,7 +45,7 @@ export async function getDashboards(teamId: ObjectId) {
export async function getDashboard(dashboardId: string, teamId: ObjectId) {
const [_dashboard, alerts] = await Promise.all([
Dashboard.findOne({ _id: dashboardId, team: teamId }),
getDashboardAlerts([dashboardId], teamId),
getDashboardAlertsByTile(teamId, dashboardId),
]);

return {
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/routers/api/__tests__/dashboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ describe('dashboard router', () => {
const alerts = await agent.get(`/alerts`).expect(200);
expect(alerts.body.data).toMatchObject([
{
tileId: dashboard.body.tiles[0].id,
...omit(MOCK_ALERT, 'channel.webhookId'),
tileId: dashboard.body.tiles[0].id,
},
]);
});
Expand All @@ -122,8 +122,8 @@ describe('dashboard router', () => {
const alerts = await agent.get(`/alerts`).expect(200);
expect(alerts.body.data).toMatchObject([
{
tileId: updatedDashboard.body.tiles[MOCK_DASHBOARD.tiles.length].id,
...omit(MOCK_ALERT, 'channel.webhookId'),
tileId: updatedDashboard.body.tiles[MOCK_DASHBOARD.tiles.length].id,
},
]);
});
Expand Down Expand Up @@ -186,8 +186,8 @@ describe('dashboard router', () => {
const alerts = await agent.get(`/alerts`).expect(200);
expect(alerts.body.data).toMatchObject([
{
tileId: dashboard.body.tiles[0].id,
...omit(updatedAlert, 'channel.webhookId'),
tileId: dashboard.body.tiles[0].id,
},
]);
});
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/DBDashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ErrorBoundary } from 'react-error-boundary';
import RGL, { WidthProvider } from 'react-grid-layout';
import { Controller, useForm } from 'react-hook-form';
import { useHotkeys } from 'react-hotkeys-hook';
import { AlertState } from '@hyperdx/common-utils/dist/types';
import {
ChartConfigWithDateRange,
DisplayType,
Expand Down Expand Up @@ -196,7 +197,7 @@ const Tile = forwardRef(
if (!alert) {
return 'transparent';
}
if (alert.state === 'OK') {
if (alert.state === AlertState.OK) {
return 'green';
}
if (alert.silenced?.at) {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/components/DBEditTimeChartForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { NativeSelect, NumberInput } from 'react-hook-form-mantine';
import z from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import {
AlertSchemaBase,
AlertBaseSchema,
ChartConfigWithDateRange,
DisplayType,
Filter,
Expand Down Expand Up @@ -221,7 +221,7 @@ const defaultTimeRange = parseTimeQuery('Past 1h', false) as [Date, Date];
const zSavedChartConfig = z
.object({
// TODO: Chart
alert: AlertSchemaBase.optional(),
alert: AlertBaseSchema.optional(),
})
.passthrough();

Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/utils/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import _ from 'lodash';
import { z } from 'zod';
import {
Alert,
AlertBaseSchema,
AlertChannelType,
AlertInterval,
AlertSchemaBase,
AlertSource,
AlertThresholdType,
} from '@hyperdx/common-utils/dist/types';
Expand Down Expand Up @@ -111,7 +111,7 @@ export const ALERT_CHANNEL_OPTIONS: Record<AlertChannelType, string> = {
webhook: 'Webhook',
};

export const DEFAULT_TILE_ALERT: z.infer<typeof AlertSchemaBase> = {
export const DEFAULT_TILE_ALERT: z.infer<typeof AlertBaseSchema> = {
threshold: 1,
thresholdType: AlertThresholdType.ABOVE,
interval: '5m',
Expand Down
6 changes: 3 additions & 3 deletions packages/common-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export const zTileAlert = z.object({
dashboardId: z.string().min(1),
});

export const AlertSchemaBase = z.object({
export const AlertBaseSchema = z.object({
id: z.string().optional(),
interval: AlertIntervalSchema,
threshold: z.number().int().min(1),
Expand All @@ -242,7 +242,7 @@ export const AlertSchemaBase = z.object({
.optional(),
});

export const AlertSchema = AlertSchemaBase.and(
export const AlertSchema = AlertBaseSchema.and(
zSavedSearchAlert.or(zTileAlert),
);

Expand Down Expand Up @@ -347,7 +347,7 @@ export const SavedChartConfigSchema = z.intersection(
z.object({
name: z.string(),
source: z.string(),
alert: AlertSchemaBase.optional(),
alert: AlertBaseSchema.optional(),
}),
_ChartConfigSchema.omit({
connection: true,
Expand Down

0 comments on commit 1ea8af3

Please sign in to comment.