Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RAM] Update rule status #140882

Merged
merged 42 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1dd3687
re-structure migrations code
XavierM Sep 13, 2022
d1ca8f0
change mappings to match new design
XavierM Sep 13, 2022
0b74e72
wip
XavierM Sep 14, 2022
bbfc35a
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Sep 16, 2022
5579627
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Sep 16, 2022
defdf85
Merge branch 'main' of github.com:elastic/kibana into 136039-rules-st…
XavierM Oct 17, 2022
1707107
wip to add a monitoring service
XavierM Oct 28, 2022
eb8ce8b
Merge branch 'main' of github.com:elastic/kibana into 136039-rules-st…
XavierM Oct 28, 2022
7ca2fc2
Merge branch '136039-rules-status' of github.com:XavierM/kibana into …
XavierM Oct 28, 2022
c882426
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Oct 28, 2022
0fe91eb
jiawei work on backend of rule
JiaweiWu Nov 1, 2022
6b4531f
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Nov 1, 2022
4a1de41
Fix conflicts
JiaweiWu Nov 1, 2022
6430691
Fix some of the missing functions used in rules_client
JiaweiWu Nov 1, 2022
4c57e09
Fix types and unit tests
JiaweiWu Nov 3, 2022
2151d4f
Removing running and fix associated tests
JiaweiWu Nov 3, 2022
aa1977c
Remove more references to running
JiaweiWu Nov 3, 2022
992cccb
Address comments and update task runner tests
JiaweiWu Nov 4, 2022
a907217
Fix and add more tests
JiaweiWu Nov 7, 2022
55f2cae
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 7, 2022
74269ac
Migration unit and integration tests, fixed some more tests
JiaweiWu Nov 7, 2022
6fdf08f
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 7, 2022
be1feb3
Fix types
JiaweiWu Nov 7, 2022
d404fd8
Fix tests and types
JiaweiWu Nov 8, 2022
8da8345
Fix SO migration snapshot, add more task runner tests
JiaweiWu Nov 8, 2022
1345ac2
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 8, 2022
daac93e
Fix type check
JiaweiWu Nov 9, 2022
9a70231
Fix type check
JiaweiWu Nov 9, 2022
f0b1c66
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 9, 2022
897ec3f
Fix more types
JiaweiWu Nov 10, 2022
cae7541
Merge branch 'main' of github.com:elastic/kibana into 136039-rules-st…
XavierM Nov 10, 2022
2ce6b6e
Address comments
JiaweiWu Nov 14, 2022
d0b5298
Update rule monitoring mock
JiaweiWu Nov 14, 2022
bbef4eb
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
1bc1db7
Merge branch '136039-rules-status' of github.com:XavierM/kibana into …
XavierM Nov 14, 2022
d9a6b9d
add more test
XavierM Nov 14, 2022
1869e17
unit test
XavierM Nov 14, 2022
c8e1da5
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
f2e4735
fix type
XavierM Nov 14, 2022
fad9da5
Merge branch '136039-rules-status' of github.com:XavierM/kibana into …
XavierM Nov 14, 2022
49d07a1
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
367b3d8
Merge branch 'main' into 136039-rules-status
kibanamachine Nov 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
Object {
"action": "7858e6d5a9f231bf23f6f2e57328eb0095b26735",
"action_task_params": "bbd38cbfd74bf6713586fe078e3fa92db2234299",
"alert": "48461f3375d9ba22882ea23a318b62a5b0921a9b",
"alert": "eefada4a02ce05962387c0679d7b292771a931c4",
"api_key_pending_invalidation": "9b4bc1235337da9a87ef05a1d1f4858b2a3b77c6",
"apm-indices": "ceb0870f3a74e2ffc3a1cd3a3c73af76baca0999",
"apm-server-schema": "2bfd2998d3873872e1366458ce553def85418f91",
Expand Down
52 changes: 44 additions & 8 deletions x-pack/plugins/alerting/common/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const RuleExecutionStatusValues = [
] as const;
export type RuleExecutionStatuses = typeof RuleExecutionStatusValues[number];

export const RuleLastRunOutcomeValues = ['succeeded', 'warning', 'failed'] as const;
export type RuleLastRunOutcomes = typeof RuleLastRunOutcomeValues[number];

export enum RuleExecutionStatusErrorReasons {
Read = 'read',
Decrypt = 'decrypt',
Expand Down Expand Up @@ -76,12 +79,25 @@ export interface RuleAction {

export interface RuleAggregations {
alertExecutionStatus: { [status: string]: number };
ruleLastRunOutcome: { [status: string]: number };
ruleEnabledStatus: { enabled: number; disabled: number };
ruleMutedStatus: { muted: number; unmuted: number };
ruleSnoozedStatus: { snoozed: number };
ruleTags: string[];
}

export interface RuleLastRun {
outcome: RuleLastRunOutcomes;
warning?: RuleExecutionStatusErrorReasons | RuleExecutionStatusWarningReasons | null;
outcomeMsg?: string | null;
alertsCount: {
active?: number | null;
new?: number | null;
recovered?: number | null;
ignored?: number | null;
};
}

export interface MappedParamsProperties {
risk_score?: number;
severity?: string;
Expand Down Expand Up @@ -116,6 +132,8 @@ export interface Rule<Params extends RuleTypeParams = never> {
snoozeSchedule?: RuleSnooze; // Remove ? when this parameter is made available in the public API
activeSnoozes?: string[];
isSnoozedUntil?: Date | null;
lastRun?: RuleLastRun | null;
nextRun?: Date | null;
}

export type SanitizedRule<Params extends RuleTypeParams = never> = Omit<Rule<Params>, 'apiKey'>;
Expand Down Expand Up @@ -175,16 +193,34 @@ export interface RuleMonitoringHistory extends SavedObjectAttributes {
success: boolean;
timestamp: number;
duration?: number;
outcome?: RuleLastRunOutcomes;
}

export interface RuleMonitoringCalculatedMetrics extends SavedObjectAttributes {
p50?: number;
p95?: number;
p99?: number;
success_ratio: number;
}

export interface RuleMonitoringLastRunMetrics extends SavedObjectAttributes {
duration?: number;
total_search_duration_ms?: number | null;
total_indexing_duration_ms?: number | null;
total_alerts_detected?: number | null;
total_alerts_created?: number | null;
gap_duration_s?: number | null;
}

export interface RuleMonitoringLastRun extends SavedObjectAttributes {
timestamp: string;
metrics: RuleMonitoringLastRunMetrics;
}

export interface RuleMonitoring extends SavedObjectAttributes {
execution: {
export interface RuleMonitoring {
run: {
history: RuleMonitoringHistory[];
calculated_metrics: {
p50?: number;
p95?: number;
p99?: number;
success_ratio: number;
};
calculated_metrics: RuleMonitoringCalculatedMetrics;
last_run: RuleMonitoringLastRun;
};
}
150 changes: 149 additions & 1 deletion x-pack/plugins/alerting/public/lib/common_transformations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { ApiRule, transformRule } from './common_transformations';
import { RuleExecutionStatusErrorReasons } from '../../common';
import { RuleExecutionStatusErrorReasons, RuleLastRunOutcomeValues } from '../../common';

beforeEach(() => jest.resetAllMocks());

Expand Down Expand Up @@ -54,6 +54,43 @@ describe('common_transformations', () => {
message: 'this is just a test',
},
},
monitoring: {
run: {
history: [
{
timestamp: dateExecuted.getTime(),
duration: 42,
success: false,
outcome: RuleLastRunOutcomeValues[2],
},
],
calculated_metrics: {
success_ratio: 0,
p50: 0,
p95: 42,
p99: 42,
},
last_run: {
timestamp: dateExecuted.toISOString(),
metrics: {
duration: 42,
total_search_duration_ms: 100,
},
},
},
},
last_run: {
outcome: RuleLastRunOutcomeValues[2],
outcome_msg: 'this is just a test',
warning: RuleExecutionStatusErrorReasons.Unknown,
alerts_count: {
new: 1,
active: 2,
recovered: 3,
ignored: 4,
},
},
next_run: dateUpdated.toISOString(),
};
expect(transformRule(apiRule)).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -89,12 +126,49 @@ describe('common_transformations', () => {
"status": "error",
},
"id": "some-id",
"lastRun": Object {
"alertsCount": Object {
"active": 2,
"ignored": 4,
"new": 1,
"recovered": 3,
},
"outcome": "failed",
"outcomeMsg": "this is just a test",
"warning": "unknown",
},
"monitoring": Object {
"run": Object {
"calculated_metrics": Object {
"p50": 0,
"p95": 42,
"p99": 42,
"success_ratio": 0,
},
"history": Array [
Object {
"duration": 42,
"outcome": "failed",
"success": false,
"timestamp": 1639571696789,
},
],
"last_run": Object {
"metrics": Object {
"duration": 42,
"total_search_duration_ms": 100,
},
"timestamp": "2021-12-15T12:34:56.789Z",
},
},
},
"muteAll": false,
"mutedInstanceIds": Array [
"bob",
"jim",
],
"name": "some-name",
"nextRun": 2021-12-15T12:34:55.789Z,
"notifyWhen": "onActiveAlert",
"params": Object {
"bar": "foo",
Expand Down Expand Up @@ -152,6 +226,43 @@ describe('common_transformations', () => {
last_execution_date: dateExecuted.toISOString(),
status: 'error',
},
monitoring: {
run: {
history: [
{
timestamp: dateExecuted.getTime(),
duration: 42,
success: false,
outcome: 'failed',
},
],
calculated_metrics: {
success_ratio: 0,
p50: 0,
p95: 42,
p99: 42,
},
last_run: {
timestamp: dateExecuted.toISOString(),
metrics: {
duration: 42,
total_search_duration_ms: 100,
},
},
},
},
last_run: {
outcome: 'failed',
outcome_msg: 'this is just a test',
warning: RuleExecutionStatusErrorReasons.Unknown,
alerts_count: {
new: 1,
active: 2,
recovered: 3,
ignored: 4,
},
},
next_run: dateUpdated.toISOString(),
};
expect(transformRule(apiRule)).toMatchInlineSnapshot(`
Object {
Expand All @@ -176,12 +287,49 @@ describe('common_transformations', () => {
"status": "error",
},
"id": "some-id",
"lastRun": Object {
"alertsCount": Object {
"active": 2,
"ignored": 4,
"new": 1,
"recovered": 3,
},
"outcome": "failed",
"outcomeMsg": "this is just a test",
"warning": "unknown",
},
"monitoring": Object {
"run": Object {
"calculated_metrics": Object {
"p50": 0,
"p95": 42,
"p99": 42,
"success_ratio": 0,
},
"history": Array [
Object {
"duration": 42,
"outcome": "failed",
"success": false,
"timestamp": 1639571696789,
},
],
"last_run": Object {
"metrics": Object {
"duration": 42,
"total_search_duration_ms": 100,
},
"timestamp": "2021-12-15T12:34:56.789Z",
},
},
},
"muteAll": false,
"mutedInstanceIds": Array [
"bob",
"jim",
],
"name": "some-name",
"nextRun": 2021-12-15T12:34:55.789Z,
"notifyWhen": "onActiveAlert",
"params": Object {},
"schedule": Object {
Expand Down
44 changes: 43 additions & 1 deletion x-pack/plugins/alerting/public/lib/common_transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
* 2.0.
*/
import { AsApiContract } from '@kbn/actions-plugin/common';
import { RuleExecutionStatus, Rule, RuleAction, RuleType } from '../../common';
import {
RuleExecutionStatus,
RuleMonitoring,
Rule,
RuleLastRun,
RuleAction,
RuleType,
} from '../../common';

function transformAction(input: AsApiContract<RuleAction>): RuleAction {
const { connector_type_id: actionTypeId, ...rest } = input;
Expand All @@ -27,6 +34,31 @@ function transformExecutionStatus(input: ApiRuleExecutionStatus): RuleExecutionS
};
}

function transformMonitoring(input: RuleMonitoring): RuleMonitoring {
const { run } = input;
const { last_run: lastRun, ...rest } = run;
const { timestamp, ...restLastRun } = lastRun;

return {
run: {
last_run: {
timestamp: input.run.last_run.timestamp,
...restLastRun,
},
...rest,
},
};
}

function transformLastRun(input: AsApiContract<RuleLastRun>): RuleLastRun {
const { outcome_msg: outcomeMsg, alerts_count: alertsCount, ...rest } = input;
return {
outcomeMsg,
alertsCount,
...rest,
};
}

// AsApiContract does not deal with object properties that also
// need snake -> camel conversion, Dates, are renamed, etc, so we do by hand
export type ApiRule = Omit<
Expand All @@ -37,13 +69,17 @@ export type ApiRule = Omit<
| 'updated_at'
| 'alert_type_id'
| 'muted_instance_ids'
| 'last_run'
| 'next_run'
> & {
execution_status: ApiRuleExecutionStatus;
actions: Array<AsApiContract<RuleAction>>;
created_at: string;
updated_at: string;
rule_type_id: string;
muted_alert_ids: string[];
last_run?: AsApiContract<RuleLastRun>;
next_run?: string;
};

export function transformRule(input: ApiRule): Rule {
XavierM marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -61,6 +97,9 @@ export function transformRule(input: ApiRule): Rule {
scheduled_task_id: scheduledTaskId,
execution_status: executionStatusAPI,
actions: actionsAPI,
next_run: nextRun,
last_run: lastRun,
monitoring: monitoring,
...rest
} = input;

Expand All @@ -78,6 +117,9 @@ export function transformRule(input: ApiRule): Rule {
executionStatus: transformExecutionStatus(executionStatusAPI),
actions: actionsAPI ? actionsAPI.map((action) => transformAction(action)) : [],
scheduledTaskId,
...(nextRun ? { nextRun: new Date(nextRun) } : {}),
...(monitoring ? { monitoring: transformMonitoring(monitoring) } : {}),
...(lastRun ? { lastRun: transformLastRun(lastRun) } : {}),
...rest,
};
}
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/alerting/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export {
ruleExecutionStatusToRaw,
ruleExecutionStatusFromRaw,
} from './rule_execution_status';
export { lastRunFromState, lastRunFromError, lastRunToRaw } from './last_run_status';
export {
updateMonitoring,
getDefaultMonitoring,
convertMonitoringFromRawAndVerify,
} from './monitoring';
export { getNextRun } from './next_run';
export { processAlerts } from './process_alerts';
export { createWrappedScopedClusterClientFactory } from './wrap_scoped_cluster_client';
export { isRuleSnoozed, getRuleSnoozeEndTime } from './is_rule_snoozed';
Expand Down
Loading