Skip to content

Commit

Permalink
Fixing tests and global event log
Browse files Browse the repository at this point in the history
  • Loading branch information
doakalexi committed Oct 5, 2022
1 parent 42534af commit 502eaa7
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 140 deletions.
216 changes: 94 additions & 122 deletions x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ExecutionUuidAggResult,
getExecutionKPIAggregation,
formatExecutionKPIResult,
getExecutionLogRuntimeMappings,
} from './get_execution_log_aggregation';

describe('formatSortForBucketSort', () => {
Expand Down Expand Up @@ -273,16 +274,15 @@ describe('getExecutionLogAggregation', () => {
outcomeAndMessage: {
top_hits: {
size: 1,
_source: {
includes: [
'kibana.alerting.outcome',
'message',
'error.message',
'kibana.version',
'rule.id',
'rule.name',
],
},
_source: false,
fields: [
'kibana.alerting.outcome',
'message',
'error.message',
'kibana.version',
'rule.id',
'rule.name',
],
},
},
},
Expand Down Expand Up @@ -478,16 +478,15 @@ describe('getExecutionLogAggregation', () => {
outcomeAndMessage: {
top_hits: {
size: 1,
_source: {
includes: [
'kibana.alerting.outcome',
'message',
'error.message',
'kibana.version',
'rule.id',
'rule.name',
],
},
_source: false,
fields: [
'kibana.alerting.outcome',
'message',
'error.message',
'kibana.version',
'rule.id',
'rule.name',
],
},
},
},
Expand Down Expand Up @@ -683,16 +682,15 @@ describe('getExecutionLogAggregation', () => {
outcomeAndMessage: {
top_hits: {
size: 1,
_source: {
includes: [
'kibana.alerting.outcome',
'message',
'error.message',
'kibana.version',
'rule.id',
'rule.name',
],
},
_source: false,
fields: [
'kibana.alerting.outcome',
'message',
'error.message',
'kibana.version',
'rule.id',
'rule.name',
],
},
},
},
Expand Down Expand Up @@ -780,17 +778,11 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: 'S4wIZX8B8TGQpG7XQZns',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },
event: {
outcome: 'success',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'success',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'success',
message:
"rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
},
Expand Down Expand Up @@ -867,18 +859,11 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: 'a4wIZX8B8TGQpG7Xwpnz',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },

event: {
outcome: 'success',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'success',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'success',
message:
"rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
},
Expand Down Expand Up @@ -1027,22 +1012,14 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: 'S4wIZX8B8TGQpG7XQZns',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },
event: {
outcome: 'failure',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'failure',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'failure',
message:
"rule execution failure: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
error: {
message: 'I am erroring in rule execution!!',
},
'error.message': 'I am erroring in rule execution!!',
},
},
],
Expand Down Expand Up @@ -1117,17 +1094,11 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: 'a4wIZX8B8TGQpG7Xwpnz',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },
event: {
outcome: 'success',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'success',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'success',
message:
"rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
},
Expand Down Expand Up @@ -1276,17 +1247,11 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: 'dJkWa38B1ylB1EvsAckB',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },
event: {
outcome: 'success',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'success',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'success',
message:
"rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
},
Expand Down Expand Up @@ -1358,17 +1323,11 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: 'a4wIZX8B8TGQpG7Xwpnz',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },
event: {
outcome: 'success',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'success',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'success',
message:
"rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
},
Expand Down Expand Up @@ -1517,17 +1476,11 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: '7xKcb38BcntAq5ycFwiu',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },
event: {
outcome: 'success',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'success',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'success',
message:
"rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
},
Expand Down Expand Up @@ -1604,17 +1557,11 @@ describe('formatExecutionLogResult', () => {
_index: '.kibana-event-log-8.2.0-000001',
_id: 'zRKbb38BcntAq5ycOwgk',
_score: 1.0,
_source: {
rule: { id: 'a348a740-9e2c-11ec-bd64-774ed95c43ef', name: 'rule_name' },
event: {
outcome: 'success',
},
kibana: {
version: '8.2.0',
alerting: {
outcome: 'success',
},
},
fields: {
'rule.id': 'a348a740-9e2c-11ec-bd64-774ed95c43ef',
'rule.name': 'rule_name',
'kibana.version': '8.2.0',
'kibana.alerting.outcome': 'success',
message:
"rule executed: example.always-firing:a348a740-9e2c-11ec-bd64-774ed95c43ef: 'test rule'",
},
Expand Down Expand Up @@ -2298,3 +2245,28 @@ describe('formatExecutionKPIAggBuckets', () => {
});
});
});

