import { css, cx } from '@emotion/css'; import { FormEvent } from 'react'; import { GrafanaTheme2, SelectableValue } from '@grafana/data'; import { Trans } from '@grafana/i18n'; import { Button, ButtonSelect, Icon, InlineFieldRow, Input, Select, useStyles2, Stack } from '@grafana/ui'; import alertDef, { EvalFunction } from '../../alerting/state/alertDef'; import { ClassicCondition, ReducerType } from '../types'; interface Props { condition: ClassicCondition; onChange: (condition: ClassicCondition) => void; onRemoveCondition: (id: number) => void; index: number; refIds: Array>; } const reducerFunctions = alertDef.reducerTypes.map<{ label: string; value: ReducerType; }>((rt) => ({ label: rt.text, value: rt.value })); const evalOperators = alertDef.evalOperators.map((eo) => ({ label: eo.text, value: eo.value })); const evalFunctions = alertDef.evalFunctions.map((ef) => ({ label: ef.text, value: ef.value })); export const Condition = ({ condition, index, onChange, onRemoveCondition, refIds }: Props) => { const styles = useStyles2(getStyles); const onEvalOperatorChange = (evalOperator: SelectableValue) => { onChange({ ...condition, operator: { type: evalOperator.value! }, }); }; const onReducerFunctionChange = (conditionFunction: SelectableValue) => { onChange({ ...condition, reducer: { type: conditionFunction.value!, params: [] }, }); }; const onRefIdChange = (refId: SelectableValue) => { onChange({ ...condition, query: { params: [refId.value!] }, }); }; const onEvalFunctionChange = (evalFunction: SelectableValue) => { onChange({ ...condition, evaluator: { params: condition.evaluator.params, type: evalFunction.value! }, }); }; const onEvaluateValueChange = (event: FormEvent, index: number) => { const newValue = parseFloat(event.currentTarget.value); const newParams = [...condition.evaluator.params]; newParams[index] = newValue; onChange({ ...condition, evaluator: { ...condition.evaluator, params: newParams }, }); }; const buttonWidth = css({ width: '75px', }); const isRange = condition.evaluator.type === EvalFunction.IsWithinRange || condition.evaluator.type === EvalFunction.IsOutsideRange || condition.evaluator.type === EvalFunction.IsOutsideRangeIncluded || condition.evaluator.type === EvalFunction.IsWithinRangeIncluded; return (
{index === 0 ? (
WHEN
) : ( ea.value === condition.operator!.type)} /> )} options={reducerFunctions} onChange={onReducerFunctionChange} width={20} value={reducerFunctions.find((rf) => rf.value === condition.reducer.type)} />
OF
onEvaluateValueChange(event, 0)} value={condition.evaluator.params[0]} />
TO
onEvaluateValueChange(event, 1)} value={condition.evaluator.params[1]} /> ) : condition.evaluator.type !== EvalFunction.HasNoValue ? ( onEvaluateValueChange(event, 0)} value={condition.evaluator.params[0]} /> ) : null}
); }; const getStyles = (theme: GrafanaTheme2) => { const buttonStyle = css({ color: theme.colors.primary.text, fontSize: theme.typography.bodySmall.fontSize, }); return { buttonSelectText: buttonStyle, button: cx( css({ display: 'flex', alignItems: 'center', borderRadius: theme.shape.radius.default, fontWeight: theme.typography.fontWeightMedium, border: `1px solid ${theme.colors.border.weak}`, whiteSpace: 'nowrap', padding: `0 ${theme.spacing(1)}`, backgroundColor: theme.colors.background.canvas, }), buttonStyle ), }; };