Skip to content

Commit

Permalink
[Discover] omit null searchType from esQuery completely, add isEsQuer…
Browse files Browse the repository at this point in the history
…yAlert check for useSavedObjectReferences hook
  • Loading branch information
dimaanj committed May 19, 2022
1 parent 17687d3 commit 84820c5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ describe('alert_form', () => {

it('renders alert name', async () => {
const alertNameField = wrapper.find('[data-test-subj="ruleNameInput"]');
expect(alertNameField.first().prop('value')).toBe('test');
expect(alertNameField.exists()).toBeTruthy();
expect(alertNameField.first().prop('value')).toBe('test');
});

it('renders registered selected alert type', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { STACK_ALERTS_FEATURE_ID } from '../../../common';
import { ExecutorOptions } from './types';
import { ActionGroupId, ES_QUERY_ID } from './constants';
import { executor } from './executor';
import { isEsQueryAlert } from './util';

export function getAlertType(
logger: Logger,
Expand Down Expand Up @@ -164,14 +165,21 @@ export function getAlertType(
useSavedObjectReferences: {
extractReferences: (params) => {
const [searchConfiguration, references] = extractReferences(params.searchConfiguration);

if (isEsQueryAlert(params.searchType)) {
return { params: params as EsQueryAlertParamsExtractedParams, references };
}
const newParams = { ...params, searchConfiguration } as EsQueryAlertParamsExtractedParams;
return { params: newParams, references };
},
injectReferences: (params, references) => {
if (isEsQueryAlert(params.searchType)) {
return params;
}
return {
...params,
searchConfiguration: injectReferences(params.searchConfiguration, references),
} as EsQueryAlertParams;
};
},
},
minimumLicenseRequired: 'basic',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ const EsQueryAlertParamsSchemaProperties = {
timeWindowUnit: schema.string({ validate: validateTimeWindowUnits }),
threshold: schema.arrayOf(schema.number(), { minSize: 1, maxSize: 2 }),
thresholdComparator: getComparatorSchemaType(validateComparator),
searchType: schema.nullable(schema.literal('searchSource')),
searchType: schema.maybe(schema.literal('searchSource')),
// searchSource alert param only
searchConfiguration: schema.conditional(
schema.siblingRef('searchType'),
schema.literal('searchSource'),
schema.object({}, { unknowns: 'allow' }),
schema.never()
schema.never(),
schema.never(),
schema.object({}, { unknowns: 'allow' })
),
// esQuery alert params only
esQuery: schema.conditional(
schema.siblingRef('searchType'),
schema.literal('searchSource'),
schema.never(),
schema.string({ minLength: 1 })
schema.string({ minLength: 1 }),
schema.never()
),
index: schema.conditional(
schema.siblingRef('searchType'),
schema.literal('searchSource'),
schema.never(),
schema.arrayOf(schema.string({ minLength: 1 }), { minSize: 1 })
schema.arrayOf(schema.string({ minLength: 1 }), { minSize: 1 }),
schema.never()
),
timeField: schema.conditional(
schema.siblingRef('searchType'),
schema.literal('searchSource'),
schema.never(),
schema.string({ minLength: 1 })
schema.string({ minLength: 1 }),
schema.never()
),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import { fetchEsQuery } from './lib/fetch_es_query';
import { EsQueryAlertParams } from './alert_type_params';
import { fetchSearchSourceQuery } from './lib/fetch_search_source_query';
import { Comparator } from '../../../common/comparator_types';
import { isEsQueryAlert } from './util';

export async function executor(
logger: Logger,
core: CoreSetup,
options: ExecutorOptions<EsQueryAlertParams>
) {
const esQueryAlert = isEsQueryAlert(options);
const esQueryAlert = isEsQueryAlert(options.params.searchType);
const { alertId, name, services, params, state } = options;
const { alertFactory, scopedClusterClient, searchSourceClient } = services;
const currentTimestamp = new Date().toISOString();
Expand Down Expand Up @@ -165,10 +166,6 @@ export function tryToParseAsDate(sortValue?: string | number | null): undefined
}
}

export function isEsQueryAlert(options: ExecutorOptions<EsQueryAlertParams>) {
return options.params.searchType !== 'searchSource';
}

export function getChecksum(params: EsQueryAlertParams) {
return sha256.create().update(JSON.stringify(params));
}
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/stack_alerts/server/alert_types/es_query/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { EsQueryAlertParams } from './alert_type_params';

export function isEsQueryAlert(searchType: EsQueryAlertParams['searchType']) {
return searchType !== 'searchSource';
}

0 comments on commit 84820c5

Please sign in to comment.