Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slo): remove react query cache for slo list page #170318

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove cache to fix inconsistent results
  • Loading branch information
kdelemme committed Nov 1, 2023
commit fe44530a3b35fbb5355937e0f6a98eb0527b9c56
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 Only change is here.
Added cacheTime, removed staleTime and keepPreviousData

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,
};
}