Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/paginate-alerts-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/api': patch
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
'@hyperdx/common-utils': patch
---

feat: page and filter GET /alerts server-side
58 changes: 39 additions & 19 deletions packages/api/scripts/seed-alerts.ts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script was updated to (a) populate tags and displayName on the seeded alert documents and (b) stop seeding DISABLED alerts, which is not a state that is actually created in the system currently

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* MONGO_URI. The dev stack must be up (`yarn dev`) and registered once, since
* the script attaches everything to the existing team and log source.
*/
import { formatTileAlertDisplayName } from '@hyperdx/common-utils/dist/alerts';
import { DisplayType } from '@hyperdx/common-utils/dist/types';
import { execFileSync } from 'child_process';
import mongoose from 'mongoose';
Expand All @@ -41,10 +42,9 @@ const INSERT_CHUNK_SIZE = 500;

/** Roughly what a real team looks like: mostly quiet, a few firing. */
const STATE_WEIGHTS: [AlertState, number][] = [
[AlertState.OK, 74],
[AlertState.OK, 79],
[AlertState.ALERT, 12],
[AlertState.PENDING, 9],
[AlertState.DISABLED, 5],
];

const INTERVALS = ['1m', '5m', '15m', '30m', '1h', '6h', '12h', '1d'] as const;
Expand Down Expand Up @@ -183,20 +183,28 @@ function makeAlertDoc({
teamId,
channel,
index,
displayName,
tags,
savedSearchId,
dashboardId,
tileId,
}: {
teamId: unknown;
channel: { type: 'webhook'; webhookId: string };
index: number;
displayName: string;
tags: string[];
savedSearchId?: string;
dashboardId?: string;
tileId?: string;
}) {
return {
team: teamId,
source: savedSearchId ? AlertSource.SAVED_SEARCH : AlertSource.TILE,
// Stored on the alert rather than left to derive from the referenced
// saved search or dashboard tile, matching what writers persist now.
displayName,
tags,
savedSearch: savedSearchId ?? null,
groupBy: null,
dashboard: dashboardId ?? null,
Expand Down Expand Up @@ -320,24 +328,32 @@ async function seed(count: number, tag: string) {
];

// --- Saved searches: up to MAX_ALERTS_PER_SAVED_SEARCH alerts each --------
const savedSearchPlans: { doc: Record<string, unknown>; alerts: number }[] =
[];
const savedSearchPlans: {
doc: Record<string, unknown>;
alerts: number;
name: string;
tags: string[];
}[] = [];
Comment thread
pulpdrew marked this conversation as resolved.
Resolved
for (let remaining = savedSearchAlertTotal; remaining > 0; ) {
const alerts = Math.min(
remaining,
randomInt(1, MAX_ALERTS_PER_SAVED_SEARCH),
);
const index = savedSearchPlans.length;
const name = `Seeded search ${stamp}-${index}`;
const tags = tagsFor(index);
savedSearchPlans.push({
alerts,
name,
tags,
doc: {
team: teamId,
name: `Seeded search ${stamp}-${index}`,
name,
select: '',
where: '',
whereLanguage: 'lucene',
source: sourceId,
tags: tagsFor(index),
tags,
},
});
remaining -= alerts;
Expand All @@ -355,6 +371,8 @@ async function seed(count: number, tag: string) {
teamId,
channel,
index: alertDocs.length,
displayName: plan.name,
tags: plan.tags,
savedSearchId,
}),
);
Expand All @@ -364,7 +382,8 @@ async function seed(count: number, tag: string) {
// --- Dashboards: up to MAX_TILES_PER_DASHBOARD tiles, <=1 alert per tile --
const dashboardPlans: {
doc: Record<string, unknown>;
alertedTileIds: string[];
alertedTiles: { id: string; displayName: string }[];
tags: string[];
}[] = [];
for (let remaining = tileAlertTotal; remaining > 0; ) {
const index = dashboardPlans.length;
Expand All @@ -374,13 +393,19 @@ async function seed(count: number, tag: string) {
);
// Some tiles are left un-alerted, which is the normal case on a dashboard.
const alertedCount = Math.min(remaining, randomInt(1, tileCount));
const name = `Seeded dashboard ${stamp}-${index}`;
const tags = tagsFor(index);
dashboardPlans.push({
alertedTileIds: tiles.slice(0, alertedCount).map(tile => tile.id),
alertedTiles: tiles.slice(0, alertedCount).map(tile => ({
id: tile.id,
displayName: formatTileAlertDisplayName(name, tile.config.name),
})),
tags,
doc: {
team: teamId,
name: `Seeded dashboard ${stamp}-${index}`,
name,
tiles,
tags: tagsFor(index),
tags,
filters: [],
},
});
Expand All @@ -392,14 +417,16 @@ async function seed(count: number, tag: string) {
);

for (const [plan, dashboardId] of zip(dashboardPlans, dashboardIds)) {
for (const tileId of plan.alertedTileIds) {
for (const tile of plan.alertedTiles) {
alertDocs.push(
makeAlertDoc({
teamId,
channel,
index: alertDocs.length,
displayName: tile.displayName,
tags: plan.tags,
dashboardId,
tileId,
tileId: tile.id,
}),
);
}
Expand All @@ -412,7 +439,6 @@ async function seed(count: number, tag: string) {
const state = String(alert.state);
byState.set(state, (byState.get(state) ?? 0) + 1);
}
const disabledCount = byState.get(AlertState.DISABLED) ?? 0;

console.log(
[
Expand All @@ -427,12 +453,6 @@ async function seed(count: number, tag: string) {
`Tagged "${tag}" — remove with: yarn seed:alerts --purge --tag ${tag}`,
].join('\n'),
);
if (disabledCount > 0) {
console.log(
`Note: the ${disabledCount} disabled alerts are not listed on the alerts ` +
'page, which only renders the triggered, pending and OK sections.',
);
}
}

async function main() {
Expand Down
Loading
Loading