From 5bc67f84e4dc60fdb1c632875f2ea8856682e843 Mon Sep 17 00:00:00 2001 From: Konrad Lalik Date: Tue, 16 Dec 2025 15:14:00 +0100 Subject: [PATCH] Alerting: Fix cache key mismatch in populateGroupsResponseCache Updates the cache population logic to include `compact` and `limitAlerts` parameters in the cache key. Previously, the cache was populated with only `folderUid`, `groupName`, and a hardcoded `limitAlerts: 0`, which caused cache key mismatches when queries used different parameter values. Changes: - Add `PopulateCacheOptions` interface for cache parameters - Update `populateGroupsResponseCache` to accept options parameter - Pass `limitAlerts` and `compact` from hook options to cache function This ensures RTK Query cache keys match the actual query parameters, enabling proper cache hits and avoiding redundant API calls. --- .../features/alerting/unified/api/prometheusApi.ts | 14 ++++++++++++-- .../rule-list/hooks/prometheusGroupsGenerator.ts | 5 ++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/public/app/features/alerting/unified/api/prometheusApi.ts b/public/app/features/alerting/unified/api/prometheusApi.ts index 4f4cf16d634..13ffa587647 100644 --- a/public/app/features/alerting/unified/api/prometheusApi.ts +++ b/public/app/features/alerting/unified/api/prometheusApi.ts @@ -138,16 +138,26 @@ export const prometheusApi = alertingApi.injectEndpoints({ }), }); +export interface PopulateCacheOptions { + limitAlerts?: number; + compact?: boolean; +} + export function usePopulateGrafanaPrometheusApiCache() { const dispatch = useDispatch(); const populateGroupsResponseCache = useCallback( - (groups: GrafanaPromRuleGroupDTO[]) => { + (groups: GrafanaPromRuleGroupDTO[], options: PopulateCacheOptions = {}) => { dispatch( prometheusApi.util.upsertQueryEntries( groups.map((group) => ({ endpointName: 'getGrafanaGroups', - arg: { folderUid: group.folderUid, groupName: group.name, limitAlerts: 0 }, + arg: { + folderUid: group.folderUid, + groupName: group.name, + limitAlerts: options.limitAlerts ?? 0, + compact: options.compact, + }, value: { data: { groups: [group] }, status: 'success' }, })) ) 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 b8381fcd85f..12458171f10 100644 --- a/public/app/features/alerting/unified/rule-list/hooks/prometheusGroupsGenerator.ts +++ b/public/app/features/alerting/unified/rule-list/hooks/prometheusGroupsGenerator.ts @@ -85,7 +85,10 @@ export function useGrafanaGroupsGenerator(hookOptions: UseGeneratorHookOptions = }).unwrap(); if (hookOptions.populateCache) { - populateGroupsResponseCache(response.data.groups); + populateGroupsResponseCache(response.data.groups, { + limitAlerts: hookOptions.limitAlerts, + compact: hookOptions.compact, + }); } return response;