Skip to content

Commit

Permalink
[Custom threshold] Add missing prefill fields to the custom threshold…
Browse files Browse the repository at this point in the history
… rule (elastic#174690)

Fixes elastic#174824

## Summary

This PR adds prefill functionality for `time size`, `time unit`, and
`alertOnGroupDisappear` in the custom threshold.
It also fixes the filter query by changing the structure to match how we
save information in the rule saved object, like this:

```
searchConfiguration: {
  index: 'metrics-*',
  query: {
    query: 'host.name: host-1',
    language: 'kuery',
  },
},
```
This will be more compatible with future changes in case we want to
support query filters other than `kuery`.

---------

Co-authored-by: Faisal Kanout <faisal.kanout@elastic.co>
  • Loading branch information
2 people authored and lcawl committed Jan 26, 2024
1 parent acbeefa commit 0840430
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

import React from 'react';
import { RuleTypeParams } from '@kbn/alerting-plugin/common';
import { Query } from '@kbn/data-plugin/common';
import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks';
import { queryClient } from '@kbn/osquery-plugin/public/query_client';
import { mountWithIntl, nextTick } from '@kbn/test-jest-helpers';
Expand All @@ -15,7 +17,7 @@ import { Aggregators, Comparator } from '../../../common/custom_threshold_rule/t
import { useKibana } from '../../utils/kibana_react';
import { kibanaStartMock } from '../../utils/kibana_react.mock';
import Expressions from './custom_threshold_rule_expression';
import { CustomThresholdPrefillOptions } from './types';
import { AlertParams, CustomThresholdPrefillOptions } from './types';

jest.mock('../../utils/kibana_react');
jest.mock('./components/preview_chart/preview_chart', () => ({
Expand All @@ -42,7 +44,7 @@ describe('Expression', () => {
currentOptions?: CustomThresholdPrefillOptions,
customRuleParams?: Record<string, unknown>
) {
const ruleParams = {
const ruleParams: RuleTypeParams & AlertParams = {
criteria: [],
groupBy: undefined,
sourceId: 'default',
Expand Down Expand Up @@ -161,34 +163,49 @@ describe('Expression', () => {
it('should prefill the rule using the context metadata', async () => {
const index = 'changedMockedIndex';
const currentOptions: CustomThresholdPrefillOptions = {
alertOnGroupDisappear: false,
groupBy: ['host.hostname'],
filterQuery: 'foo',
searchConfiguration: { index },
searchConfiguration: {
index,
query: {
query: 'foo',
language: 'kuery',
},
},
criteria: [
{
metrics: [
{ name: 'A', aggType: Aggregators.AVERAGE, field: 'system.load.1' },
{ name: 'B', aggType: Aggregators.CARDINALITY, field: 'system.cpu.user.pct' },
],
comparator: Comparator.LT_OR_EQ,
equation: 'A * B',
label: 'prefill label',
threshold: [500],
timeSize: 7,
timeUnit: 'h',
},
],
};

const { ruleParams } = await setup(currentOptions, { searchConfiguration: undefined });

expect(ruleParams.alertOnGroupDisappear).toEqual(false);
expect(ruleParams.groupBy).toEqual(['host.hostname']);
expect(ruleParams.searchConfiguration.query.query).toBe('foo');
expect((ruleParams.searchConfiguration.query as Query).query).toBe('foo');
expect(ruleParams.searchConfiguration.index).toBe(index);
expect(ruleParams.criteria).toEqual([
{
metrics: [
{ name: 'A', aggType: Aggregators.AVERAGE, field: 'system.load.1' },
{ name: 'B', aggType: Aggregators.CARDINALITY, field: 'system.cpu.user.pct' },
],
comparator: Comparator.GT,
threshold: [100],
timeSize: 1,
timeUnit: 'm',
comparator: Comparator.LT_OR_EQ,
equation: 'A * B',
label: 'prefill label',
threshold: [500],
timeSize: 7,
timeUnit: 'h',
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ export default function Expressions(props: Props) {
if (!ruleParams.searchConfiguration || !ruleParams.searchConfiguration.index) {
if (metadata?.currentOptions?.searchConfiguration) {
initialSearchConfiguration = {
...metadata.currentOptions.searchConfiguration,
query: {
query: ruleParams.searchConfiguration?.query ?? '',
language: 'kuery',
},
...metadata.currentOptions.searchConfiguration,
};
} else {
const newSearchSource = data.search.searchSource.createEmpty();
Expand Down Expand Up @@ -164,10 +164,6 @@ export default function Expressions(props: Props) {
preFillCriteria();
}

if (!ruleParams.filterQuery) {
preFillFilterQuery();
}

if (!ruleParams.groupBy) {
preFillGroupBy();
}
Expand All @@ -176,7 +172,7 @@ export default function Expressions(props: Props) {
setRuleParams('alertOnNoData', true);
}
if (typeof ruleParams.alertOnGroupDisappear === 'undefined') {
setRuleParams('alertOnGroupDisappear', true);
preFillAlertOnGroupDisappear();
}
}, [metadata]); // eslint-disable-line react-hooks/exhaustive-deps

Expand Down Expand Up @@ -277,23 +273,13 @@ export default function Expressions(props: Props) {
[ruleParams.criteria, setRuleParams]
);

const preFillFilterQuery = useCallback(() => {
const md = metadata;

if (md && md.currentOptions?.filterQuery) {
setRuleParams('searchConfiguration', {
...ruleParams.searchConfiguration,
query: {
query: md.currentOptions.filterQuery,
language: 'kuery',
},
});
}
}, [metadata, setRuleParams, ruleParams.searchConfiguration]);

const preFillCriteria = useCallback(() => {
const md = metadata;
if (md?.currentOptions?.criteria?.length) {
const { timeSize: prefillTimeSize, timeUnit: prefillTimeUnit } =
md.currentOptions.criteria[0];
if (prefillTimeSize) setTimeSize(prefillTimeSize);
if (prefillTimeUnit) setTimeUnit(prefillTimeUnit);
setRuleParams(
'criteria',
md.currentOptions.criteria.map((criterion) => ({
Expand All @@ -313,6 +299,15 @@ export default function Expressions(props: Props) {
}
}, [metadata, setRuleParams]);

const preFillAlertOnGroupDisappear = useCallback(() => {
const md = metadata;
if (md && typeof md.currentOptions?.alertOnGroupDisappear !== 'undefined') {
setRuleParams('alertOnGroupDisappear', md.currentOptions.alertOnGroupDisappear);
} else {
setRuleParams('alertOnGroupDisappear', true);
}
}, [metadata, setRuleParams]);

const hasGroupBy = useMemo(
() => ruleParams.groupBy && ruleParams.groupBy.length > 0,
[ruleParams.groupBy]
Expand Down

0 comments on commit 0840430

Please sign in to comment.