Skip to content

Commit

Permalink
[Metrics UI] Clear threshold alert groups state when filterQuery chan…
Browse files Browse the repository at this point in the history
…ges (#116205)
  • Loading branch information
Zacqary authored Oct 26, 2021
1 parent cd3ec12 commit f389b74
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,50 @@ describe('The metric threshold alert type', () => {
);
expect(stateResult3.groups).toEqual(expect.arrayContaining(['a', 'b']));
});

const executeWithFilter = (
comparator: Comparator,
threshold: number[],
filterQuery: string,
metric?: string,
state?: any
) =>
executor({
...mockOptions,
services,
params: {
groupBy: ['something'],
criteria: [
{
...baseNonCountCriterion,
comparator,
threshold,
metric: metric ?? baseNonCountCriterion.metric,
},
],
},
state: state ?? mockOptions.state.wrapped,
});
test('persists previous groups that go missing, until the filterQuery param changes', async () => {
const stateResult1 = await executeWithFilter(Comparator.GT, [0.75], 'query', 'test.metric.2');
expect(stateResult1.groups).toEqual(expect.arrayContaining(['a', 'b', 'c']));
const stateResult2 = await executeWithFilter(
Comparator.GT,
[0.75],
'query',
'test.metric.1',
stateResult1
);
expect(stateResult2.groups).toEqual(expect.arrayContaining(['a', 'b', 'c']));
const stateResult3 = await executeWithFilter(
Comparator.GT,
[0.75],
'different query',
'test.metric.1',
stateResult2
);
expect(stateResult3.groups).toEqual(expect.arrayContaining(['a', 'b']));
});
});

describe('querying with multiple criteria', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type MetricThresholdAlertTypeParams = Record<string, any>;
export type MetricThresholdAlertTypeState = AlertTypeState & {
groups: string[];
groupBy?: string | string[];
filterQuery?: string;
};
export type MetricThresholdAlertInstanceState = AlertInstanceState; // no specific instace state used
export type MetricThresholdAlertInstanceContext = AlertInstanceContext; // no specific instace state used
Expand Down Expand Up @@ -94,8 +95,11 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
const config = source.configuration;

const previousGroupBy = state.groupBy;
const previousFilterQuery = state.filterQuery;
const prevGroups =
alertOnGroupDisappear && isEqual(previousGroupBy, params.groupBy)
alertOnGroupDisappear &&
isEqual(previousGroupBy, params.groupBy) &&
isEqual(previousFilterQuery, params.filterQuery)
? // Filter out the * key from the previous groups, only include it if it's one of
// the current groups. In case of a groupBy alert that starts out with no data and no
// groups, we don't want to persist the existence of the * alert instance
Expand Down Expand Up @@ -220,7 +224,7 @@ export const createMetricThresholdExecutor = (libs: InfraBackendLibs) =>
}
}

return { groups, groupBy: params.groupBy };
return { groups, groupBy: params.groupBy, filterQuery: params.filterQuery };
});

export const FIRED_ACTIONS = {
Expand Down

0 comments on commit f389b74

Please sign in to comment.