|
| 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 | +}; |
0 commit comments