Skip to content

Commit 5d17885

Browse files
committed
[Metrics UI] Add jest tests for alert previews
1 parent 6ee2460 commit 5d17885

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import * as mocks from './test_mocks';
8+
import { Comparator, Aggregators, MetricExpressionParams } from './types';
9+
import { alertsMock, AlertServicesMock } from '../../../../../alerts/server/mocks';
10+
import { previewMetricThresholdAlert } from './preview_metric_threshold_alert';
11+
12+
describe('Previewing the metric threshold alert type', () => {
13+
describe('querying the entire infrastructure', () => {
14+
test('returns the expected results using a bucket interval equal to the alert interval', async () => {
15+
const [ungroupedResult] = await previewMetricThresholdAlert({
16+
...baseParams,
17+
lookback: 'h',
18+
alertInterval: '1m',
19+
});
20+
const [firedResults, noDataResults, errorResults] = ungroupedResult;
21+
expect(firedResults).toBe(30);
22+
expect(noDataResults).toBe(0);
23+
expect(errorResults).toBe(0);
24+
});
25+
26+
test('returns the expected results using a bucket interval shorter than the alert interval', async () => {
27+
const [ungroupedResult] = await previewMetricThresholdAlert({
28+
...baseParams,
29+
lookback: 'h',
30+
alertInterval: '3m',
31+
});
32+
const [firedResults, noDataResults, errorResults] = ungroupedResult;
33+
expect(firedResults).toBe(10);
34+
expect(noDataResults).toBe(0);
35+
expect(errorResults).toBe(0);
36+
});
37+
test('returns the expected results using a bucket interval longer than the alert interval', async () => {
38+
const [ungroupedResult] = await previewMetricThresholdAlert({
39+
...baseParams,
40+
lookback: 'h',
41+
alertInterval: '30s',
42+
});
43+
const [firedResults, noDataResults, errorResults] = ungroupedResult;
44+
expect(firedResults).toBe(60);
45+
expect(noDataResults).toBe(0);
46+
expect(errorResults).toBe(0);
47+
});
48+
});
49+
describe('querying with a groupBy parameter', () => {
50+
test('returns the expected results', async () => {
51+
const [resultA, resultB] = await previewMetricThresholdAlert({
52+
...baseParams,
53+
params: {
54+
...baseParams.params,
55+
groupBy: ['something'],
56+
},
57+
lookback: 'h',
58+
alertInterval: '1m',
59+
});
60+
const [firedResultsA, noDataResultsA, errorResultsA] = resultA;
61+
expect(firedResultsA).toBe(30);
62+
expect(noDataResultsA).toBe(0);
63+
expect(errorResultsA).toBe(0);
64+
const [firedResultsB, noDataResultsB, errorResultsB] = resultB;
65+
expect(firedResultsB).toBe(60);
66+
expect(noDataResultsB).toBe(0);
67+
expect(errorResultsB).toBe(0);
68+
});
69+
});
70+
describe('querying a data set with a period of No Data', () => {
71+
test('returns the expected results', async () => {
72+
const [ungroupedResult] = await previewMetricThresholdAlert({
73+
...baseParams,
74+
params: {
75+
...baseParams.params,
76+
criteria: [
77+
{
78+
...baseCriterion,
79+
metric: 'test.metric.2',
80+
} as MetricExpressionParams,
81+
],
82+
},
83+
lookback: 'h',
84+
alertInterval: '1m',
85+
});
86+
const [firedResults, noDataResults, errorResults] = ungroupedResult;
87+
expect(firedResults).toBe(25);
88+
expect(noDataResults).toBe(10);
89+
expect(errorResults).toBe(0);
90+
});
91+
});
92+
});
93+
94+
const services: AlertServicesMock = alertsMock.createAlertServices();
95+
services.callCluster.mockImplementation(async (_: string, { body, index }: any) => {
96+
const metric = body.query.bool.filter[1]?.exists.field;
97+
if (body.aggs.groupings) {
98+
if (body.aggs.groupings.composite.after) {
99+
return mocks.compositeEndResponse;
100+
}
101+
return mocks.basicCompositePreviewResponse;
102+
}
103+
if (metric === 'test.metric.2') {
104+
return mocks.alternateMetricPreviewResponse;
105+
}
106+
return mocks.basicMetricPreviewResponse;
107+
});
108+
109+
const baseCriterion = {
110+
aggType: Aggregators.AVERAGE,
111+
metric: 'test.metric.1',
112+
timeSize: 1,
113+
timeUnit: 'm',
114+
comparator: Comparator.GT,
115+
threshold: [0.75],
116+
} as MetricExpressionParams;
117+
118+
const config = {
119+
metricAlias: 'metricbeat-*',
120+
fields: {
121+
timefield: '@timestamp',
122+
},
123+
} as any;
124+
125+
const baseParams = {
126+
callCluster: services.callCluster,
127+
params: {
128+
criteria: [baseCriterion],
129+
groupBy: undefined,
130+
filterQuery: undefined,
131+
},
132+
config,
133+
};

x-pack/plugins/infra/server/lib/alerting/metric_threshold/test_mocks.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ const bucketsC = [
3838
},
3939
];
4040

41+
const previewBucketsA = Array.from(Array(60), (_, i) => bucketsA[i % 2]); // Repeat bucketsA to a total length of 60
42+
const previewBucketsB = Array.from(Array(60), (_, i) => bucketsB[i % 2]);
43+
const previewBucketsWithNulls = [
44+
...Array.from(Array(10), (_, i) => ({ aggregatedValue: { value: null } })),
45+
...previewBucketsA.slice(10),
46+
];
47+
4148
export const basicMetricResponse = {
4249
aggregations: {
4350
aggregatedIntervals: {
@@ -150,3 +157,50 @@ export const changedSourceIdResponse = {
150157
},
151158
},
152159
};
160+
161+
export const basicMetricPreviewResponse = {
162+
aggregations: {
163+
aggregatedIntervals: {
164+
buckets: previewBucketsA,
165+
},
166+
},
167+
};
168+
169+
export const alternateMetricPreviewResponse = {
170+
aggregations: {
171+
aggregatedIntervals: {
172+
buckets: previewBucketsWithNulls,
173+
},
174+
},
175+
};
176+
177+
export const basicCompositePreviewResponse = {
178+
aggregations: {
179+
groupings: {
180+
after_key: { groupBy0: 'foo' },
181+
buckets: [
182+
{
183+
key: {
184+
groupBy0: 'a',
185+
},
186+
aggregatedIntervals: {
187+
buckets: previewBucketsA,
188+
},
189+
},
190+
{
191+
key: {
192+
groupBy0: 'b',
193+
},
194+
aggregatedIntervals: {
195+
buckets: previewBucketsB,
196+
},
197+
},
198+
],
199+
},
200+
},
201+
hits: {
202+
total: {
203+
value: 2,
204+
},
205+
},
206+
};

x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface NonCountMetricExpressionParams extends BaseMetricExpressionParams {
3535
}
3636

3737
interface CountMetricExpressionParams extends BaseMetricExpressionParams {
38-
aggType: 'count';
38+
aggType: Aggregators.COUNT;
3939
metric: never;
4040
}
4141

0 commit comments

Comments
 (0)