Skip to content

Commit

Permalink
[Metrics UI] Add ability for user to set anomaly threshold (#90313) (#…
Browse files Browse the repository at this point in the history
…90827)

* add anomaly threshold configuration to settings

* hide panel if not ml capable

* send threshold value to query

* update license

* update api integration to expect anomalyThreshold to exist

* add some tests to the anomaly queries

* change type to number

* api validate in source update and config for anamolyThreshold and tests

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
neptunian and kibanamachine authored Feb 9, 2021
1 parent fa8adf1 commit 277fe42
Show file tree
Hide file tree
Showing 29 changed files with 441 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const getMetricsHostsAnomaliesRequestPayloadRT = rt.type({
rt.type({
// the ID of the source configuration
sourceId: rt.string,
anomalyThreshold: rt.number,
// the time range to fetch the log entry anomalies from
timeRange: timeRangeRT,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const getMetricsK8sAnomaliesRequestPayloadRT = rt.type({
rt.type({
// the ID of the source configuration
sourceId: rt.string,
anomalyThreshold: rt.number,
// the time range to fetch the log entry anomalies from
timeRange: timeRangeRT,
}),
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/infra/common/http_api/source_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const SavedSourceConfigurationRuntimeType = rt.partial({
metricsExplorerDefaultView: rt.string,
fields: SavedSourceConfigurationFieldsRuntimeType,
logColumns: rt.array(SavedSourceConfigurationColumnRuntimeType),
anomalyThreshold: rt.number,
});

export interface InfraSavedSourceConfiguration
Expand All @@ -107,6 +108,7 @@ export const pickSavedSourceConfiguration = (
inventoryDefaultView,
metricsExplorerDefaultView,
logColumns,
anomalyThreshold,
} = value;
const { container, host, pod, tiebreaker, timestamp } = fields;

Expand All @@ -119,6 +121,7 @@ export const pickSavedSourceConfiguration = (
metricsExplorerDefaultView,
fields: { container, host, pod, tiebreaker, timestamp },
logColumns,
anomalyThreshold,
};
};

Expand All @@ -140,6 +143,7 @@ export const StaticSourceConfigurationRuntimeType = rt.partial({
metricsExplorerDefaultView: rt.string,
fields: StaticSourceConfigurationFieldsRuntimeType,
logColumns: rt.array(SavedSourceConfigurationColumnRuntimeType),
anomalyThreshold: rt.number,
});

export interface InfraStaticSourceConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('ExpressionChart', () => {
pod: 'kubernetes.pod.uid',
tiebreaker: '_doc',
},
anomalyThreshold: 20,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import { ReactNode, useCallback, useMemo, useState } from 'react';

import { createInputFieldProps, validateInputFieldNotEmpty } from './input_fields';
import {
createInputFieldProps,
createInputRangeFieldProps,
validateInputFieldNotEmpty,
} from './input_fields';

interface FormState {
name: string;
Expand All @@ -20,6 +24,7 @@ interface FormState {
podField: string;
tiebreakerField: string;
timestampField: string;
anomalyThreshold: number;
}

type FormStateChanges = Partial<FormState>;
Expand Down Expand Up @@ -124,6 +129,17 @@ export const useIndicesConfigurationFormState = ({
}),
[formState.timestampField]
);
const anomalyThresholdFieldProps = useMemo(
() =>
createInputRangeFieldProps({
errors: validateInputFieldNotEmpty(formState.anomalyThreshold),
name: 'anomalyThreshold',
onChange: (anomalyThreshold) =>
setFormStateChanges((changes) => ({ ...changes, anomalyThreshold })),
value: formState.anomalyThreshold,
}),
[formState.anomalyThreshold]
);

const fieldProps = useMemo(
() => ({
Expand All @@ -135,6 +151,7 @@ export const useIndicesConfigurationFormState = ({
podField: podFieldFieldProps,
tiebreakerField: tiebreakerFieldFieldProps,
timestampField: timestampFieldFieldProps,
anomalyThreshold: anomalyThresholdFieldProps,
}),
[
nameFieldProps,
Expand All @@ -145,6 +162,7 @@ export const useIndicesConfigurationFormState = ({
podFieldFieldProps,
tiebreakerFieldFieldProps,
timestampFieldFieldProps,
anomalyThresholdFieldProps,
]
);

Expand Down Expand Up @@ -183,4 +201,5 @@ const defaultFormState: FormState = {
podField: '',
tiebreakerField: '',
timestampField: '',
anomalyThreshold: 0,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React from 'react';
import React, { ReactText } from 'react';

import { FormattedMessage } from '@kbn/i18n/react';

Expand Down Expand Up @@ -43,7 +43,47 @@ export const createInputFieldProps = <
value,
});

export const validateInputFieldNotEmpty = (value: string) =>
export interface InputRangeFieldProps<
Value extends ReactText = ReactText,
FieldElement extends HTMLInputElement = HTMLInputElement,
ButtonElement extends HTMLButtonElement = HTMLButtonElement
> {
error: React.ReactNode[];
isInvalid: boolean;
name: string;
onChange?: (
evt: React.ChangeEvent<FieldElement> | React.MouseEvent<ButtonElement>,
isValid: boolean
) => void;
value: Value;
}

export const createInputRangeFieldProps = <
Value extends ReactText = ReactText,
FieldElement extends HTMLInputElement = HTMLInputElement,
ButtonElement extends HTMLButtonElement = HTMLButtonElement
>({
errors,
name,
onChange,
value,
}: {
errors: FieldErrorMessage[];
name: string;
onChange: (newValue: number, isValid: boolean) => void;
value: Value;
}): InputRangeFieldProps<Value, FieldElement, ButtonElement> => ({
error: errors,
isInvalid: errors.length > 0,
name,
onChange: (
evt: React.ChangeEvent<FieldElement> | React.MouseEvent<ButtonElement>,
isValid: boolean
) => onChange(+evt.currentTarget.value, isValid),
value,
});

export const validateInputFieldNotEmpty = (value: React.ReactText) =>
value === ''
? [
<FormattedMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 { EuiTitle } from '@elastic/eui';
import { EuiSpacer } from '@elastic/eui';
import { EuiFormRow } from '@elastic/eui';
import { EuiRange } from '@elastic/eui';
import { EuiDescribedFormGroup } from '@elastic/eui';
import { EuiForm } from '@elastic/eui';
import React from 'react';
import { FormattedMessage } from 'react-intl';
import { InputRangeFieldProps } from './input_fields';

interface MLConfigurationPanelProps {
isLoading: boolean;
readOnly: boolean;
anomalyThresholdFieldProps: InputRangeFieldProps;
}

export const MLConfigurationPanel = ({
isLoading,
readOnly,
anomalyThresholdFieldProps,
}: MLConfigurationPanelProps) => {
return (
<EuiForm>
<EuiTitle size="s">
<h3>
<FormattedMessage
id="xpack.infra.sourceConfiguration.mlSectionTitle"
defaultMessage="Machine Learning"
/>
</h3>
</EuiTitle>
<EuiSpacer size="m" />
<EuiDescribedFormGroup
title={
<h4>
<FormattedMessage
id="xpack.infra.sourceConfiguration.anomalyThresholdTitle"
defaultMessage="Anomaly Severity Threshold"
/>
</h4>
}
description={
<FormattedMessage
id="xpack.infra.sourceConfiguration.anomalyThresholdDescription"
defaultMessage="Sets the minimum severity score required to show anomalies in the Metrics application."
/>
}
>
<EuiFormRow
error={anomalyThresholdFieldProps.error}
fullWidth
isInvalid={anomalyThresholdFieldProps.isInvalid}
label={
<FormattedMessage
id="xpack.infra.sourceConfiguration.anomalyThresholdLabel"
defaultMessage="Minimum severity score"
/>
}
>
<EuiRange
min={0}
max={100}
data-test-subj="anomalyThresholdInput"
showInput
showTicks
showRange
tickInterval={20}
aria-label="Minimum severity score"
disabled={isLoading}
readOnly={readOnly}
isLoading={isLoading}
{...anomalyThresholdFieldProps}
/>
</EuiFormRow>
</EuiDescribedFormGroup>
</EuiForm>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const useSourceConfigurationFormState = (configuration?: InfraSourceConfi
podField: configuration.fields.pod,
tiebreakerField: configuration.fields.tiebreaker,
timestampField: configuration.fields.timestamp,
anomalyThreshold: configuration.anomalyThreshold,
}
: undefined,
[configuration]
Expand Down Expand Up @@ -79,6 +80,7 @@ export const useSourceConfigurationFormState = (configuration?: InfraSourceConfi
timestamp: indicesConfigurationFormState.formState.timestampField,
},
logColumns: logColumnsConfigurationFormState.formState.logColumns,
anomalyThreshold: indicesConfigurationFormState.formState.anomalyThreshold,
}),
[indicesConfigurationFormState.formState, logColumnsConfigurationFormState.formState]
);
Expand All @@ -97,6 +99,7 @@ export const useSourceConfigurationFormState = (configuration?: InfraSourceConfi
timestamp: indicesConfigurationFormState.formStateChanges.timestampField,
},
logColumns: logColumnsConfigurationFormState.formStateChanges.logColumns,
anomalyThreshold: indicesConfigurationFormState.formStateChanges.anomalyThreshold,
}),
[
indicesConfigurationFormState.formStateChanges,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { NameConfigurationPanel } from './name_configuration_panel';
import { useSourceConfigurationFormState } from './source_configuration_form_state';
import { SourceLoadingPage } from '../source_loading_page';
import { Prompt } from '../../utils/navigation_warning_prompt';
import { MLConfigurationPanel } from './ml_configuration_panel';
import { useInfraMLCapabilitiesContext } from '../../containers/ml/infra_ml_capabilities';

interface SourceConfigurationSettingsProps {
shouldAllowEdit: boolean;
Expand All @@ -52,7 +54,6 @@ export const SourceConfigurationSettings = ({
formState,
formStateChanges,
} = useSourceConfigurationFormState(source && source.configuration);

const persistUpdates = useCallback(async () => {
if (sourceExists) {
await updateSourceConfiguration(formStateChanges);
Expand All @@ -74,6 +75,8 @@ export const SourceConfigurationSettings = ({
source,
]);

const { hasInfraMLCapabilites } = useInfraMLCapabilitiesContext();

if ((isLoading || isUninitialized) && !source) {
return <SourceLoadingPage />;
}
Expand Down Expand Up @@ -125,6 +128,18 @@ export const SourceConfigurationSettings = ({
/>
</EuiPanel>
<EuiSpacer />
{hasInfraMLCapabilites && (
<>
<EuiPanel paddingSize="l">
<MLConfigurationPanel
isLoading={isLoading}
readOnly={!isWriteable}
anomalyThresholdFieldProps={indicesConfigurationProps.anomalyThreshold}
/>
</EuiPanel>
<EuiSpacer />
</>
)}
{errors.length > 0 ? (
<>
<EuiCallOut color="danger">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface Props {
}

export const Timeline: React.FC<Props> = ({ interval, yAxisFormatter, isVisible }) => {
const { sourceId } = useSourceContext();
const { sourceId, source } = useSourceContext();
const { metric, nodeType, accountId, region } = useWaffleOptionsContext();
const { currentTime, jumpToTime, stopAutoReload } = useWaffleTimeContext();
const { filterQueryAsJson } = useWaffleFiltersContext();
Expand All @@ -70,6 +70,7 @@ export const Timeline: React.FC<Props> = ({ interval, yAxisFormatter, isVisible

const anomalyParams = {
sourceId: 'default',
anomalyThreshold: source?.configuration.anomalyThreshold || 0,
startTime,
endTime,
defaultSortOptions: {
Expand Down
Loading

0 comments on commit 277fe42

Please sign in to comment.