From 24aaaba5a813bc2ab7e394cbfdae40ca20b4269c Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Wed, 10 Nov 2021 13:45:08 -0700 Subject: [PATCH 1/4] [Rule Registry] Switch to _source for updating documents instead of Fields API --- .../server/utils/create_lifecycle_executor.test.ts | 8 ++++---- .../server/utils/create_lifecycle_executor.ts | 4 +--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.test.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.test.ts index c65fdece6c5f0..6be945dfe02da 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.test.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.test.ts @@ -126,7 +126,7 @@ describe('createLifecycleExecutor', () => { hits: { hits: [ { - fields: { + _source: { '@timestamp': '', [ALERT_INSTANCE_ID]: 'TEST_ALERT_0', [ALERT_UUID]: 'ALERT_0_UUID', @@ -143,7 +143,7 @@ describe('createLifecycleExecutor', () => { }, }, { - fields: { + _source: { '@timestamp': '', [ALERT_INSTANCE_ID]: 'TEST_ALERT_1', [ALERT_UUID]: 'ALERT_1_UUID', @@ -246,7 +246,7 @@ describe('createLifecycleExecutor', () => { hits: { hits: [ { - fields: { + _source: { '@timestamp': '', [ALERT_INSTANCE_ID]: 'TEST_ALERT_0', [ALERT_UUID]: 'ALERT_0_UUID', @@ -262,7 +262,7 @@ describe('createLifecycleExecutor', () => { }, }, { - fields: { + _source: { '@timestamp': '', [ALERT_INSTANCE_ID]: 'TEST_ALERT_1', [ALERT_UUID]: 'ALERT_1_UUID', diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts index 1acbc0c3f43bd..6fb0717f32dc0 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts @@ -216,8 +216,6 @@ export const createLifecycleExecutor = collapse: { field: ALERT_UUID, }, - _source: false, - fields: [{ field: '*', include_unmapped: true }], sort: { [TIMESTAMP]: 'desc' as const, }, @@ -226,7 +224,7 @@ export const createLifecycleExecutor = }); hits.hits.forEach((hit) => { - const fields = parseTechnicalFields(hit.fields); + const fields = parseTechnicalFields(hit._source); const indexName = hit._index; const alertId = fields[ALERT_INSTANCE_ID]; trackedAlertsDataMap[alertId] = { From c56697676d4141d20f365bf46aba611fb884a3ed Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Wed, 10 Nov 2021 15:46:12 -0700 Subject: [PATCH 2/4] updating test with _source instead of fields --- .../server/utils/create_lifecycle_rule_type.test.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts index 3fa567b8aca96..5c42b2220ed16 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts @@ -272,13 +272,9 @@ describe('createLifecycleRuleTypeFactory', () => { (doc: any) => !('index' in doc) && doc['service.name'] === 'opbeans-node' ) as Record; - const stored = mapValues(lastOpbeansNodeDoc, (val) => { - return castArray(val); - }); - helpers.ruleDataClientMock.getReader().search.mockResolvedValueOnce({ hits: { - hits: [{ fields: stored } as any], + hits: [{ _source: lastOpbeansNodeDoc } as any], total: { value: 1, relation: 'eq', @@ -356,13 +352,9 @@ describe('createLifecycleRuleTypeFactory', () => { (doc: any) => !('index' in doc) && doc['service.name'] === 'opbeans-node' ) as Record; - const stored = mapValues(lastOpbeansNodeDoc, (val) => { - return castArray(val); - }); - helpers.ruleDataClientMock.getReader().search.mockResolvedValueOnce({ hits: { - hits: [{ fields: stored } as any], + hits: [{ _source: lastOpbeansNodeDoc } as any], total: { value: 1, relation: 'eq', From 96ab9c33b5b84b0f07ace7df1934a35404c03b44 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Thu, 11 Nov 2021 08:32:19 -0700 Subject: [PATCH 3/4] removing mapValues dep --- .../server/utils/create_lifecycle_rule_type.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts index 5c42b2220ed16..8d64da9db3704 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_rule_type.test.ts @@ -14,7 +14,7 @@ import { ALERT_UUID, } from '@kbn/rule-data-utils'; import { loggerMock } from '@kbn/logging/mocks'; -import { castArray, omit, mapValues } from 'lodash'; +import { castArray, omit } from 'lodash'; import { RuleDataClient } from '../rule_data_client'; import { createRuleDataClientMock } from '../rule_data_client/rule_data_client.mock'; import { createLifecycleRuleTypeFactory } from './create_lifecycle_rule_type_factory'; From c705ad10dbe4a793f08f7cca5cf9ed95cd09f02b Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Thu, 9 Dec 2021 13:05:53 -0700 Subject: [PATCH 4/4] Refactor types and clean up names --- .../server/rule_data_client/types.ts | 6 ++---- .../server/utils/create_lifecycle_executor.ts | 16 ++++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/rule_registry/server/rule_data_client/types.ts b/x-pack/plugins/rule_registry/server/rule_data_client/types.ts index 5ddbd0035526d..5a1df05bf5cac 100644 --- a/x-pack/plugins/rule_registry/server/rule_data_client/types.ts +++ b/x-pack/plugins/rule_registry/server/rule_data_client/types.ts @@ -10,7 +10,7 @@ import { BulkRequest, BulkResponse } from '@elastic/elasticsearch/lib/api/typesW import { ESSearchRequest, ESSearchResponse } from 'src/core/types/elasticsearch'; import { FieldDescriptor } from 'src/plugins/data/server'; -import { TechnicalRuleDataFieldName } from '../../common/technical_rule_data_field_names'; +import { ParsedTechnicalFields } from '../../common/parse_technical_fields'; export interface IRuleDataClient { indexName: string; @@ -23,9 +23,7 @@ export interface IRuleDataClient { export interface IRuleDataReader { search( request: TSearchRequest - ): Promise< - ESSearchResponse>, TSearchRequest> - >; + ): Promise, TSearchRequest>>; getDynamicIndexPattern(target?: string): Promise<{ title: string; diff --git a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts index b6d94f347e06e..0ca0002470af0 100644 --- a/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts +++ b/x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts @@ -18,7 +18,7 @@ import { AlertTypeParams, AlertTypeState, } from '../../../alerting/server'; -import { ParsedTechnicalFields, parseTechnicalFields } from '../../common/parse_technical_fields'; +import { ParsedTechnicalFields } from '../../common/parse_technical_fields'; import { ALERT_DURATION, ALERT_END, @@ -224,13 +224,13 @@ export const createLifecycleExecutor = }); hits.hits.forEach((hit) => { - const fields = parseTechnicalFields(hit._source); - const indexName = hit._index; - const alertId = fields[ALERT_INSTANCE_ID]; - trackedAlertsDataMap[alertId] = { - indexName, - fields, - }; + const alertId = hit._source[ALERT_INSTANCE_ID]; + if (alertId) { + trackedAlertsDataMap[alertId] = { + indexName: hit._index, + fields: hit._source, + }; + } }); }