Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alerting] Allow rule to execute if the value is 0 and that mets the condition #105626

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
* 2.0.
*/

import uuid from 'uuid';
import type { Writable } from '@kbn/utility-types';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
import { getAlertType } from './alert_type';
import { AlertServices } from '../../../../alerting/server';
import { getAlertType, ActionGroupId } from './alert_type';
import { ActionContext } from './action_context';
import { Params } from './alert_type_params';
import { AlertServicesMock, alertsMock } from '../../../../alerting/server/mocks';

describe('alertType', () => {
const logger = loggingSystemMock.create().get();
const data = {
timeSeriesQuery: jest.fn(),
};
const alertServices: AlertServicesMock = alertsMock.createAlertServices();

const alertType = getAlertType(logger, Promise.resolve(data));

afterEach(() => {
data.timeSeriesQuery.mockReset();
});

it('alert type creation structure is the expected value', async () => {
expect(alertType.id).toBe('.index-threshold');
expect(alertType.name).toBe('Index threshold');
Expand Down Expand Up @@ -135,4 +144,68 @@ describe('alertType', () => {
`"[aggType]: invalid aggType: \\"foo\\""`
);
});

it('should ensure 0 results fires actions if it passes the comparator check', async () => {
data.timeSeriesQuery.mockImplementation((...args) => {
return {
results: [
{
group: 'all documents',
metrics: [['2021-07-14T14:49:30.978Z', 0]],
},
],
};
});
const params: Params = {
index: 'index-name',
timeField: 'time-field',
aggType: 'foo',
groupBy: 'all',
timeWindowSize: 5,
timeWindowUnit: 'm',
thresholdComparator: '<',
threshold: [1],
};

await alertType.executor({
alertId: uuid.v4(),
startedAt: new Date(),
previousStartedAt: new Date(),
services: (alertServices as unknown) as AlertServices<
{},
ActionContext,
typeof ActionGroupId
>,
params,
state: {
latestTimestamp: undefined,
},
spaceId: uuid.v4(),
name: uuid.v4(),
tags: [],
createdBy: null,
updatedBy: null,
rule: {
name: uuid.v4(),
tags: [],
consumer: '',
producer: '',
ruleTypeId: '',
ruleTypeName: '',
enabled: true,
schedule: {
interval: '1h',
},
actions: [],
createdBy: null,
updatedBy: null,
createdAt: new Date(),
updatedAt: new Date(),
throttle: null,
notifyWhen: null,
},
});

expect(alertServices.alertInstanceFactory).toHaveBeenCalledWith('all documents');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { ComparatorFns, getHumanReadableComparator } from '../lib';

export const ID = '.index-threshold';
const ActionGroupId = 'threshold met';
export const ActionGroupId = 'threshold met';

export function getAlertType(
logger: Logger,
Expand Down Expand Up @@ -180,17 +180,15 @@ export function getAlertType(
groupResult.metrics && groupResult.metrics.length > 0 ? groupResult.metrics[0] : null;
const value = metric && metric.length === 2 ? metric[1] : null;

if (!value) {
const met = compareFn(value ?? 0, params.threshold);
chrisronline marked this conversation as resolved.
Show resolved Hide resolved
if (!value && !met) {
logger.debug(
`alert ${ID}:${alertId} "${name}": no metrics found for group ${instanceId}} from groupResult ${JSON.stringify(
groupResult
)}`
);
continue;
}

const met = compareFn(value, params.threshold);

if (!met) continue;

const agg = params.aggField ? `${params.aggType}(${params.aggField})` : `${params.aggType}`;
Expand All @@ -201,7 +199,7 @@ export function getAlertType(
const baseContext: BaseActionContext = {
date,
group: instanceId,
value,
value: value ?? 0,
conditions: humanFn,
};
const actionContext = addMessages(options, baseContext, params);
Expand Down