[ResponseOps][Rules] Allow users to create a snooze schedule for rule via API#210584
[ResponseOps][Rules] Allow users to create a snooze schedule for rule via API#210584js-jankisalvi merged 39 commits intoelastic:mainfrom
Conversation
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --include-path /api/dashboards --update'
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --include-path /api/dashboards --update'
|
Pinging @elastic/response-ops (Team:ResponseOps) |
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --include-path /api/dashboards --update'
There was a problem hiding this comment.
Great work! Very meticulous with great testing coverage. I left some minor comments, and I would like to discuss the following:
- If the user puts
48has a duration, we return2dbecause of the transformation we do. As we do not persist with the initial request, I do not think there is anything we can do. I just wanted to call it out. I think it is fine as long as we are consistent and always return the same. Maybe we have to document it. - Users can define seconds in the duration field. This can lead to the following interesting scenario: I set the duration to
129600s, which will be converted to1.5d. How do you think we should handle this? Is it okay to leave it as it is?
x-pack/platform/plugins/shared/alerting/common/routes/schedule/schema/v1.ts
Outdated
Show resolved
Hide resolved
| } | ||
| } | ||
|
|
||
| if (duration === '-1' && recurring) { |
There was a problem hiding this comment.
@cnasikas What do you think of having duration: undefined instead of duration: -1? Isn't that clearer?
An answer for the future (aside from the fact that we will not support it initially): I think it is better if we force users to be explicit when they want to schedule the rule indefinitely. Having undefined to mean indefinitely can easily lead to mistakes, where users forget to put the duration and accidentally snooze a critical rule forever.
| import moment from 'moment'; | ||
| import { DURATION_REGEX, INTERVAL_FREQUENCY_REGEXP } from '../../constants'; | ||
|
|
||
| export const validateSchedule = (schedule: { |
There was a problem hiding this comment.
How does the rrule library handles this scenario?
| start: schema.string({ | ||
| validate: validateStartDateV1, | ||
| meta: { | ||
| description: 'The start date and time of the schedule in ISO 8601 format.', |
There was a problem hiding this comment.
nit: Should we mention that it has to be in the UTC format (with the z in the end) and use the timezone field to specify the timezone?
There was a problem hiding this comment.
Updated to use UTC timezone format and added example
x-pack/platform/plugins/shared/alerting/common/routes/schedule/schema/v1.ts
Outdated
Show resolved
Hide resolved
...latform/plugins/shared/alerting/server/routes/rule/apis/snooze/external/snooze_rule_route.ts
Outdated
Show resolved
Hide resolved
x-pack/platform/plugins/shared/alerting/common/routes/schedule/transforms/custom_to_rrule/v1.ts
Outdated
Show resolved
Hide resolved
...latform/plugins/shared/alerting/server/routes/rule/apis/snooze/external/snooze_rule_route.ts
Outdated
Show resolved
Hide resolved
...rm/plugins/shared/alerting/server/routes/rule/apis/snooze/external/snooze_rule_route.test.ts
Outdated
Show resolved
Hide resolved
...latform/plugins/shared/alerting/server/routes/rule/apis/snooze/internal/snooze_rule_route.ts
Show resolved
Hide resolved
x-pack/platform/plugins/shared/alerting/common/routes/schedule/constants.ts
Outdated
Show resolved
Hide resolved
| import moment from 'moment'; | ||
| import { DURATION_REGEX, INTERVAL_FREQUENCY_REGEXP } from '../../constants'; | ||
|
|
||
| export const validateSchedule = (schedule: { |
There was a problem hiding this comment.
How then do we validate the duration, does it have to match exactly the difference between the start and end date?
end is optional, it gets converted to the until property in RRULE, meaning "recur until this date." Maybe we should rename end to until to reduce confusion? Making it part of recurring.end is a little helpful but I think if @adcoelho is getting confused then the user might also.
It's possible for the duration to go on longer than the end date, the rrule library won't throw an error or anything, it just won't make the rule recur ever.
| .asDays(); | ||
|
|
||
| if (intervalInDays && interval && durationInDays >= intervalInDays) { | ||
| return `Recurrence every ${every} must be longer than the duration ${duration}.`; |
There was a problem hiding this comment.
Good, excellent, we have absolutely had SDHs come up around this before. This would technically work but let's not let the user hurt themselves.
…t --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules --include-path /api/actions --include-path /api/security/role --include-path /api/spaces --include-path /api/fleet --include-path /api/dashboards --update'
I found a way to return same duration string passed by user in request. We pass transformRRuleToCustomSchedule({
snoozeSchedule: createdSchedule,
originalDuration: customSchedule.duration,
}) |
I like the idea, but instead of passing it to the Also, could you please open an issue to persist the new format to ES along with the old rrule format? |
|
💚 Build Succeeded
Metrics [docs]Unknown metric groupsESLint disabled line counts
Total ESLint disabled count
History
|
|
Starting backport for target branches: 8.x |
💔 All backports failed
Manual backportTo create the backport manually run: Questions ?Please refer to the Backport tool documentation |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
… via API (elastic#210584) ## Summary Resolves first part of elastic#198783 (snooze API) Creates a public API for adding snooze schedule to rules. For this purpose we created new schedule schema which will be used as standard schedule schema across rules and alerting framework. **Note** The code to be reviewed for public API is under `common/routes/schedule` and inside `external` folders. Rest of the code is just moving existing internal API route and its code to `/internal` folders. ### Checklist - [x] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### How to test - Create a rule in kibana - Snooze that rule via new public API ```typescript Path: https://localhost:5601/api/alerting/rule/<rule_id>/snooze_schedule Method: POST Body: { "schedule": { "custom": { "start": "2025-02-25T18:00:00.000Z", "duration": "15m", "recurring": { "every": "1w", "onWeekDay": ["TU", "FR"], "occurrences": 10 } } } } ``` - Verify various snooze schedule scenarios are generated correctly #### Flaky test runner: https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8009 ### Release note Allow users to create a snooze schedule for rule via API --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: lcawl <lcawley@elastic.co> (cherry picked from commit 14871c3) # Conflicts: # x-pack/platform/plugins/shared/alerting/tsconfig.json # x-pack/test/alerting_api_integration/spaces_only/tests/alerting/group4/snooze.ts
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
…r rule via API (#210584) (#214566) # Backport This will backport the following commits from `main` to `8.x`: - [[ResponseOps][Rules] Allow users to create a snooze schedule for rule via API (#210584)](#210584) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Janki Salvi","email":"117571355+js-jankisalvi@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-03-13T15:03:11Z","message":"[ResponseOps][Rules] Allow users to create a snooze schedule for rule via API (#210584)\n\n## Summary\n\nResolves first part of https://github.com/elastic/kibana/issues/198783\n(snooze API)\nCreates a public API for adding snooze schedule to rules.\nFor this purpose we created new schedule schema which will be used as\nstandard schedule schema across rules and alerting framework.\n\n**Note** \nThe code to be reviewed for public API is under `common/routes/schedule`\nand inside `external` folders.\nRest of the code is just moving existing internal API route and its code\nto `/internal` folders.\n\n\n### Checklist\n\n- [x]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n### How to test\n- Create a rule in kibana\n- Snooze that rule via new public API\n```typescript\nPath: https://localhost:5601/api/alerting/rule/<rule_id>/snooze_schedule\nMethod: POST\nBody: \n{\n \"schedule\": { \n \"custom\": {\n \"start\": \"2025-02-25T18:00:00.000Z\",\n \"duration\": \"15m\",\n \"recurring\": {\n \"every\": \"1w\",\n \"onWeekDay\": [\"TU\", \"FR\"],\n \"occurrences\": 10\n }\n }\n }\n}\n``` \n- Verify various snooze schedule scenarios are generated correctly\n\n#### Flaky test runner:\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8009\n\n### Release note\nAllow users to create a snooze schedule for rule via API\n\n---------\n\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>\nCo-authored-by: lcawl <lcawley@elastic.co>","sha":"14871c31aaeaacef22b3498343ec0f23b3c7d79f","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","Team:ResponseOps","Feature:Alerting/RulesManagement","backport:version","v9.1.0","v8.19.0"],"title":"[ResponseOps][Rules] Allow users to create a snooze schedule for rule via API","number":210584,"url":"https://github.com/elastic/kibana/pull/210584","mergeCommit":{"message":"[ResponseOps][Rules] Allow users to create a snooze schedule for rule via API (#210584)\n\n## Summary\n\nResolves first part of https://github.com/elastic/kibana/issues/198783\n(snooze API)\nCreates a public API for adding snooze schedule to rules.\nFor this purpose we created new schedule schema which will be used as\nstandard schedule schema across rules and alerting framework.\n\n**Note** \nThe code to be reviewed for public API is under `common/routes/schedule`\nand inside `external` folders.\nRest of the code is just moving existing internal API route and its code\nto `/internal` folders.\n\n\n### Checklist\n\n- [x]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n### How to test\n- Create a rule in kibana\n- Snooze that rule via new public API\n```typescript\nPath: https://localhost:5601/api/alerting/rule/<rule_id>/snooze_schedule\nMethod: POST\nBody: \n{\n \"schedule\": { \n \"custom\": {\n \"start\": \"2025-02-25T18:00:00.000Z\",\n \"duration\": \"15m\",\n \"recurring\": {\n \"every\": \"1w\",\n \"onWeekDay\": [\"TU\", \"FR\"],\n \"occurrences\": 10\n }\n }\n }\n}\n``` \n- Verify various snooze schedule scenarios are generated correctly\n\n#### Flaky test runner:\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8009\n\n### Release note\nAllow users to create a snooze schedule for rule via API\n\n---------\n\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>\nCo-authored-by: lcawl <lcawley@elastic.co>","sha":"14871c31aaeaacef22b3498343ec0f23b3c7d79f"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/210584","number":210584,"mergeCommit":{"message":"[ResponseOps][Rules] Allow users to create a snooze schedule for rule via API (#210584)\n\n## Summary\n\nResolves first part of https://github.com/elastic/kibana/issues/198783\n(snooze API)\nCreates a public API for adding snooze schedule to rules.\nFor this purpose we created new schedule schema which will be used as\nstandard schedule schema across rules and alerting framework.\n\n**Note** \nThe code to be reviewed for public API is under `common/routes/schedule`\nand inside `external` folders.\nRest of the code is just moving existing internal API route and its code\nto `/internal` folders.\n\n\n### Checklist\n\n- [x]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n### How to test\n- Create a rule in kibana\n- Snooze that rule via new public API\n```typescript\nPath: https://localhost:5601/api/alerting/rule/<rule_id>/snooze_schedule\nMethod: POST\nBody: \n{\n \"schedule\": { \n \"custom\": {\n \"start\": \"2025-02-25T18:00:00.000Z\",\n \"duration\": \"15m\",\n \"recurring\": {\n \"every\": \"1w\",\n \"onWeekDay\": [\"TU\", \"FR\"],\n \"occurrences\": 10\n }\n }\n }\n}\n``` \n- Verify various snooze schedule scenarios are generated correctly\n\n#### Flaky test runner:\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8009\n\n### Release note\nAllow users to create a snooze schedule for rule via API\n\n---------\n\nCo-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>\nCo-authored-by: lcawl <lcawley@elastic.co>","sha":"14871c31aaeaacef22b3498343ec0f23b3c7d79f"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT-->
… via API (elastic#210584) ## Summary Resolves first part of elastic#198783 (snooze API) Creates a public API for adding snooze schedule to rules. For this purpose we created new schedule schema which will be used as standard schedule schema across rules and alerting framework. **Note** The code to be reviewed for public API is under `common/routes/schedule` and inside `external` folders. Rest of the code is just moving existing internal API route and its code to `/internal` folders. ### Checklist - [x] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### How to test - Create a rule in kibana - Snooze that rule via new public API ```typescript Path: https://localhost:5601/api/alerting/rule/<rule_id>/snooze_schedule Method: POST Body: { "schedule": { "custom": { "start": "2025-02-25T18:00:00.000Z", "duration": "15m", "recurring": { "every": "1w", "onWeekDay": ["TU", "FR"], "occurrences": 10 } } } } ``` - Verify various snooze schedule scenarios are generated correctly #### Flaky test runner: https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8009 ### Release note Allow users to create a snooze schedule for rule via API --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: lcawl <lcawley@elastic.co>
Summary
Resolves first part of #198783 (snooze API)
Creates a public API for adding snooze schedule to rules.
For this purpose we created new schedule schema which will be used as standard schedule schema across rules and alerting framework.
Note
The code to be reviewed for public API is under
common/routes/scheduleand insideexternalfolders.Rest of the code is just moving existing internal API route and its code to
/internalfolders.Checklist
How to test
Flaky test runner:
https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8009
Release note
Allow users to create a snooze schedule for rule via API