Repository navigation
feat(rpc): type and gate alarm trigger conditions - #988
izadoesdev merged 5 commits into
Conversation
|
@izadoesdev could you take a look when you have a chance? This covers the typed-trigger-conditions slice of #958 (step 1 of the delivery plan) — evaluators and the rest stay gated on your call on where they should run. |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@FindMalek is attempting to deploy a commit to the Databuddy OSS Team on Vercel. A member of the Team first needs to authorize it. |
|
|
fixed both. removed the unnecessary Record<string, unknown> annotation on updateData, it infers fine without it same as before this PR. and pulled the trigger resolution logic in the update handler into its own function with tests covering exactly the case you flagged - sending only triggerConditions (or only triggerType) and resolving the other from the stored row, which is what the monitor-linking flow does. |
|
@greptile review |
This comment has been minimized.
This comment has been minimized.
|
fixed, builds updateData with spread now instead of Object.assign |
|
@greptile review |
triggerConditions was an untyped z.record(unknown), and create/update accepted traffic_spike and error_rate trigger types with no evaluator for either, so those alarms saved successfully but could never fire. Add a discriminated-union schema keyed on triggerType (uptime requires monitorIds: string[], matching current informal usage; traffic_spike and error_rate are typed per the shapes proposed in databuddy-analytics#958 for when an evaluator exists). Reject create/update for any trigger type without an evaluator with a clear error instead of silently accepting it. Read paths stay untyped so existing rows of any shape remain listable.
Removes an unnecessary explicit Record<string, unknown> annotation on updateData (the previous inferred type already worked and matches the type this repo already used here before). Extracts the trigger-type/conditions resolution in alarms.update into resolveTriggerUpdate, a pure function with its own tests, covering the case the update handler didn't have direct coverage for: sending only triggerConditions (or only triggerType) and resolving the other side from the currently stored row, which is what the monitor-linking flow actually does.
e3d9d07 to
9d5078f
Compare
696012e
into
databuddy-analytics:staging
Summary
Addresses part of #958 — the "typed trigger conditions plus RPC validation" slice from the delivery plan, not the full issue. Evaluators,
alarm_states, scheduling, and the dashboard picker/preview stay out of scope, pending the maintainer's answer to the issue's open architecture question ("where should the evaluator run?").triggerConditions: z.record(z.string(), z.unknown())with a discriminated union ontriggerType, following the same pattern already used fordestinations(z.discriminatedUnion("type", [...])) in this file:uptime→{ monitorIds: string[] }(defaults to[]), matching what the code already expected informally (alarmMonitorIdsin the dashboard anduptime-transition-alerts.tsalready just checkArray.isArray(...)).traffic_spike/error_rate→ typed per the shapes proposed in the issue, so the contract is documented, but see below — they're not creatable yet.alarms.createandalarms.updatenow reject any trigger type other thanuptimewith aBAD_REQUESTexplaining there's no evaluator for it yet, instead of silently accepting and persisting a dead alarm.updateresolves the effective trigger type/conditions against the existing row when only one of the two fields is sent (e.g. the monitor-linking flow inmonitor-sheet.tsx, which patchestriggerConditionswithouttriggerType).alarms.list, thealarms.create/updateoutput schema) are unchanged and stay permissive (z.record), so any row written before this change — regardless of trigger type or conditions shape — stays listable. Only writes are tightened.packages/rpc/src/routers/alarms.test.tscovering: valid/defaulteduptimeconditions, malformeduptimeconditions, and rejection oftraffic_spike/error_rate/unknown types.Verification
alarm-sheet.tsxalways hardcodestriggerType: "uptime"on create and has no UI for selectingtraffic_spike/error_rate— there's nothing to remove, the tightened API doesn't break any existing UI flow.alarm-sheet.tsxcreates withtriggerConditions: {}, thenmonitor-sheet.tsxlater patches inmonitorIdsviaalarms.update): still works becausemonitorIdsdefaults to[].bun run check-types,bun run lint, andpackages/rpc's fullbun run test(223 pass) locally against Postgres/Redis/ClickHouse. The pre-push hook additionally ran the full monorepobun run test(1101+ pass across all packages) — all green.uptime).Out of scope (left for follow-up PRs, per the issue's delivery plan)
traffic_spike/error_rateevaluators,alarm_statestable, and BullMQ scheduling (step 2/3 in the issue).Disclosure: implemented with AI assistance (Claude Code); verified and tested by hand before submitting.
Summary by cubic
Types and gates alarm trigger conditions so
alarms.create/updatereject alarms nothing can evaluate. Previously any trigger type was accepted with untyped conditions, sotraffic_spikeanderror_ratealarms saved but could never fire. Writes now validatetriggerConditionsagainsttriggerType:uptimerequiresmonitorIds: string[](defaulting to[]), and other types get aBAD_REQUEST. Webhook header blocking now reuses the sharedFORBIDDEN_WEBHOOK_HEADERSexport so alarms reject the headers delivery actually drops. Read paths stay permissive, so alarms written before this change remain listable.updateresolves the effective trigger type and conditions against the stored row via a pureresolveTriggerUpdatehelper, keeping single-field updates like the monitor-linking flow working. Addspackages/rpc/src/routers/alarms.test.tscovering defaulted and malformeduptimeconditions, single-field resolution, and rejection of unsupported trigger types.Written for commit 9d5078f. Summary will update on new commits.