From 49b763f8e73e5616c194cd8daae3ffba1f6b7093 Mon Sep 17 00:00:00 2001 From: Coen Warmer Date: Wed, 12 Apr 2023 23:31:38 +0200 Subject: [PATCH] [SLO] Add refresh button to SLO Detail page (#154856) --- .../burn_rate_rule_editor.tsx | 4 ++- .../hooks/slo/use_fetch_historical_summary.ts | 4 +++ .../public/hooks/slo/use_fetch_slo_details.ts | 17 +++++++++++-- .../components/slo_details.stories.tsx | 1 + .../slo_details/components/slo_details.tsx | 5 ++-- .../public/pages/slo_details/slo_details.tsx | 25 ++++++++++++++++--- .../public/pages/slo_edit/slo_edit.tsx | 2 +- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/observability/public/components/app/burn_rate_rule_editor/burn_rate_rule_editor.tsx b/x-pack/plugins/observability/public/components/app/burn_rate_rule_editor/burn_rate_rule_editor.tsx index 3582fba4ac2de..2f15c3d473a7a 100644 --- a/x-pack/plugins/observability/public/components/app/burn_rate_rule_editor/burn_rate_rule_editor.tsx +++ b/x-pack/plugins/observability/public/components/app/burn_rate_rule_editor/burn_rate_rule_editor.tsx @@ -28,7 +28,9 @@ type Props = Pick< export function BurnRateRuleEditor(props: Props) { const { setRuleParams, ruleParams, errors } = props; - const { isLoading: loadingInitialSlo, slo: initialSlo } = useFetchSloDetails(ruleParams?.sloId); + const { isLoading: loadingInitialSlo, slo: initialSlo } = useFetchSloDetails({ + sloId: ruleParams?.sloId, + }); const [selectedSlo, setSelectedSlo] = useState(undefined); const [longWindowDuration, setLongWindowDuration] = useState({ diff --git a/x-pack/plugins/observability/public/hooks/slo/use_fetch_historical_summary.ts b/x-pack/plugins/observability/public/hooks/slo/use_fetch_historical_summary.ts index 469d98e63538c..fe9ffd92f579d 100644 --- a/x-pack/plugins/observability/public/hooks/slo/use_fetch_historical_summary.ts +++ b/x-pack/plugins/observability/public/hooks/slo/use_fetch_historical_summary.ts @@ -21,10 +21,13 @@ export interface UseFetchHistoricalSummaryResponse { export interface Params { sloIds: string[]; + shouldRefetch?: boolean; } +const LONG_REFETCH_INTERVAL = 1000 * 60; // 1 minute export function useFetchHistoricalSummary({ sloIds = [], + shouldRefetch, }: Params): UseFetchHistoricalSummaryResponse { const { http } = useKibana().services; @@ -45,6 +48,7 @@ export function useFetchHistoricalSummary({ // ignore error } }, + refetchInterval: shouldRefetch ? LONG_REFETCH_INTERVAL : undefined, refetchOnWindowFocus: false, }); diff --git a/x-pack/plugins/observability/public/hooks/slo/use_fetch_slo_details.ts b/x-pack/plugins/observability/public/hooks/slo/use_fetch_slo_details.ts index be90446a94736..92ccf45d6fe4c 100644 --- a/x-pack/plugins/observability/public/hooks/slo/use_fetch_slo_details.ts +++ b/x-pack/plugins/observability/public/hooks/slo/use_fetch_slo_details.ts @@ -15,7 +15,9 @@ import { GetSLOResponse, SLOWithSummaryResponse } from '@kbn/slo-schema'; import { useKibana } from '../../utils/kibana_react'; export interface UseFetchSloDetailsResponse { + isInitialLoading: boolean; isLoading: boolean; + isRefetching: boolean; isSuccess: boolean; isError: boolean; slo: SLOWithSummaryResponse | undefined; @@ -24,7 +26,15 @@ export interface UseFetchSloDetailsResponse { ) => Promise>; } -export function useFetchSloDetails(sloId?: string): UseFetchSloDetailsResponse { +const LONG_REFETCH_INTERVAL = 1000 * 60; // 1 minute + +export function useFetchSloDetails({ + sloId, + shouldRefetch, +}: { + sloId?: string; + shouldRefetch?: boolean; +}): UseFetchSloDetailsResponse { const { http } = useKibana().services; const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data, refetch } = useQuery( @@ -43,13 +53,16 @@ export function useFetchSloDetails(sloId?: string): UseFetchSloDetailsResponse { } }, enabled: Boolean(sloId), + refetchInterval: shouldRefetch ? LONG_REFETCH_INTERVAL : undefined, refetchOnWindowFocus: false, } ); return { slo: data, - isLoading: isInitialLoading || isLoading || isRefetching, + isLoading, + isInitialLoading, + isRefetching, isSuccess, isError, refetch, diff --git a/x-pack/plugins/observability/public/pages/slo_details/components/slo_details.stories.tsx b/x-pack/plugins/observability/public/pages/slo_details/components/slo_details.stories.tsx index 237af2c0698ae..067cef3aab55a 100644 --- a/x-pack/plugins/observability/public/pages/slo_details/components/slo_details.stories.tsx +++ b/x-pack/plugins/observability/public/pages/slo_details/components/slo_details.stories.tsx @@ -22,6 +22,7 @@ const Template: ComponentStory = (props: Props) => ().services; const { ObservabilityPageTemplate } = usePluginContext(); + const { hasAtLeast } = useLicense(); const hasRightLicense = hasAtLeast('platinum'); const { sloId } = useParams(); - const { isLoading, slo } = useFetchSloDetails(sloId); + + const [isAutoRefreshing, setIsAutoRefreshing] = useState(true); + + const { isLoading, slo } = useFetchSloDetails({ sloId, shouldRefetch: isAutoRefreshing }); + useBreadcrumbs(getBreadcrumbs(basePath, slo)); const isSloNotFound = !isLoading && slo === undefined; @@ -48,17 +54,28 @@ export function SloDetailsPage() { navigateToUrl(basePath.prepend(paths.observability.slos)); } + const handleToggleAutoRefresh = () => { + setIsAutoRefreshing(!isAutoRefreshing); + }; + return ( , - rightSideItems: [], + rightSideItems: [ + , + , + ], bottomBorder: false, }} data-test-subj="sloDetailsPage" > {isLoading && } - {!isLoading && } + {!isLoading && } ); } diff --git a/x-pack/plugins/observability/public/pages/slo_edit/slo_edit.tsx b/x-pack/plugins/observability/public/pages/slo_edit/slo_edit.tsx index 438e6a78ded54..135a2f2279955 100644 --- a/x-pack/plugins/observability/public/pages/slo_edit/slo_edit.tsx +++ b/x-pack/plugins/observability/public/pages/slo_edit/slo_edit.tsx @@ -38,7 +38,7 @@ export function SloEditPage() { }, ]); - const { slo, isLoading } = useFetchSloDetails(sloId); + const { slo, isLoading } = useFetchSloDetails({ sloId }); if (hasRightLicense === false) { navigateToUrl(basePath.prepend(paths.observability.slos));