describe('getExecutionLogRuntimeMappings', () => {
test('should correctly generate runtime field mappings', () => {
expect(getExecutionLogRuntimeMappings()).toMatchInlineSnapshot(`
Object {
"kibana.alerting.outcome": Object {
"script": Object {
"source": "
def kibana = params._source['kibana'];
if (kibana.alerting == null || (kibana.alerting != null && kibana.alerting.outcome == null)) {
def event = params._source['event'];
if (event.outcome != null)
emit(event.outcome);
} else {
if (kibana.alerting != null && params._source['kibana'].alerting.outcome != null) {
emit(kibana.alerting.outcome)
}
}",
},
"type": "keyword",
},
}
`);
});
});
28 changes: 15 additions & 13 deletions x-pack/plugins/alerting/server/lib/get_execution_log_aggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { KueryNode } from '@kbn/core-saved-objects-api-server';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import Boom from '@hapi/boom';
import { flatMap, get } from 'lodash';
import { flatMap, get, head } from 'lodash';
import { AggregateEventsBySavedObjectResult } from '@kbn/event-log-plugin/server';
import { fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query';
import { parseDuration } from '.';
Expand Down Expand Up @@ -428,12 +428,14 @@ export function getExecutionLogRuntimeMappings(): estypes.MappingRuntimeFields {
type: 'keyword',
script: {
source: `
if (params._source['kibana'].alerting != null && params._source['kibana'].alerting.outcome == null) {
if (params._source['event'].outcome != null)
emit(params._source['event'].outcome);
def kibana = params._source['kibana'];
if (kibana.alerting == null || (kibana.alerting != null && kibana.alerting.outcome == null)) {
def event = params._source['event'];
if (event.outcome != null)
emit(event.outcome);
} else {
if (params._source['kibana'].alerting != null && params._source['kibana'].alerting.outcome != null) {
emit(params._source['kibana'].alerting.outcome)
if (kibana.alerting != null && params._source['kibana'].alerting.outcome != null) {
emit(kibana.alerting.outcome)
}
}`,
},
Expand Down Expand Up @@ -485,15 +487,15 @@ function formatExecutionLogAggBucket(bucket: IExecutionUuidAggBucket): IExecutio
actionExecutionOutcomes.find((subBucket) => subBucket?.key === 'failure')?.doc_count ?? 0;

const outcomeAndMessage = bucket?.ruleExecution?.outcomeAndMessage?.hits?.hits[0]?.fields ?? {};
const status = outcomeAndMessage['kibana.alerting.outcome'] ?? '';
const status: string = head(outcomeAndMessage['kibana.alerting.outcome']) ?? '';
const outcomeMessage: string = head(outcomeAndMessage.message) ?? '';
const outcomeErrorMessage: string = head(outcomeAndMessage['error.message']) ?? '';
const message =
status === 'failure'
? `${outcomeAndMessage.message ?? ''} - ${outcomeAndMessage['error.message'] ?? ''}`
: outcomeAndMessage.message ?? '';
const version = outcomeAndMessage['kibana.version'] ?? '';
status === 'failure' ? `${outcomeMessage} - ${outcomeErrorMessage}` : outcomeMessage;
const version: string = head(outcomeAndMessage['kibana.version']) ?? '';

const ruleId = outcomeAndMessage['rule.id'] ?? '';
const ruleName = outcomeAndMessage['rule.name'] ?? '';
const ruleId: string = head(outcomeAndMessage['rule.id']) ?? '';
const ruleName: string = head(outcomeAndMessage['rule.name']) ?? '';
return {
id: bucket?.key ?? '',
timestamp: bucket?.ruleExecution?.executeStartTime.value_as_string ?? '',
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ export class RulesClient {
perPage,
sort,
}),
}
},
getExecutionLogRuntimeMappings()
);

return formatExecutionLogResult(aggResult);
Expand Down Expand Up @@ -1113,7 +1114,8 @@ export class RulesClient {
start: parsedDateStart.toISOString(),
end: parsedDateEnd.toISOString(),
aggs: getExecutionKPIAggregation(filter),
}
},
getExecutionLogRuntimeMappings()
);

return formatExecutionKPIResult(aggResult);
Expand Down
Loading

0 comments on commit 502eaa7

Please sign in to comment.