Files
grafana/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.test.ts
Sonia Aguilar 64ee42d01e Alerting: Add limits and move state and label matching filters to the BE (#66267)
* WIP

* Add instance totals to combined rule. Use totals to display instances stats in the UI

* WIP

* add global summaries, fix TS errors

* fix useCombined test

* fix test

* use activeAt from rule when available

* Fix NaN in global stats

* Add no data total to global summary

* Add totals recalculation for filtered rules

* Fix instances totals, remove instances filtering from alert list view

* Update tests

* Fetch alerts considering filtering label matchers

* WIP - Fetch alerts appending state filter to endpoint

* Fix multiple values for state in request being applyied

* fix test

* Calculate hidden by for grafana managed alerts

* Use INSTANCES_DISPLAY_LIMIT constant for limiting alert instances instead of 1

* Rename matchers parameter according to API changes

* Fix calculating total number of grafana instances

* Rename matcher prop after previous change

* Display button to remove max instances limit

* Change matcher query param to be an array of strings

* Add test for paramsWithMatcherAndState method

* Refactor matcher to be an string array to be consistent with state

* Use matcher query string as matcher object type (encoded JSON)

* Avoind encoding matcher parameters twice

* fix tests

* Enable toggle for the limit/show all button and restore limit and filters when we come back from custom view

* Move getMatcherListFromString method to utils/alertmanager.ts

* Fix limit toggle button being shown when it's not necessary

* Use filteredTotals from be response to calculate hidden by count

* Fix variables not being replaced correctly

* Fix total shown to be all the instances filtered without limits

* Adress some PR review comments

* Move paramsWithMatcherAndState inside prometheusUrlBuilder method

---------

Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com>
Co-authored-by: Virginia Cepeda <virginia.cepeda@grafana.com>
2023-04-25 11:19:20 +02:00

65 lines
1.7 KiB
TypeScript

import { CombinedRuleGroup, CombinedRuleNamespace } from 'app/types/unified-alerting';
import { sortRulesByName, flattenGrafanaManagedRules } from './useCombinedRuleNamespaces';
describe('flattenGrafanaManagedRules', () => {
it('should properly transform grafana managed namespaces', () => {
// the rules from both ungrouped groups should go in the default group
const ungroupedGroup1 = {
name: 'my-rule',
rules: [{ name: 'my-rule' }],
totals: {},
} as CombinedRuleGroup;
const ungroupedGroup2 = {
name: 'another-rule',
rules: [{ name: 'another-rule' }],
totals: {},
} as CombinedRuleGroup;
// the rules from both these groups should go in their own group name
const group1 = {
name: 'group1',
rules: [{ name: 'rule-1' }, { name: 'rule-2' }],
totals: {},
} as CombinedRuleGroup;
const group2 = {
name: 'group2',
rules: [{ name: 'rule-1' }, { name: 'rule-2' }],
totals: {},
} as CombinedRuleGroup;
const namespace1 = {
rulesSource: 'grafana',
name: 'ns1',
groups: [ungroupedGroup1, ungroupedGroup2, group1, group2],
};
const namespace2 = {
rulesSource: 'grafana',
name: 'ns2',
groups: [ungroupedGroup1],
};
const input = [namespace1, namespace2] as CombinedRuleNamespace[];
const [ns1, ns2] = flattenGrafanaManagedRules(input);
expect(ns1.groups).toEqual([
{
name: 'default',
rules: sortRulesByName([...ungroupedGroup1.rules, ...ungroupedGroup2.rules, ...group1.rules, ...group2.rules]),
totals: {},
},
]);
expect(ns2.groups).toEqual([
{
name: 'default',
rules: ungroupedGroup1.rules,
totals: {},
},
]);
});
});