[Rule Registry] Switch to _source for updating documents instead of Fields API (#118245) (#121026)

* [Rule Registry] Switch to _source for updating documents instead of Fields API

* updating test with _source instead of fields

* removing mapValues dep

* Refactor types and clean up names

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Chris Cowan 2021-12-13 10:20:20 -07:00 committed by GitHub
parent f3142517ed
commit dab6bc442d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 29 deletions

View file

@ -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;
@ -24,9 +24,7 @@ export interface IRuleDataClient {
export interface IRuleDataReader {
search<TSearchRequest extends ESSearchRequest>(
request: TSearchRequest
): Promise<
ESSearchResponse<Partial<Record<TechnicalRuleDataFieldName, unknown[]>>, TSearchRequest>
>;
): Promise<ESSearchResponse<Partial<ParsedTechnicalFields>, TSearchRequest>>;
getDynamicIndexPattern(target?: string): Promise<{
title: string;

View file

@ -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',

View file

@ -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,
@ -216,8 +216,6 @@ export const createLifecycleExecutor =
collapse: {
field: ALERT_UUID,
},
_source: false,
fields: [{ field: '*', include_unmapped: true }],
sort: {
[TIMESTAMP]: 'desc' as const,
},
@ -226,13 +224,13 @@ export const createLifecycleExecutor =
});
hits.hits.forEach((hit) => {
const fields = parseTechnicalFields(hit.fields);
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,
};
}
});
}

View file

@ -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';
@ -272,13 +272,10 @@ describe('createLifecycleRuleTypeFactory', () => {
(doc: any) => !('index' in doc) && doc['service.name'] === 'opbeans-node'
) as Record<string, any>;
const stored = mapValues(lastOpbeansNodeDoc, (val) => {
return castArray(val);
});
// @ts-ignore 4.3.5 upgrade
helpers.ruleDataClientMock.getReader().search.mockResolvedValueOnce({
hits: {
hits: [{ fields: stored } as any],
hits: [{ _source: lastOpbeansNodeDoc } as any],
total: {
value: 1,
relation: 'eq',
@ -356,13 +353,9 @@ describe('createLifecycleRuleTypeFactory', () => {
(doc: any) => !('index' in doc) && doc['service.name'] === 'opbeans-node'
) as Record<string, any>;
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',