|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * React component for rendering a select element with threshold levels. |
| 9 | + * This is basically a copy of SelectSeverity in public/application/components/controls/select_severity |
| 10 | + * but which stores its state internally rather than in the appState |
| 11 | + */ |
| 12 | +import React, { Fragment, FC, useState } from 'react'; |
| 13 | +import { i18n } from '@kbn/i18n'; |
| 14 | +import { FormattedMessage } from '@kbn/i18n/react'; |
| 15 | + |
| 16 | +import { EuiHealth, EuiSpacer, EuiSuperSelect, EuiText } from '@elastic/eui'; |
| 17 | + |
| 18 | +import { getSeverityColor } from '../../../../../../common/util/anomaly_utils'; |
| 19 | + |
| 20 | +const warningLabel = i18n.translate('xpack.ml.controls.selectSeverity.warningLabel', { |
| 21 | + defaultMessage: 'warning', |
| 22 | +}); |
| 23 | +const minorLabel = i18n.translate('xpack.ml.controls.selectSeverity.minorLabel', { |
| 24 | + defaultMessage: 'minor', |
| 25 | +}); |
| 26 | +const majorLabel = i18n.translate('xpack.ml.controls.selectSeverity.majorLabel', { |
| 27 | + defaultMessage: 'major', |
| 28 | +}); |
| 29 | +const criticalLabel = i18n.translate('xpack.ml.controls.selectSeverity.criticalLabel', { |
| 30 | + defaultMessage: 'critical', |
| 31 | +}); |
| 32 | + |
| 33 | +const optionsMap = { |
| 34 | + [warningLabel]: 0, |
| 35 | + [minorLabel]: 25, |
| 36 | + [majorLabel]: 50, |
| 37 | + [criticalLabel]: 75, |
| 38 | +}; |
| 39 | + |
| 40 | +interface TableSeverity { |
| 41 | + val: number; |
| 42 | + display: string; |
| 43 | + color: string; |
| 44 | +} |
| 45 | + |
| 46 | +export const SEVERITY_OPTIONS: TableSeverity[] = [ |
| 47 | + { |
| 48 | + val: 0, |
| 49 | + display: warningLabel, |
| 50 | + color: getSeverityColor(0), |
| 51 | + }, |
| 52 | + { |
| 53 | + val: 25, |
| 54 | + display: minorLabel, |
| 55 | + color: getSeverityColor(25), |
| 56 | + }, |
| 57 | + { |
| 58 | + val: 50, |
| 59 | + display: majorLabel, |
| 60 | + color: getSeverityColor(50), |
| 61 | + }, |
| 62 | + { |
| 63 | + val: 75, |
| 64 | + display: criticalLabel, |
| 65 | + color: getSeverityColor(75), |
| 66 | + }, |
| 67 | +]; |
| 68 | + |
| 69 | +function optionValueToThreshold(value: number) { |
| 70 | + // Get corresponding threshold object with required display and val properties from the specified value. |
| 71 | + let threshold = SEVERITY_OPTIONS.find(opt => opt.val === value); |
| 72 | + |
| 73 | + // Default to warning if supplied value doesn't map to one of the options. |
| 74 | + if (threshold === undefined) { |
| 75 | + threshold = SEVERITY_OPTIONS[0]; |
| 76 | + } |
| 77 | + |
| 78 | + return threshold; |
| 79 | +} |
| 80 | + |
| 81 | +const TABLE_SEVERITY_DEFAULT = SEVERITY_OPTIONS[0]; |
| 82 | + |
| 83 | +const getSeverityOptions = () => |
| 84 | + SEVERITY_OPTIONS.map(({ color, display, val }) => ({ |
| 85 | + value: display, |
| 86 | + inputDisplay: ( |
| 87 | + <Fragment> |
| 88 | + <EuiHealth color={color} style={{ lineHeight: 'inherit' }}> |
| 89 | + {display} |
| 90 | + </EuiHealth> |
| 91 | + </Fragment> |
| 92 | + ), |
| 93 | + dropdownDisplay: ( |
| 94 | + <Fragment> |
| 95 | + <EuiHealth color={color} style={{ lineHeight: 'inherit' }}> |
| 96 | + {display} |
| 97 | + </EuiHealth> |
| 98 | + <EuiSpacer size="xs" /> |
| 99 | + <EuiText size="xs" color="subdued"> |
| 100 | + <p className="euiTextColor--subdued"> |
| 101 | + <FormattedMessage |
| 102 | + id="xpack.ml.controls.selectSeverity.scoreDetailsDescription" |
| 103 | + defaultMessage="score {value} and above" |
| 104 | + values={{ value: val }} |
| 105 | + /> |
| 106 | + </p> |
| 107 | + </EuiText> |
| 108 | + </Fragment> |
| 109 | + ), |
| 110 | + })); |
| 111 | + |
| 112 | +interface Props { |
| 113 | + onChange: (sev: TableSeverity) => void; |
| 114 | +} |
| 115 | + |
| 116 | +export const SelectSeverity: FC<Props> = ({ onChange }) => { |
| 117 | + const [severity, setSeverity] = useState(TABLE_SEVERITY_DEFAULT); |
| 118 | + |
| 119 | + const onSeverityChange = (valueDisplay: string) => { |
| 120 | + const option = optionValueToThreshold(optionsMap[valueDisplay]); |
| 121 | + setSeverity(option); |
| 122 | + onChange(option); |
| 123 | + }; |
| 124 | + |
| 125 | + return ( |
| 126 | + <EuiSuperSelect |
| 127 | + hasDividers |
| 128 | + options={getSeverityOptions()} |
| 129 | + valueOfSelected={severity.display} |
| 130 | + onChange={onSeverityChange} |
| 131 | + /> |
| 132 | + ); |
| 133 | +}; |
0 commit comments