diff --git a/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.test.ts b/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.test.ts index 75022427bea27..80090effca9d1 100644 --- a/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.test.ts +++ b/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.test.ts @@ -11,6 +11,7 @@ import { formatExecutionLogResult, formatSortForBucketSort, formatSortForTermSort, + ExecutionUuidAggResult, } from './get_execution_log_aggregation'; describe('formatSortForBucketSort', () => { @@ -128,92 +129,111 @@ describe('getExecutionLogAggregation', () => { sort: [{ timestamp: { order: 'asc' } }, { execution_duration: { order: 'desc' } }], }) ).toEqual({ - executionUuidCardinality: { cardinality: { field: 'kibana.alert.rule.execution.uuid' } }, - executionUuid: { - terms: { - field: 'kibana.alert.rule.execution.uuid', - size: 1000, - order: [ - { 'ruleExecution>executeStartTime': 'asc' }, - { 'ruleExecution>executionDuration': 'desc' }, - ], + excludeExecuteStart: { + filter: { + bool: { + must_not: [ + { + term: { + 'event.action': 'execute-start', + }, + }, + ], + }, }, aggs: { - executionUuidSorted: { - bucket_sort: { - sort: [ - { 'ruleExecution>executeStartTime': { order: 'asc' } }, - { 'ruleExecution>executionDuration': { order: 'desc' } }, + executionUuidCardinality: { cardinality: { field: 'kibana.alert.rule.execution.uuid' } }, + executionUuid: { + terms: { + field: 'kibana.alert.rule.execution.uuid', + size: 1000, + order: [ + { 'ruleExecution>executeStartTime': 'asc' }, + { 'ruleExecution>executionDuration': 'desc' }, ], - from: 10, - size: 10, - gap_policy: 'insert_zeros', - }, - }, - alertCounts: { - filters: { - filters: { - newAlerts: { match: { 'event.action': 'new-instance' } }, - activeAlerts: { match: { 'event.action': 'active-instance' } }, - recoveredAlerts: { match: { 'event.action': 'recovered-instance' } }, - }, - }, - }, - actionExecution: { - filter: { - bool: { - must: [ - { match: { 'event.action': 'execute' } }, - { match: { 'event.provider': 'actions' } }, - ], - }, - }, - aggs: { actionOutcomes: { terms: { field: 'event.outcome', size: 2 } } }, - }, - ruleExecution: { - filter: { - bool: { - must: [ - { match: { 'event.action': 'execute' } }, - { match: { 'event.provider': 'alerting' } }, - ], - }, }, aggs: { - executeStartTime: { min: { field: 'event.start' } }, - scheduleDelay: { - max: { - field: 'kibana.task.schedule_delay', + executionUuidSorted: { + bucket_sort: { + sort: [ + { 'ruleExecution>executeStartTime': { order: 'asc' } }, + { 'ruleExecution>executionDuration': { order: 'desc' } }, + ], + from: 10, + size: 10, + gap_policy: 'insert_zeros', }, }, - totalSearchDuration: { - max: { field: 'kibana.alert.rule.execution.metrics.total_search_duration_ms' }, - }, - esSearchDuration: { - max: { field: 'kibana.alert.rule.execution.metrics.es_search_duration_ms' }, - }, - numTriggeredActions: { - max: { field: 'kibana.alert.rule.execution.metrics.number_of_triggered_actions' }, + alertCounts: { + filters: { + filters: { + newAlerts: { match: { 'event.action': 'new-instance' } }, + activeAlerts: { match: { 'event.action': 'active-instance' } }, + recoveredAlerts: { match: { 'event.action': 'recovered-instance' } }, + }, + }, }, - numScheduledActions: { - max: { field: 'kibana.alert.rule.execution.metrics.number_of_scheduled_actions' }, + actionExecution: { + filter: { + bool: { + must: [ + { match: { 'event.action': 'execute' } }, + { match: { 'event.provider': 'actions' } }, + ], + }, + }, + aggs: { actionOutcomes: { terms: { field: 'event.outcome', size: 2 } } }, }, - executionDuration: { max: { field: 'event.duration' } }, - outcomeAndMessage: { - top_hits: { - size: 1, - _source: { includes: ['event.outcome', 'message', 'error.message'] }, + ruleExecution: { + filter: { + bool: { + must: [ + { match: { 'event.action': 'execute' } }, + { match: { 'event.provider': 'alerting' } }, + ], + }, + }, + aggs: { + executeStartTime: { min: { field: 'event.start' } }, + scheduleDelay: { + max: { + field: 'kibana.task.schedule_delay', + }, + }, + totalSearchDuration: { + max: { field: 'kibana.alert.rule.execution.metrics.total_search_duration_ms' }, + }, + esSearchDuration: { + max: { field: 'kibana.alert.rule.execution.metrics.es_search_duration_ms' }, + }, + numTriggeredActions: { + max: { + field: 'kibana.alert.rule.execution.metrics.number_of_triggered_actions', + }, + }, + numScheduledActions: { + max: { + field: 'kibana.alert.rule.execution.metrics.number_of_scheduled_actions', + }, + }, + executionDuration: { max: { field: 'event.duration' } }, + outcomeAndMessage: { + top_hits: { + size: 1, + _source: { includes: ['event.outcome', 'message', 'error.message'] }, + }, + }, }, }, - }, - }, - timeoutMessage: { - filter: { - bool: { - must: [ - { match: { 'event.action': 'execute-timeout' } }, - { match: { 'event.provider': 'alerting' } }, - ], + timeoutMessage: { + filter: { + bool: { + must: [ + { match: { 'event.action': 'execute-timeout' } }, + { match: { 'event.provider': 'alerting' } }, + ], + }, + }, }, }, }, @@ -230,188 +250,202 @@ describe('formatExecutionLogResult', () => { data: [], }); }); + test('should return empty results if aggregations.excludeExecuteStart are undefined', () => { + expect( + formatExecutionLogResult({ + aggregations: { excludeExecuteStart: undefined as unknown as ExecutionUuidAggResult }, + }) + ).toEqual({ + total: 0, + data: [], + }); + }); test('should format results correctly', () => { const results = { aggregations: { - executionUuid: { + excludeExecuteStart: { meta: {}, - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: '6705da7d-2635-499d-a6a8-1aee1ae1eac9', - doc_count: 27, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + doc_count: 875, + executionUuid: { + meta: {}, + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: '6705da7d-2635-499d-a6a8-1aee1ae1eac9', + doc_count: 27, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { + doc_count: 5, + }, + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 0, + }, }, - newAlerts: { - doc_count: 5, + }, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 0, + numScheduledActions: { + value: 5.0, }, - }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ - { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'S4wIZX8B8TGQpG7XQZns', - _score: 1.0, - _source: { - event: { - outcome: 'success', + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'S4wIZX8B8TGQpG7XQZns', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, + ], + }, + }, + scheduleDelay: { + value: 3.074e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.056e9, + }, + executeStartTime: { + value: 1.646667512617e12, + value_as_string: '2022-03-07T15:38:32.617Z', + }, + }, + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.074e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.056e9, - }, - executeStartTime: { - value: 1.646667512617e12, - value_as_string: '2022-03-07T15:38:32.617Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', + { + key: '41b2755e-765a-4044-9745-b03875d5e79a', + doc_count: 32, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { doc_count: 5, }, - ], + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 5, + }, + }, }, - }, - }, - { - key: '41b2755e-765a-4044-9745-b03875d5e79a', - doc_count: 32, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - newAlerts: { - doc_count: 5, + numScheduledActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 5, + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'a4wIZX8B8TGQpG7Xwpnz', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", + }, + }, + ], + }, + }, + scheduleDelay: { + value: 3.126e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.165e9, + }, + executeStartTime: { + value: 1.646667545604e12, + value_as_string: '2022-03-07T15:39:05.604Z', }, }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'a4wIZX8B8TGQpG7Xwpnz', - _score: 1.0, - _source: { - event: { - outcome: 'success', - }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", - }, + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.126e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.165e9, - }, - executeStartTime: { - value: 1.646667545604e12, - value_as_string: '2022-03-07T15:39:05.604Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', - doc_count: 5, - }, - ], - }, - }, - }, - ], - }, - executionUuidCardinality: { - value: 374, + ], + }, + executionUuidCardinality: { + value: 374, + }, }, }, }; @@ -463,188 +497,192 @@ describe('formatExecutionLogResult', () => { test('should format results correctly with rule execution errors', () => { const results = { aggregations: { - executionUuid: { + excludeExecuteStart: { meta: {}, - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: '6705da7d-2635-499d-a6a8-1aee1ae1eac9', - doc_count: 27, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + doc_count: 875, + executionUuid: { + meta: {}, + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: '6705da7d-2635-499d-a6a8-1aee1ae1eac9', + doc_count: 27, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { + doc_count: 5, + }, + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 0, + }, }, - newAlerts: { - doc_count: 5, + }, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 0, + numScheduledActions: { + value: 5.0, }, - }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ - { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'S4wIZX8B8TGQpG7XQZns', - _score: 1.0, - _source: { - event: { - outcome: 'failure', - }, - message: - "rule execution failure: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", - error: { - message: 'I am erroring in rule execution!!', + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'S4wIZX8B8TGQpG7XQZns', + _score: 1.0, + _source: { + event: { + outcome: 'failure', + }, + message: + "rule execution failure: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", + error: { + message: 'I am erroring in rule execution!!', + }, }, }, + ], + }, + }, + scheduleDelay: { + value: 3.074e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.056e9, + }, + executeStartTime: { + value: 1.646667512617e12, + value_as_string: '2022-03-07T15:38:32.617Z', + }, + }, + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.074e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.056e9, - }, - executeStartTime: { - value: 1.646667512617e12, - value_as_string: '2022-03-07T15:38:32.617Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', + { + key: '41b2755e-765a-4044-9745-b03875d5e79a', + doc_count: 32, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { doc_count: 5, }, - ], + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 5, + }, + }, }, - }, - }, - { - key: '41b2755e-765a-4044-9745-b03875d5e79a', - doc_count: 32, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - newAlerts: { - doc_count: 5, + numScheduledActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 5, + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'a4wIZX8B8TGQpG7Xwpnz', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", + }, + }, + ], + }, + }, + scheduleDelay: { + value: 3.126e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.165e9, + }, + executeStartTime: { + value: 1.646667545604e12, + value_as_string: '2022-03-07T15:39:05.604Z', }, }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'a4wIZX8B8TGQpG7Xwpnz', - _score: 1.0, - _source: { - event: { - outcome: 'success', - }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", - }, + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.126e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.165e9, - }, - executeStartTime: { - value: 1.646667545604e12, - value_as_string: '2022-03-07T15:39:05.604Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', - doc_count: 5, - }, - ], - }, - }, - }, - ], - }, - executionUuidCardinality: { - value: 374, + ], + }, + executionUuidCardinality: { + value: 374, + }, }, }, }; @@ -696,180 +734,184 @@ describe('formatExecutionLogResult', () => { test('should format results correctly when execution timeouts occur', () => { const results = { aggregations: { - executionUuid: { + excludeExecuteStart: { meta: {}, - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: '09b5aeab-d50d-43b2-88e7-f1a20f682b3f', - doc_count: 3, - timeoutMessage: { - meta: {}, - doc_count: 1, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 0, + doc_count: 875, + executionUuid: { + meta: {}, + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: '09b5aeab-d50d-43b2-88e7-f1a20f682b3f', + doc_count: 3, + timeoutMessage: { + meta: {}, + doc_count: 1, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { + doc_count: 0, + }, + newAlerts: { + doc_count: 0, + }, + recoveredAlerts: { + doc_count: 0, + }, }, - newAlerts: { - doc_count: 0, + }, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 0.0, }, - recoveredAlerts: { - doc_count: 0, + numScheduledActions: { + value: 0.0, }, - }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 0.0, - }, - numScheduledActions: { - value: 0.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ - { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'dJkWa38B1ylB1EvsAckB', - _score: 1.0, - _source: { - event: { - outcome: 'success', + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'dJkWa38B1ylB1EvsAckB', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, - }, - ], + ], + }, + }, + scheduleDelay: { + value: 3.074e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.0279e10, + }, + executeStartTime: { + value: 1.646769067607e12, + value_as_string: '2022-03-08T19:51:07.607Z', }, }, - scheduleDelay: { - value: 3.074e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.0279e10, - }, - executeStartTime: { - value: 1.646769067607e12, - value_as_string: '2022-03-08T19:51:07.607Z', + actionExecution: { + meta: {}, + doc_count: 0, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [], + }, }, }, - actionExecution: { - meta: {}, - doc_count: 0, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [], + { + key: '41b2755e-765a-4044-9745-b03875d5e79a', + doc_count: 32, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { + doc_count: 5, + }, + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 5, + }, + }, }, - }, - }, - { - key: '41b2755e-765a-4044-9745-b03875d5e79a', - doc_count: 32, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - newAlerts: { - doc_count: 5, + numScheduledActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 5, + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'a4wIZX8B8TGQpG7Xwpnz', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", + }, + }, + ], + }, + }, + scheduleDelay: { + value: 3.126e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.165e9, + }, + executeStartTime: { + value: 1.646667545604e12, + value_as_string: '2022-03-07T15:39:05.604Z', }, }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'a4wIZX8B8TGQpG7Xwpnz', - _score: 1.0, - _source: { - event: { - outcome: 'success', - }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", - }, + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.126e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.165e9, - }, - executeStartTime: { - value: 1.646667545604e12, - value_as_string: '2022-03-07T15:39:05.604Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', - doc_count: 5, - }, - ], - }, - }, - }, - ], - }, - executionUuidCardinality: { - value: 374, + ], + }, + executionUuidCardinality: { + value: 374, + }, }, }, }; @@ -921,185 +963,189 @@ describe('formatExecutionLogResult', () => { test('should format results correctly when action errors occur', () => { const results = { aggregations: { - executionUuid: { + excludeExecuteStart: { meta: {}, - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'ecf7ac4c-1c15-4a1d-818a-cacbf57f6158', - doc_count: 32, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + doc_count: 875, + executionUuid: { + meta: {}, + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: 'ecf7ac4c-1c15-4a1d-818a-cacbf57f6158', + doc_count: 32, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { + doc_count: 5, + }, + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 5, + }, }, - newAlerts: { - doc_count: 5, + }, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 5, + numScheduledActions: { + value: 5.0, }, - }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ - { - _index: '.kibana-event-log-8.2.0-000001', - _id: '7xKcb38BcntAq5ycFwiu', - _score: 1.0, - _source: { - event: { - outcome: 'success', + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: '7xKcb38BcntAq5ycFwiu', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, + ], + }, + }, + scheduleDelay: { + value: 3.126e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.374e9, + }, + executeStartTime: { + value: 1.646844973039e12, + value_as_string: '2022-03-09T16:56:13.039Z', + }, + }, + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: 'failure', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.126e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.374e9, - }, - executeStartTime: { - value: 1.646844973039e12, - value_as_string: '2022-03-09T16:56:13.039Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'failure', + { + key: '61bb867b-661a-471f-bf92-23471afa10b3', + doc_count: 32, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { doc_count: 5, }, - ], + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 5, + }, + }, }, - }, - }, - { - key: '61bb867b-661a-471f-bf92-23471afa10b3', - doc_count: 32, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - newAlerts: { - doc_count: 5, + numScheduledActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 5, + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'zRKbb38BcntAq5ycOwgk', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", + }, + }, + ], + }, + }, + scheduleDelay: { + value: 3.133e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 4.18e8, + }, + executeStartTime: { + value: 1.646844917518e12, + value_as_string: '2022-03-09T16:55:17.518Z', }, }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'zRKbb38BcntAq5ycOwgk', - _score: 1.0, - _source: { - event: { - outcome: 'success', - }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", - }, + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.133e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 4.18e8, - }, - executeStartTime: { - value: 1.646844917518e12, - value_as_string: '2022-03-09T16:55:17.518Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', - doc_count: 5, - }, - ], - }, - }, - }, - ], - }, - executionUuidCardinality: { - value: 417, + ], + }, + executionUuidCardinality: { + value: 417, + }, }, }, }; diff --git a/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.ts b/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.ts index 6f8d0d8059b69..fbe72508dab2b 100644 --- a/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.ts +++ b/x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.ts @@ -68,10 +68,15 @@ interface IExecutionUuidAggBucket extends estypes.AggregationsStringTermsBucketK }; } -interface ExecutionUuidAggResult +export interface ExecutionUuidAggResult extends estypes.AggregationsAggregateBase { buckets: TBucket[]; } + +interface ExcludeExecuteStartAggResult extends estypes.AggregationsAggregateBase { + executionUuid: ExecutionUuidAggResult; + executionUuidCardinality: estypes.AggregationsCardinalityAggregate; +} export interface IExecutionLogAggOptions { page: number; perPage: number; @@ -112,104 +117,119 @@ export function getExecutionLogAggregation({ page, perPage, sort }: IExecutionLo } return { - // Get total number of executions - executionUuidCardinality: { - cardinality: { - field: EXECUTION_UUID_FIELD, - }, - }, - executionUuid: { - // Bucket by execution UUID - terms: { - field: EXECUTION_UUID_FIELD, - size: DEFAULT_MAX_BUCKETS_LIMIT, - order: formatSortForTermSort(sort), + excludeExecuteStart: { + filter: { + bool: { + must_not: [ + { + term: { + [ACTION_FIELD]: 'execute-start', + }, + }, + ], + }, }, aggs: { - // Bucket sort to allow paging through executions - executionUuidSorted: { - bucket_sort: { - sort: formatSortForBucketSort(sort), - from: (page - 1) * perPage, - size: perPage, - gap_policy: 'insert_zeros' as estypes.AggregationsGapPolicy, + // Get total number of executions + executionUuidCardinality: { + cardinality: { + field: EXECUTION_UUID_FIELD, }, }, - // Get counts for types of alerts and whether there was an execution timeout - alertCounts: { - filters: { - filters: { - newAlerts: { match: { [ACTION_FIELD]: 'new-instance' } }, - activeAlerts: { match: { [ACTION_FIELD]: 'active-instance' } }, - recoveredAlerts: { match: { [ACTION_FIELD]: 'recovered-instance' } }, - }, + executionUuid: { + // Bucket by execution UUID + terms: { + field: EXECUTION_UUID_FIELD, + size: DEFAULT_MAX_BUCKETS_LIMIT, + order: formatSortForTermSort(sort), }, - }, - // Filter by action execute doc and get information from this event - actionExecution: { - filter: getProviderAndActionFilter('actions', 'execute'), aggs: { - actionOutcomes: { - terms: { - field: OUTCOME_FIELD, - size: 2, - }, - }, - }, - }, - // Filter by rule execute doc and get information from this event - ruleExecution: { - filter: getProviderAndActionFilter('alerting', 'execute'), - aggs: { - executeStartTime: { - min: { - field: START_FIELD, - }, - }, - scheduleDelay: { - max: { - field: SCHEDULE_DELAY_FIELD, - }, - }, - totalSearchDuration: { - max: { - field: TOTAL_SEARCH_DURATION_FIELD, - }, - }, - esSearchDuration: { - max: { - field: ES_SEARCH_DURATION_FIELD, - }, - }, - numTriggeredActions: { - max: { - field: NUMBER_OF_TRIGGERED_ACTIONS_FIELD, + // Bucket sort to allow paging through executions + executionUuidSorted: { + bucket_sort: { + sort: formatSortForBucketSort(sort), + from: (page - 1) * perPage, + size: perPage, + gap_policy: 'insert_zeros' as estypes.AggregationsGapPolicy, }, }, - numScheduledActions: { - max: { - field: NUMBER_OF_SCHEDULED_ACTIONS_FIELD, + // Get counts for types of alerts and whether there was an execution timeout + alertCounts: { + filters: { + filters: { + newAlerts: { match: { [ACTION_FIELD]: 'new-instance' } }, + activeAlerts: { match: { [ACTION_FIELD]: 'active-instance' } }, + recoveredAlerts: { match: { [ACTION_FIELD]: 'recovered-instance' } }, + }, }, }, - executionDuration: { - max: { - field: DURATION_FIELD, + // Filter by action execute doc and get information from this event + actionExecution: { + filter: getProviderAndActionFilter('actions', 'execute'), + aggs: { + actionOutcomes: { + terms: { + field: OUTCOME_FIELD, + size: 2, + }, + }, }, }, - outcomeAndMessage: { - top_hits: { - size: 1, - _source: { - includes: [OUTCOME_FIELD, MESSAGE_FIELD, ERROR_MESSAGE_FIELD], + // Filter by rule execute doc and get information from this event + ruleExecution: { + filter: getProviderAndActionFilter('alerting', 'execute'), + aggs: { + executeStartTime: { + min: { + field: START_FIELD, + }, + }, + scheduleDelay: { + max: { + field: SCHEDULE_DELAY_FIELD, + }, + }, + totalSearchDuration: { + max: { + field: TOTAL_SEARCH_DURATION_FIELD, + }, + }, + esSearchDuration: { + max: { + field: ES_SEARCH_DURATION_FIELD, + }, + }, + numTriggeredActions: { + max: { + field: NUMBER_OF_TRIGGERED_ACTIONS_FIELD, + }, + }, + numScheduledActions: { + max: { + field: NUMBER_OF_SCHEDULED_ACTIONS_FIELD, + }, + }, + executionDuration: { + max: { + field: DURATION_FIELD, + }, + }, + outcomeAndMessage: { + top_hits: { + size: 1, + _source: { + includes: [OUTCOME_FIELD, MESSAGE_FIELD, ERROR_MESSAGE_FIELD], + }, + }, }, }, }, + // If there was a timeout, this filter will return non-zero doc count + timeoutMessage: { + filter: getProviderAndActionFilter('alerting', 'execute-timeout'), + }, }, }, - // If there was a timeout, this filter will return non-zero doc count - timeoutMessage: { - filter: getProviderAndActionFilter('alerting', 'execute-timeout'), - }, }, }, }; @@ -280,13 +300,14 @@ export function formatExecutionLogResult( ): IExecutionLogResult { const { aggregations } = results; - if (!aggregations) { + if (!aggregations || !aggregations.excludeExecuteStart) { return EMPTY_EXECUTION_LOG_RESULT; } - const total = (aggregations.executionUuidCardinality as estypes.AggregationsCardinalityAggregate) - .value; - const buckets = (aggregations.executionUuid as ExecutionUuidAggResult).buckets; + const aggs = aggregations.excludeExecuteStart as ExcludeExecuteStartAggResult; + + const total = aggs.executionUuidCardinality.value; + const buckets = aggs.executionUuid.buckets; return { total, diff --git a/x-pack/plugins/alerting/server/rules_client/tests/get_execution_log.test.ts b/x-pack/plugins/alerting/server/rules_client/tests/get_execution_log.test.ts index 8a16bcb2d2fd7..df5f9252a98a0 100644 --- a/x-pack/plugins/alerting/server/rules_client/tests/get_execution_log.test.ts +++ b/x-pack/plugins/alerting/server/rules_client/tests/get_execution_log.test.ts @@ -96,185 +96,189 @@ const BaseRuleSavedObject: SavedObject = { const aggregateResults = { aggregations: { - executionUuid: { + excludeExecuteStart: { meta: {}, - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: '6705da7d-2635-499d-a6a8-1aee1ae1eac9', - doc_count: 27, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + doc_count: 875, + executionUuid: { + meta: {}, + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: '6705da7d-2635-499d-a6a8-1aee1ae1eac9', + doc_count: 27, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { + doc_count: 5, + }, + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { + doc_count: 0, + }, }, - newAlerts: { - doc_count: 5, + }, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, }, - recoveredAlerts: { - doc_count: 0, + numScheduledActions: { + value: 5.0, }, - }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ - { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'S4wIZX8B8TGQpG7XQZns', - _score: 1.0, - _source: { - event: { - outcome: 'success', + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'S4wIZX8B8TGQpG7XQZns', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", }, + ], + }, + }, + scheduleDelay: { + value: 3.126e9, + }, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.056e9, + }, + executeStartTime: { + value: 1.646667512617e12, + value_as_string: '2022-03-07T15:38:32.617Z', + }, + }, + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.126e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.056e9, - }, - executeStartTime: { - value: 1.646667512617e12, - value_as_string: '2022-03-07T15:38:32.617Z', - }, }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', + { + key: '41b2755e-765a-4044-9745-b03875d5e79a', + doc_count: 32, + timeoutMessage: { + meta: {}, + doc_count: 0, + }, + alertCounts: { + meta: {}, + buckets: { + activeAlerts: { + doc_count: 5, + }, + newAlerts: { + doc_count: 5, + }, + recoveredAlerts: { doc_count: 5, }, - ], + }, }, - }, - }, - { - key: '41b2755e-765a-4044-9745-b03875d5e79a', - doc_count: 32, - timeoutMessage: { - meta: {}, - doc_count: 0, - }, - alertCounts: { - meta: {}, - buckets: { - activeAlerts: { - doc_count: 5, + ruleExecution: { + meta: {}, + doc_count: 1, + numTriggeredActions: { + value: 5.0, + }, + numScheduledActions: { + value: 5.0, + }, + outcomeAndMessage: { + hits: { + total: { + value: 1, + relation: 'eq', + }, + max_score: 1.0, + hits: [ + { + _index: '.kibana-event-log-8.2.0-000001', + _id: 'a4wIZX8B8TGQpG7Xwpnz', + _score: 1.0, + _source: { + event: { + outcome: 'success', + }, + message: + "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", + }, + }, + ], + }, }, - newAlerts: { - doc_count: 5, + scheduleDelay: { + value: 3.345e9, }, - recoveredAlerts: { - doc_count: 5, + totalSearchDuration: { + value: 0.0, + }, + esSearchDuration: { + value: 0.0, + }, + executionDuration: { + value: 1.165e9, + }, + executeStartTime: { + value: 1.646667545604e12, + value_as_string: '2022-03-07T15:39:05.604Z', }, }, - }, - ruleExecution: { - meta: {}, - doc_count: 1, - numTriggeredActions: { - value: 5.0, - }, - numScheduledActions: { - value: 5.0, - }, - outcomeAndMessage: { - hits: { - total: { - value: 1, - relation: 'eq', - }, - max_score: 1.0, - hits: [ + actionExecution: { + meta: {}, + doc_count: 5, + actionOutcomes: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ { - _index: '.kibana-event-log-8.2.0-000001', - _id: 'a4wIZX8B8TGQpG7Xwpnz', - _score: 1.0, - _source: { - event: { - outcome: 'success', - }, - message: - "rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'", - }, + key: 'success', + doc_count: 5, }, ], }, }, - scheduleDelay: { - value: 3.345e9, - }, - totalSearchDuration: { - value: 0.0, - }, - esSearchDuration: { - value: 0.0, - }, - executionDuration: { - value: 1.165e9, - }, - executeStartTime: { - value: 1.646667545604e12, - value_as_string: '2022-03-07T15:39:05.604Z', - }, - }, - actionExecution: { - meta: {}, - doc_count: 5, - actionOutcomes: { - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, - buckets: [ - { - key: 'success', - doc_count: 5, - }, - ], - }, }, - }, - ], - }, - executionUuidCardinality: { - value: 374, + ], + }, + executionUuidCardinality: { + value: 374, + }, }, }, }; diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts index dd37f0cc8b67e..7a4506dc53a31 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/alerts/server/alert_types.ts @@ -675,23 +675,32 @@ export function defineAlertTypes( throw new Error('this alert is intended to fail'); }, }; - const longRunningAlertType: RuleType<{}, {}, {}, {}, {}, 'default'> = { - id: 'test.longRunning', - name: 'Test: Long Running', - actionGroups: [ - { - id: 'default', - name: 'Default', + function getLongRunningRuleType() { + const paramsSchema = schema.object({ + delay: schema.maybe(schema.number({ defaultValue: 5000 })), + }); + type ParamsType = TypeOf; + + const result: RuleType = { + id: 'test.longRunning', + name: 'Test: Long Running', + actionGroups: [ + { + id: 'default', + name: 'Default', + }, + ], + producer: 'alertsFixture', + defaultActionGroupId: 'default', + minimumLicenseRequired: 'basic', + isExportable: true, + async executor(ruleExecutorOptions) { + const { params } = ruleExecutorOptions; + await new Promise((resolve) => setTimeout(resolve, params.delay ?? 5000)); }, - ], - producer: 'alertsFixture', - defaultActionGroupId: 'default', - minimumLicenseRequired: 'basic', - isExportable: true, - async executor() { - await new Promise((resolve) => setTimeout(resolve, 5000)); - }, - }; + }; + return result; + } const exampleAlwaysFiringAlertType: RuleType<{}, {}, {}, {}, {}, 'small' | 'medium' | 'large'> = { id: 'example.always-firing', name: 'Always firing', @@ -769,7 +778,7 @@ export function defineAlertTypes( alerting.registerType(onlyStateVariablesAlertType); alerting.registerType(getPatternFiringAlertType()); alerting.registerType(throwAlertType); - alerting.registerType(longRunningAlertType); + alerting.registerType(getLongRunningRuleType()); alerting.registerType(goldNoopAlertType); alerting.registerType(exampleAlwaysFiringAlertType); alerting.registerType(multipleSearchesRuleType); diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts index 17e2a4c395989..7b4463996edf3 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/get_execution_log.ts @@ -126,6 +126,32 @@ export default function createGetExecutionLogTests({ getService }: FtrProviderCo expect(response.body.errors).to.eql([]); }); + it('gets execution log for rule that is currently running', async () => { + const { body: createdRule } = await supertest + .post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`) + .set('kbn-xsrf', 'foo') + .send(getTestRuleData({ rule_type_id: 'test.longRunning', params: { delay: 120000 } })) + .expect(200); + objectRemover.add(Spaces.space1.id, createdRule.id, 'rule', 'alerting'); + + // wait for execute-start event that signals rule has started running + await waitForEvents(createdRule.id, 'alerting', new Map([['execute-start', { gte: 1 }]])); + + const response = await supertest.get( + `${getUrlPrefix(Spaces.space1.id)}/internal/alerting/rule/${ + createdRule.id + }/_execution_log?date_start=${dateStart}` + ); + + expect(response.status).to.eql(200); + + // since these events should have been excluded from the agg, should return empty + expect(response.body.total).to.eql(0); + expect(response.body.data).to.eql([]); + expect(response.body.totalErrors).to.eql(0); + expect(response.body.errors).to.eql([]); + }); + it('gets execution log for rule that performs ES searches', async () => { const { body: createdRule } = await supertest .post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)