-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slo): Show SLI preview chart for custom kql (#159713)
- Loading branch information
Showing
13 changed files
with
335 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
x-pack/plugins/observability/public/hooks/slo/use_get_preview_data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { GetPreviewDataResponse, Indicator } from '@kbn/slo-schema'; | ||
import { | ||
QueryObserverResult, | ||
RefetchOptions, | ||
RefetchQueryFilters, | ||
useQuery, | ||
} from '@tanstack/react-query'; | ||
import { useKibana } from '../../utils/kibana_react'; | ||
import { sloKeys } from './query_key_factory'; | ||
|
||
export interface UseGetPreviewData { | ||
data: GetPreviewDataResponse | undefined; | ||
isInitialLoading: boolean; | ||
isRefetching: boolean; | ||
isLoading: boolean; | ||
isSuccess: boolean; | ||
isError: boolean; | ||
refetch: <TPageData>( | ||
options?: (RefetchOptions & RefetchQueryFilters<TPageData>) | undefined | ||
) => Promise<QueryObserverResult<GetPreviewDataResponse | undefined, unknown>>; | ||
} | ||
|
||
export function useGetPreviewData(indicator?: Indicator): UseGetPreviewData { | ||
const { http } = useKibana().services; | ||
|
||
const { isInitialLoading, isLoading, isError, isSuccess, isRefetching, data, refetch } = useQuery( | ||
{ | ||
queryKey: sloKeys.preview(indicator), | ||
queryFn: async ({ signal }) => { | ||
const response = await http.post<GetPreviewDataResponse>( | ||
'/internal/observability/slos/_preview', | ||
{ | ||
body: JSON.stringify({ indicator }), | ||
signal, | ||
} | ||
); | ||
|
||
return response; | ||
}, | ||
retry: false, | ||
refetchOnWindowFocus: false, | ||
enabled: Boolean(indicator), | ||
} | ||
); | ||
|
||
return { | ||
data, | ||
isLoading, | ||
isRefetching, | ||
isInitialLoading, | ||
isSuccess, | ||
isError, | ||
refetch, | ||
}; | ||
} |
105 changes: 105 additions & 0 deletions
105
x-pack/plugins/observability/public/pages/slo_edit/components/common/data_preview_chart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { AreaSeries, Axis, Chart, Position, ScaleType, Settings } from '@elastic/charts'; | ||
import { EuiFlexItem, EuiIcon, EuiLoadingChart, EuiPanel } from '@elastic/eui'; | ||
import numeral from '@elastic/numeral'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { CreateSLOInput } from '@kbn/slo-schema'; | ||
import moment from 'moment'; | ||
import React from 'react'; | ||
import { useFormContext } from 'react-hook-form'; | ||
import { useKibana } from '../../../../utils/kibana_react'; | ||
import { useDebouncedGetPreviewData } from '../../hooks/use_preview'; | ||
export function DataPreviewChart() { | ||
const { watch, getFieldState } = useFormContext<CreateSLOInput>(); | ||
const { charts, uiSettings } = useKibana().services; | ||
|
||
const { data: previewData, isLoading: isPreviewLoading } = useDebouncedGetPreviewData( | ||
watch('indicator') | ||
); | ||
|
||
const theme = charts.theme.useChartsTheme(); | ||
const baseTheme = charts.theme.useChartsBaseTheme(); | ||
const dateFormat = uiSettings.get('dateFormat'); | ||
const percentFormat = uiSettings.get('format:percent:defaultPattern'); | ||
|
||
if (getFieldState('indicator').invalid) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<EuiFlexItem> | ||
{isPreviewLoading && <EuiLoadingChart size="m" mono />} | ||
{!isPreviewLoading && !!previewData && ( | ||
<EuiPanel hasBorder={true} hasShadow={false}> | ||
<Chart size={{ height: 160, width: '100%' }}> | ||
<Settings | ||
baseTheme={baseTheme} | ||
showLegend={false} | ||
theme={[ | ||
{ | ||
...theme, | ||
lineSeriesStyle: { | ||
point: { visible: false }, | ||
}, | ||
}, | ||
]} | ||
tooltip="vertical" | ||
noResults={ | ||
<EuiIcon type="visualizeApp" size="l" color="subdued" title="no results" /> | ||
} | ||
/> | ||
|
||
<Axis | ||
id="y-axis" | ||
title={i18n.translate('xpack.observability.slo.sloEdit.dataPreviewChart.yTitle', { | ||
defaultMessage: 'SLI', | ||
})} | ||
ticks={5} | ||
position={Position.Left} | ||
tickFormat={(d) => numeral(d).format(percentFormat)} | ||
/> | ||
|
||
<Axis | ||
id="time" | ||
title={i18n.translate('xpack.observability.slo.sloEdit.dataPreviewChart.xTitle', { | ||
defaultMessage: 'Last hour', | ||
})} | ||
tickFormat={(d) => moment(d).format(dateFormat)} | ||
position={Position.Bottom} | ||
timeAxisLayerCount={2} | ||
gridLine={{ visible: true }} | ||
style={{ | ||
tickLine: { size: 0.0001, padding: 4, visible: true }, | ||
tickLabel: { | ||
alignment: { | ||
horizontal: Position.Left, | ||
vertical: Position.Bottom, | ||
}, | ||
padding: 0, | ||
offset: { x: 0, y: 0 }, | ||
}, | ||
}} | ||
/> | ||
<AreaSeries | ||
id="SLI" | ||
xScaleType={ScaleType.Time} | ||
yScaleType={ScaleType.Linear} | ||
xAccessor="date" | ||
yAccessors={['value']} | ||
data={previewData.map((datum) => ({ | ||
date: new Date(datum.date).getTime(), | ||
value: datum.sliValue >= 0 ? datum.sliValue : null, | ||
}))} | ||
/> | ||
</Chart> | ||
</EuiPanel> | ||
)} | ||
</EuiFlexItem> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
x-pack/plugins/observability/public/pages/slo_edit/hooks/use_preview.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Indicator } from '@kbn/slo-schema'; | ||
import { debounce } from 'lodash'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useGetPreviewData } from '../../../hooks/slo/use_get_preview_data'; | ||
|
||
export function useDebouncedGetPreviewData(indicator: Indicator) { | ||
const serializedIndicator = JSON.stringify(indicator); | ||
const [indicatorState, setIndicatorState] = useState<string>(serializedIndicator); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const store = useCallback( | ||
debounce((value: string) => setIndicatorState(value), 800), | ||
[] | ||
); | ||
useEffect(() => { | ||
if (indicatorState !== serializedIndicator) { | ||
store(serializedIndicator); | ||
} | ||
}, [indicatorState, serializedIndicator, store]); | ||
|
||
return useGetPreviewData(JSON.parse(indicatorState)); | ||
} |
Oops, something went wrong.