[SLO] Display values >100% in preview chart (#173840)

## Summary

Fixes https://github.com/elastic/kibana/issues/170900

Display values >100% in preview chart with warning. 

<img width="989" alt="image"
src="9b2fb81c-36b7-4734-b4bc-74d6f774efb4">
This commit is contained in:
Shahzad 2023-12-28 11:58:09 +01:00 committed by GitHub
parent 7e17b6f34b
commit 445b757a65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 7 deletions

View file

@ -16,8 +16,11 @@ import {
ScaleType,
Settings,
Tooltip,
TooltipTable,
TooltipTableColumn,
} from '@elastic/charts';
import {
EuiCallOut,
EuiFlexGroup,
EuiFlexItem,
EuiFormRow,
@ -77,6 +80,8 @@ export function DataPreviewChart({
isError,
} = useDebouncedGetPreviewData(isIndicatorSectionValid, watch('indicator'), range);
const isMoreThan100 = previewData?.find((row) => row.sliValue > 1) != null;
const baseTheme = charts.theme.useChartsBaseTheme();
const dateFormat = uiSettings.get('dateFormat');
const numberFormat =
@ -165,9 +170,54 @@ export function DataPreviewChart({
</>
);
const columns: TooltipTableColumn[] = [
{
id: 'color',
type: 'color',
},
{
id: 'label',
type: 'custom',
truncate: true,
cell: ({ label }) => <span className="echTooltip__label">{label}</span>,
style: {
textAlign: 'left',
},
},
{
id: 'value',
type: 'custom',
cell: ({ formattedValue }) => (
<>
<span className="echTooltip__value" dir="ltr">
{formattedValue}
</span>
</>
),
style: {
textAlign: 'right',
},
},
];
return (
<EuiFlexItem>
{title}
{isMoreThan100 && (
<>
<EuiSpacer size="xs" />
<EuiCallOut
size="s"
color="warning"
title={i18n.translate('xpack.observability.slo.sloEdit.dataPreviewChart.moreThan100', {
defaultMessage:
'Some of the SLI values are more than 100%. That means good query is returning more results than total query.',
})}
iconType="warning"
/>
<EuiSpacer size="xs" />
</>
)}
<EuiFormRow fullWidth>
<EuiPanel hasBorder={true} hasShadow={false} style={{ minHeight: 194 }}>
{(isPreviewLoading || isError) && (
@ -189,7 +239,40 @@ export function DataPreviewChart({
)}
{isSuccess && (
<Chart size={{ height: 160, width: '100%' }}>
<Tooltip type="vertical" />
<Tooltip
type="vertical"
body={({ items }) => {
const firstItem = items[0];
const events = firstItem.datum.events;
const rows = [items[0]];
if (events) {
rows.push({
...firstItem,
formattedValue: events.good,
value: events.good,
label: i18n.translate(
'xpack.observability.slo.sloEdit.dataPreviewChart.goodEvents',
{
defaultMessage: 'Good events',
}
),
});
rows.push({
...firstItem,
value: events.total,
formattedValue: events.total,
label: i18n.translate(
'xpack.observability.slo.sloEdit.dataPreviewChart.badEvents',
{
defaultMessage: 'Total events',
}
),
});
}
return <TooltipTable columns={columns} items={rows} />;
}}
/>
<Settings
baseTheme={baseTheme}
showLegend={false}
@ -249,6 +332,7 @@ export function DataPreviewChart({
data={(previewData ?? []).map((datum) => ({
date: new Date(datum.date).getTime(),
value: datum.sliValue >= 0 ? datum.sliValue : null,
events: datum.events,
}))}
/>
</Chart>

View file

@ -16,8 +16,8 @@ describe('computeSLI', () => {
expect(computeSLI(100, 1000)).toEqual(0.1);
});
it('returns 1 when good is greater than total events', () => {
expect(computeSLI(9999, 9)).toEqual(1);
it('returns when good is greater than total events', () => {
expect(computeSLI(9999, 9)).toEqual(1111);
});
it('returns rounds the value to 6 digits', () => {

View file

@ -14,9 +14,5 @@ export function computeSLI(good: number, total: number): number {
return NO_DATA;
}
if (good >= total) {
return 1;
}
return toHighPrecision(good / total);
}