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.
This commit is contained in:
Konrad Lalik
2025-12-16 15:14:00 +01:00
parent d5fdafed83
commit 5bc67f84e4
2 changed files with 16 additions and 3 deletions
@@ -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' },
}))
)
@@ -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;