import { throttle } from 'lodash'; import { getBackendSrv, locationService } from '@grafana/runtime'; import { SupportBundle, SupportBundleCollector, SupportBundleCreateRequest, ThunkResult } from 'app/types'; import { collectorsFetchBegin, collectorsFetchEnd, fetchBegin, fetchEnd, setCreateBundleError, setLoadBundleError, supportBundleCollectorsLoaded, supportBundlesLoaded, } from './reducers'; export function loadBundles(skipPageRefresh = false): ThunkResult { return async (dispatch) => { try { if (!skipPageRefresh) { dispatch(fetchBegin()); } const result = await getBackendSrv().get('/api/support-bundles'); dispatch(supportBundlesLoaded(result)); } finally { dispatch(fetchEnd()); } }; } const checkBundlesStatusThrottled = throttle(async (dispatch) => { const result = await getBackendSrv().get('/api/support-bundles'); dispatch(supportBundlesLoaded(result)); }, 1000); export function checkBundles(): ThunkResult { return async (dispatch) => { dispatch(checkBundlesStatusThrottled); }; } export function removeBundle(uid: string): ThunkResult { return async (dispatch) => { await getBackendSrv().delete(`/api/support-bundles/${uid}`); dispatch(loadBundles(true)); }; } export function loadSupportBundleCollectors(): ThunkResult { return async (dispatch) => { try { dispatch(collectorsFetchBegin()); const result = await getBackendSrv().get('/api/support-bundles/collectors'); dispatch(supportBundleCollectorsLoaded(result)); } catch (err) { dispatch(setLoadBundleError('Error loading support bundles data collectors')); } finally { dispatch(collectorsFetchEnd()); } }; } export function createSupportBundle(data: SupportBundleCreateRequest): ThunkResult { return async (dispatch) => { try { await getBackendSrv().post('/api/support-bundles', data); locationService.push('/support-bundles'); } catch (err) { dispatch(setCreateBundleError('Error creating support bundle')); } }; }