Skip to content

Commit

Permalink
Remove cache to fix inconsistent results
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme committed Nov 1, 2023
1 parent f21e31a commit fe44530
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ export const useFetchSloList = (): UseFetchSloListResponse => {
isError: false,
isSuccess: true,
data: sloList,
refetch: function () {} as UseFetchSloListResponse['refetch'],
};
};
105 changes: 46 additions & 59 deletions x-pack/plugins/observability/public/hooks/slo/use_fetch_slo_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@
* 2.0.
*/

import { useState } from 'react';
import {
QueryObserverResult,
RefetchOptions,
RefetchQueryFilters,
useQuery,
useQueryClient,
} from '@tanstack/react-query';
import { i18n } from '@kbn/i18n';
import { FindSLOResponse } from '@kbn/slo-schema';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useState } from 'react';

import { useKibana } from '../../utils/kibana_react';
import { sloKeys } from './query_key_factory';
Expand All @@ -34,9 +28,6 @@ export interface UseFetchSloListResponse {
isSuccess: boolean;
isError: boolean;
data: FindSLOResponse | undefined;
refetch: <TPageData>(
options?: (RefetchOptions & RefetchQueryFilters<TPageData>) | undefined
) => Promise<QueryObserverResult<FindSLOResponse | undefined, unknown>>;
}

const SHORT_REFETCH_INTERVAL = 1000 * 5; // 5 seconds
Expand All @@ -56,56 +47,53 @@ export function useFetchSloList({
const queryClient = useQueryClient();
const [stateRefetchInterval, setStateRefetchInterval] = useState<number>(SHORT_REFETCH_INTERVAL);

const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data, refetch } = useQuery(
{
queryKey: sloKeys.list({ kqlQuery, page, sortBy, sortDirection }),
queryFn: async ({ signal }) => {
const response = await http.get<FindSLOResponse>(`/api/observability/slos`, {
query: {
...(kqlQuery && { kqlQuery }),
...(sortBy && { sortBy }),
...(sortDirection && { sortDirection }),
...(page && { page }),
},
signal,
});
const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data } = useQuery({
queryKey: sloKeys.list({ kqlQuery, page, sortBy, sortDirection }),
queryFn: async ({ signal }) => {
const response = await http.get<FindSLOResponse>(`/api/observability/slos`, {
query: {
...(kqlQuery && { kqlQuery }),
...(sortBy && { sortBy }),
...(sortDirection && { sortDirection }),
...(page && { page }),
},
signal,
});

return response;
},
keepPreviousData: true,
refetchOnWindowFocus: false,
refetchInterval: shouldRefetch ? stateRefetchInterval : undefined,
staleTime: 1000,
retry: (failureCount, error) => {
if (String(error) === 'Error: Forbidden') {
return false;
}
return failureCount < 4;
},
onSuccess: ({ results }: FindSLOResponse) => {
queryClient.invalidateQueries({ queryKey: sloKeys.historicalSummaries(), exact: false });
queryClient.invalidateQueries({ queryKey: sloKeys.activeAlerts(), exact: false });
queryClient.invalidateQueries({ queryKey: sloKeys.rules(), exact: false });
return response;
},
cacheTime: 0,
refetchOnWindowFocus: false,
refetchInterval: shouldRefetch ? stateRefetchInterval : undefined,
retry: (failureCount, error) => {
if (String(error) === 'Error: Forbidden') {
return false;
}
return failureCount < 4;
},
onSuccess: ({ results }: FindSLOResponse) => {
queryClient.invalidateQueries({ queryKey: sloKeys.historicalSummaries(), exact: false });
queryClient.invalidateQueries({ queryKey: sloKeys.activeAlerts(), exact: false });
queryClient.invalidateQueries({ queryKey: sloKeys.rules(), exact: false });

if (!shouldRefetch) {
return;
}
if (!shouldRefetch) {
return;
}

if (results.find((slo) => slo.summary.status === 'NO_DATA' || !slo.summary)) {
setStateRefetchInterval(SHORT_REFETCH_INTERVAL);
} else {
setStateRefetchInterval(LONG_REFETCH_INTERVAL);
}
},
onError: (error: Error) => {
toasts.addError(error, {
title: i18n.translate('xpack.observability.slo.list.errorNotification', {
defaultMessage: 'Something went wrong while fetching SLOs',
}),
});
},
}
);
if (results.find((slo) => slo.summary.status === 'NO_DATA' || !slo.summary)) {
setStateRefetchInterval(SHORT_REFETCH_INTERVAL);
} else {
setStateRefetchInterval(LONG_REFETCH_INTERVAL);
}
},
onError: (error: Error) => {
toasts.addError(error, {
title: i18n.translate('xpack.observability.slo.list.errorNotification', {
defaultMessage: 'Something went wrong while fetching SLOs',
}),
});
},
});

return {
data,
Expand All @@ -114,6 +102,5 @@ export function useFetchSloList({
isRefetching,
isSuccess,
isError,
refetch,
};
}

0 comments on commit fe44530

Please sign in to comment.