From ae542071d70db42e621fdb77a7891c39456eb203 Mon Sep 17 00:00:00 2001 From: Konrad Lalik Date: Tue, 16 Dec 2025 14:38:42 +0100 Subject: [PATCH] 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`. --- .../features/alerting/unified/featureToggles.ts | 2 ++ .../unified/rule-list/GrafanaGroupLoader.tsx | 4 ++-- .../unified/rule-list/PaginatedGrafanaLoader.tsx | 2 ++ .../rule-list/hooks/prometheusGroupsGenerator.ts | 16 +++++++++++++--- .../rule-list/hooks/useFilteredRulesIterator.ts | 6 +++++- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/public/app/features/alerting/unified/featureToggles.ts b/public/app/features/alerting/unified/featureToggles.ts index 7ca1747a8c5..8d907dd7a66 100644 --- a/public/app/features/alerting/unified/featureToggles.ts +++ b/public/app/features/alerting/unified/featureToggles.ts @@ -26,3 +26,5 @@ export const shouldUseBackendFilters = () => config.featureToggles.alertingUIUse export const shouldUseFullyCompatibleBackendFilters = () => config.featureToggles.alertingUIUseFullyCompatBackendFilters ?? false; + +export const shouldUseCompactRulesResponse = () => config.featureToggles.alertingCompactRulesResponse ?? false; diff --git a/public/app/features/alerting/unified/rule-list/GrafanaGroupLoader.tsx b/public/app/features/alerting/unified/rule-list/GrafanaGroupLoader.tsx index 670670d6801..052009be582 100644 --- a/public/app/features/alerting/unified/rule-list/GrafanaGroupLoader.tsx +++ b/public/app/features/alerting/unified/rule-list/GrafanaGroupLoader.tsx @@ -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 } ); diff --git a/public/app/features/alerting/unified/rule-list/PaginatedGrafanaLoader.tsx b/public/app/features/alerting/unified/rule-list/PaginatedGrafanaLoader.tsx index 9dfd25309ba..febf60ae2ed 100644 --- a/public/app/features/alerting/unified/rule-list/PaginatedGrafanaLoader.tsx +++ b/public/app/features/alerting/unified/rule-list/PaginatedGrafanaLoader.tsx @@ -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. diff --git a/public/app/features/alerting/unified/rule-list/hooks/prometheusGroupsGenerator.ts b/public/app/features/alerting/unified/rule-list/hooks/prometheusGroupsGenerator.ts index 61d4ebf1f0a..b8381fcd85f 100644 --- a/public/app/features/alerting/unified/rule-list/hooks/prometheusGroupsGenerator.ts +++ b/public/app/features/alerting/unified/rule-list/hooks/prometheusGroupsGenerator.ts @@ -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( diff --git a/public/app/features/alerting/unified/rule-list/hooks/useFilteredRulesIterator.ts b/public/app/features/alerting/unified/rule-list/hooks/useFilteredRulesIterator.ts index 5202c580542..9db0bee02fe 100644 --- a/public/app/features/alerting/unified/rule-list/hooks/useFilteredRulesIterator.ts +++ b/public/app/features/alerting/unified/rule-list/hooks/useFilteredRulesIterator.ts @@ -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 */