|
|
@ -1,10 +1,9 @@ |
|
|
|
import { useCallback } from 'react'; |
|
|
|
import { useCallback } from 'react'; |
|
|
|
|
|
|
|
|
|
|
|
import { useDispatch } from 'app/types/store'; |
|
|
|
|
|
|
|
import { DataSourceRulesSourceIdentifier, RuleHealth } from 'app/types/unified-alerting'; |
|
|
|
import { DataSourceRulesSourceIdentifier, RuleHealth } from 'app/types/unified-alerting'; |
|
|
|
import { PromAlertingRuleState, PromRuleGroupDTO } from 'app/types/unified-alerting-dto'; |
|
|
|
import { PromAlertingRuleState, PromRuleGroupDTO } from 'app/types/unified-alerting-dto'; |
|
|
|
|
|
|
|
|
|
|
|
import { PromRulesResponse, prometheusApi } from '../../api/prometheusApi'; |
|
|
|
import { PromRulesResponse, prometheusApi, usePopulateGrafanaPrometheusApiCache } from '../../api/prometheusApi'; |
|
|
|
|
|
|
|
|
|
|
|
const { useLazyGetGroupsQuery, useLazyGetGrafanaGroupsQuery } = prometheusApi; |
|
|
|
const { useLazyGetGroupsQuery, useLazyGetGrafanaGroupsQuery } = prometheusApi; |
|
|
|
|
|
|
|
|
|
|
@ -22,8 +21,7 @@ interface FetchGroupsOptions { |
|
|
|
groupNextToken?: string; |
|
|
|
groupNextToken?: string; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function usePrometheusGroupsGenerator(hookOptions: UseGeneratorHookOptions = {}) { |
|
|
|
export function usePrometheusGroupsGenerator() { |
|
|
|
const dispatch = useDispatch(); |
|
|
|
|
|
|
|
const [getGroups] = useLazyGetGroupsQuery(); |
|
|
|
const [getGroups] = useLazyGetGroupsQuery(); |
|
|
|
|
|
|
|
|
|
|
|
return useCallback( |
|
|
|
return useCallback( |
|
|
@ -35,24 +33,12 @@ export function usePrometheusGroupsGenerator(hookOptions: UseGeneratorHookOption |
|
|
|
...fetchOptions, |
|
|
|
...fetchOptions, |
|
|
|
}).unwrap(); |
|
|
|
}).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
if (hookOptions.populateCache) { |
|
|
|
|
|
|
|
response.data.groups.forEach((group) => { |
|
|
|
|
|
|
|
dispatch( |
|
|
|
|
|
|
|
prometheusApi.util.upsertQueryData( |
|
|
|
|
|
|
|
'getGroups', |
|
|
|
|
|
|
|
{ ruleSource: { uid: ruleSource.uid }, namespace: group.file, groupName: group.name }, |
|
|
|
|
|
|
|
{ data: { groups: [group] }, status: 'success' } |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
return response; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
yield* genericGroupsGenerator(getRuleSourceGroupsWithCache, groupLimit); |
|
|
|
yield* genericGroupsGenerator(getRuleSourceGroupsWithCache, groupLimit); |
|
|
|
}, |
|
|
|
}, |
|
|
|
[getGroups, dispatch, hookOptions.populateCache] |
|
|
|
[getGroups] |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -67,8 +53,8 @@ interface GrafanaFetchGroupsOptions extends FetchGroupsOptions { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function useGrafanaGroupsGenerator(hookOptions: UseGeneratorHookOptions = {}) { |
|
|
|
export function useGrafanaGroupsGenerator(hookOptions: UseGeneratorHookOptions = {}) { |
|
|
|
const dispatch = useDispatch(); |
|
|
|
|
|
|
|
const [getGrafanaGroups] = useLazyGetGrafanaGroupsQuery(); |
|
|
|
const [getGrafanaGroups] = useLazyGetGrafanaGroupsQuery(); |
|
|
|
|
|
|
|
const { populateGroupsResponseCache } = usePopulateGrafanaPrometheusApiCache(); |
|
|
|
|
|
|
|
|
|
|
|
const getGroupsAndProvideCache = useCallback( |
|
|
|
const getGroupsAndProvideCache = useCallback( |
|
|
|
async (fetchOptions: GrafanaFetchGroupsOptions) => { |
|
|
|
async (fetchOptions: GrafanaFetchGroupsOptions) => { |
|
|
@ -78,25 +64,13 @@ export function useGrafanaGroupsGenerator(hookOptions: UseGeneratorHookOptions = |
|
|
|
...fetchOptions.filter, |
|
|
|
...fetchOptions.filter, |
|
|
|
}).unwrap(); |
|
|
|
}).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
// This is not mandatory to preload ruler rules, but it improves the UX
|
|
|
|
|
|
|
|
// Because the user waits a bit longer for the initial load but doesn't need to wait for each group to be loaded
|
|
|
|
|
|
|
|
if (hookOptions.populateCache) { |
|
|
|
if (hookOptions.populateCache) { |
|
|
|
const cacheAndRulerPreload = response.data.groups.map(async (group) => { |
|
|
|
populateGroupsResponseCache(response.data.groups); |
|
|
|
await dispatch( |
|
|
|
|
|
|
|
prometheusApi.util.upsertQueryData( |
|
|
|
|
|
|
|
'getGrafanaGroups', |
|
|
|
|
|
|
|
{ folderUid: group.folderUid, groupName: group.name, limitAlerts: hookOptions.limitAlerts }, |
|
|
|
|
|
|
|
{ data: { groups: [group] }, status: 'success' } |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Promise.allSettled(cacheAndRulerPreload); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
return response; |
|
|
|
}, |
|
|
|
}, |
|
|
|
[getGrafanaGroups, dispatch, hookOptions.populateCache, hookOptions.limitAlerts] |
|
|
|
[getGrafanaGroups, hookOptions.limitAlerts, hookOptions.populateCache, populateGroupsResponseCache] |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return useCallback( |
|
|
|
return useCallback( |
|
|
|