Refactor compact parameter to be configurable via hook options

Moves the `compact` parameter from being hardcoded inside the
`useGrafanaGroupsGenerator` hook to being an explicit option in
`UseGeneratorHookOptions`. This improves flexibility and consistency
across the codebase.

Changes:
- Add `compact` parameter to `UseGeneratorHookOptions` interface
- Update `useGrafanaGroupsGenerator` to use `compact` from hook options
- Update all call sites to explicitly pass `shouldUseCompactRulesResponse()`
- Replace direct feature toggle access with `shouldUseCompactRulesResponse()` helper

This change makes the feature toggle decision visible at call sites,
allows for easier testing, and follows the existing pattern used by
other hook options like `populateCache` and `limitAlerts`.
This commit is contained in:
Konrad Lalik
2025-12-16 14:38:42 +01:00
parent e9b92ce955
commit ae542071d7
5 changed files with 24 additions and 6 deletions
@@ -26,3 +26,5 @@ export const shouldUseBackendFilters = () => config.featureToggles.alertingUIUse
export const shouldUseFullyCompatibleBackendFilters = () =>
config.featureToggles.alertingUIUseFullyCompatBackendFilters ?? false;
export const shouldUseCompactRulesResponse = () => config.featureToggles.alertingCompactRulesResponse ?? false;
@@ -3,11 +3,11 @@ import { useMemo } from 'react';
import { GrafanaTheme2 } from '@grafana/data';
import { t } from '@grafana/i18n';
import { config } from '@grafana/runtime';
import { Alert, Stack, useStyles2 } from '@grafana/ui';
import { GrafanaRuleGroupIdentifier } from 'app/types/unified-alerting';
import { prometheusApi } from '../api/prometheusApi';
import { shouldUseCompactRulesResponse } from '../featureToggles';
import { useContinuousPagination } from '../hooks/usePagination';
import { DEFAULT_PER_PAGE_PAGINATION_RULES_PER_GROUP, RULE_LIST_POLL_INTERVAL_MS } from '../utils/constants';
@@ -45,7 +45,7 @@ export function GrafanaGroupLoader({
folderUid: groupIdentifier.namespace.uid,
groupName: groupIdentifier.groupName,
limitAlerts: 0,
compact: config.featureToggles.alertingCompactRulesResponse,
compact: shouldUseCompactRulesResponse(),
},
{ pollingInterval: RULE_LIST_POLL_INTERVAL_MS }
);
@@ -7,6 +7,7 @@ import { GrafanaPromRuleGroupDTO, PromRuleGroupDTO } from 'app/types/unified-ale
import { FolderActionsButton } from '../components/folder-actions/FolderActionsButton';
import { GrafanaNoRulesCTA } from '../components/rules/NoRulesCTA';
import { shouldUseCompactRulesResponse } from '../featureToggles';
import { GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
import { groups } from '../utils/navigation';
@@ -47,6 +48,7 @@ function PaginatedGroupsLoader({ groupFilter, namespaceFilter }: LoaderProps) {
const grafanaGroupsGenerator = useGrafanaGroupsGenerator({
populateCache: needsClientSideFiltering ? false : true,
limitAlerts: 0,
compact: shouldUseCompactRulesResponse(),
});
// If there are no filters we can match one frontend page to one API page.
@@ -1,7 +1,6 @@
import { useCallback } from 'react';
import { MergeExclusive } from 'type-fest';
import { config } from '@grafana/runtime';
import { DataSourceRulesSourceIdentifier, RuleHealth } from 'app/types/unified-alerting';
import { PromAlertingRuleState, PromRuleGroupDTO } from 'app/types/unified-alerting-dto';
@@ -16,6 +15,11 @@ interface UseGeneratorHookOptions {
*/
populateCache?: boolean;
limitAlerts?: number;
/**
* Whether to use compact response format from the API.
* Typically controlled by the alertingCompactRulesResponse feature toggle.
*/
compact?: boolean;
}
export function usePrometheusGroupsGenerator() {
@@ -77,7 +81,7 @@ export function useGrafanaGroupsGenerator(hookOptions: UseGeneratorHookOptions =
...fetchOptions,
limitAlerts: hookOptions.limitAlerts,
...fetchOptions.filter,
compact: config.featureToggles.alertingCompactRulesResponse,
compact: hookOptions.compact,
}).unwrap();
if (hookOptions.populateCache) {
@@ -86,7 +90,13 @@ export function useGrafanaGroupsGenerator(hookOptions: UseGeneratorHookOptions =
return response;
},
[getGrafanaGroups, hookOptions.limitAlerts, hookOptions.populateCache, populateGroupsResponseCache]
[
getGrafanaGroups,
hookOptions.limitAlerts,
hookOptions.compact,
hookOptions.populateCache,
populateGroupsResponseCache,
]
);
return useCallback(
@@ -15,6 +15,7 @@ import {
PromRuleGroupDTO,
} from 'app/types/unified-alerting-dto';
import { shouldUseCompactRulesResponse } from '../../featureToggles';
import { RuleSource, RulesFilter } from '../../search/rulesSearchParser';
import {
getDataSourceByUid,
@@ -76,7 +77,10 @@ export function useFilteredRulesIteratorProvider() {
const allExternalRulesSources = getExternalRulesSources();
const prometheusGroupsGenerator = usePrometheusGroupsGenerator();
const grafanaGroupsGenerator = useGrafanaGroupsGenerator({ limitAlerts: 0 });
const grafanaGroupsGenerator = useGrafanaGroupsGenerator({
limitAlerts: 0,
compact: shouldUseCompactRulesResponse(),
});
const getFilteredRulesIterable = (filterState: RulesFilter, options: FetchGroupsLimitOptions): GetIteratorResult => {
/* this is the abort controller that allows us to stop an AsyncIterable */