[Metrics UI] Fix evaluating rate-aggregated alerts when there's no normalized value (#73545)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Zacqary Adam Xeper 2020-07-29 13:05:19 -05:00 committed by GitHub
parent cbfdb36d9f
commit f8ba95f1fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 5 deletions

View file

@ -67,10 +67,13 @@ export const evaluateAlert = (
currentValue: Array.isArray(points) ? last(points)?.value : NaN,
timestamp: Array.isArray(points) ? last(points)?.key : NaN,
shouldFire: Array.isArray(points)
? points.map((point) => comparisonFunction(point.value, threshold))
? points.map(
(point) =>
typeof point.value === 'number' && comparisonFunction(point.value, threshold)
)
: [false],
isNoData: points === null,
isError: isNaN(points),
isNoData: (Array.isArray(points) ? last(points)?.value : points) === null,
isError: isNaN(Array.isArray(points) ? last(points)?.value : points),
};
});
})
@ -172,7 +175,7 @@ const getValuesFromAggregations = (
}
return buckets.map((bucket) => ({
key: bucket.key_as_string,
value: bucket.aggregatedValue.value,
value: bucket.aggregatedValue?.value ?? null,
}));
} catch (e) {
return NaN; // Error state

View file

@ -318,6 +318,31 @@ describe('The metric threshold alert type', () => {
});
});
describe("querying a rate-aggregated metric that hasn't reported data", () => {
const instanceID = '*';
const execute = () =>
executor({
services,
params: {
criteria: [
{
...baseCriterion,
comparator: Comparator.GT,
threshold: 1,
metric: 'test.metric.3',
aggType: 'rate',
},
],
alertOnNoData: true,
},
});
test('sends a No Data alert', async () => {
await execute();
expect(mostRecentAction(instanceID).id).toBe(FIRED_ACTIONS.id);
expect(getState(instanceID).alertState).toBe(AlertStates.NO_DATA);
});
});
// describe('querying a metric that later recovers', () => {
// const instanceID = '*';
// const execute = (threshold: number[]) =>
@ -401,7 +426,9 @@ services.callCluster.mockImplementation(async (_: string, { body, index }: any)
if (metric === 'test.metric.2') {
return mocks.alternateMetricResponse;
} else if (metric === 'test.metric.3') {
return mocks.emptyMetricResponse;
return body.aggs.aggregatedIntervals.aggregations.aggregatedValue_max
? mocks.emptyRateResponse
: mocks.emptyMetricResponse;
}
return mocks.basicMetricResponse;
});

View file

@ -62,6 +62,19 @@ export const emptyMetricResponse = {
},
};
export const emptyRateResponse = {
aggregations: {
aggregatedIntervals: {
buckets: [
{
doc_count: 2,
aggregatedValue_max: { value: null },
},
],
},
},
};
export const basicCompositeResponse = {
aggregations: {
groupings: {