Skip to content
10 changes: 10 additions & 0 deletions .changeset/alert-history-lookback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@hyperdx/api': patch
---

perf: bound the check-alerts previous-history lookup by the alert interval

The check-alerts task no longer scans 7 days of alert history per alert on
every tick. A one-row lookup per alert skips alerts already evaluated in the
current window. Non-grouped alerts take their previous state from that row,
and grouped alerts scan a window sized to their interval (1 to 7 days).
9 changes: 5 additions & 4 deletions packages/api/src/models/alertHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ AlertHistorySchema.index(
{ expireAfterSeconds: ms('30d') / 1000 },
);

// Used by getRecentAlertHistories (matches on alert, sorts by createdAt)
// Used by all alert/task reads: getRecentAlertHistories,
// getAlertTransitionsInRange, getLatestAlertHistories,
// getPreviousAlertHistories, and getConsecutiveWindowHistories — all match on
// alert equality with a createdAt range/sort, which this index serves with
// tight bounds.
AlertHistorySchema.index({ alert: 1, createdAt: -1 });

// Used by getPreviousAlertHistories (groups by {alert, group}, sorts by createdAt)
AlertHistorySchema.index({ alert: 1, group: 1, createdAt: -1 });

export default mongoose.model<IAlertHistory>(
'AlertHistory',
AlertHistorySchema,
Expand Down
Loading
Loading