44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7- import React , { Fragment } from 'react' ;
8- import { EuiFlexGroup , EuiFlexItem , EuiFieldNumber , EuiFormRow } from '@elastic/eui' ;
7+ import React , { Fragment , useState } from 'react' ;
8+ import {
9+ EuiFlexGroup ,
10+ EuiFlexItem ,
11+ EuiFieldNumber ,
12+ EuiFormRow ,
13+ EuiPopover ,
14+ EuiExpression ,
15+ EuiSpacer ,
16+ } from '@elastic/eui' ;
917import { i18n } from '@kbn/i18n' ;
10- import { AlertTypeModel } from '../../../../plugins/triggers_actions_ui/public' ;
11- import { DEFAULT_INSTANCES_TO_GENERATE } from '../../common/constants' ;
12-
13- interface AlwaysFiringParamsProps {
14- alertParams : { instances ?: number } ;
15- setAlertParams : ( property : string , value : any ) => void ;
16- errors : { [ key : string ] : string [ ] } ;
17- }
18+ import { omit , pick } from 'lodash' ;
19+ import {
20+ ActionGroupWithCondition ,
21+ AlertConditions ,
22+ AlertConditionsGroup ,
23+ AlertTypeModel ,
24+ AlertTypeParamsExpressionProps ,
25+ AlertsContextValue ,
26+ } from '../../../../plugins/triggers_actions_ui/public' ;
27+ import {
28+ AlwaysFiringParams ,
29+ AlwaysFiringActionGroupIds ,
30+ DEFAULT_INSTANCES_TO_GENERATE ,
31+ } from '../../common/constants' ;
1832
1933export function getAlertType ( ) : AlertTypeModel {
2034 return {
@@ -24,7 +38,7 @@ export function getAlertType(): AlertTypeModel {
2438 iconClass : 'bolt' ,
2539 documentationUrl : null ,
2640 alertParamsExpression : AlwaysFiringExpression ,
27- validate : ( alertParams : AlwaysFiringParamsProps [ 'alertParams' ] ) => {
41+ validate : ( alertParams : AlwaysFiringParams ) => {
2842 const { instances } = alertParams ;
2943 const validationResult = {
3044 errors : {
@@ -44,11 +58,30 @@ export function getAlertType(): AlertTypeModel {
4458 } ;
4559}
4660
47- export const AlwaysFiringExpression : React . FunctionComponent < AlwaysFiringParamsProps > = ( {
48- alertParams,
49- setAlertParams,
50- } ) => {
51- const { instances = DEFAULT_INSTANCES_TO_GENERATE } = alertParams ;
61+ const DEFAULT_THRESHOLDS : AlwaysFiringParams [ 'thresholds' ] = {
62+ small : 0 ,
63+ medium : 5000 ,
64+ large : 10000 ,
65+ } ;
66+
67+ export const AlwaysFiringExpression : React . FunctionComponent < AlertTypeParamsExpressionProps <
68+ AlwaysFiringParams ,
69+ AlertsContextValue
70+ > > = ( { alertParams, setAlertParams, actionGroups, defaultActionGroupId } ) => {
71+ const {
72+ instances = DEFAULT_INSTANCES_TO_GENERATE ,
73+ thresholds = pick ( DEFAULT_THRESHOLDS , defaultActionGroupId ) ,
74+ } = alertParams ;
75+
76+ const actionGroupsWithConditions = actionGroups . map ( ( actionGroup ) =>
77+ Number . isInteger ( thresholds [ actionGroup . id as AlwaysFiringActionGroupIds ] )
78+ ? {
79+ ...actionGroup ,
80+ conditions : thresholds [ actionGroup . id as AlwaysFiringActionGroupIds ] ! ,
81+ }
82+ : actionGroup
83+ ) ;
84+
5285 return (
5386 < Fragment >
5487 < EuiFlexGroup gutterSize = "s" wrap direction = "column" >
@@ -67,6 +100,88 @@ export const AlwaysFiringExpression: React.FunctionComponent<AlwaysFiringParamsP
67100 </ EuiFormRow >
68101 </ EuiFlexItem >
69102 </ EuiFlexGroup >
103+ < EuiSpacer size = "m" />
104+ < EuiFlexGroup >
105+ < EuiFlexItem grow = { true } >
106+ < AlertConditions
107+ headline = { 'Set different thresholds for randomly generated T-Shirt sizes' }
108+ actionGroups = { actionGroupsWithConditions }
109+ onInitializeConditionsFor = { ( actionGroup ) => {
110+ setAlertParams ( 'thresholds' , {
111+ ...thresholds ,
112+ ...pick ( DEFAULT_THRESHOLDS , actionGroup . id ) ,
113+ } ) ;
114+ } }
115+ >
116+ < AlertConditionsGroup
117+ onResetConditionsFor = { ( actionGroup ) => {
118+ setAlertParams ( 'thresholds' , omit ( thresholds , actionGroup . id ) ) ;
119+ } }
120+ >
121+ < TShirtSelector
122+ setTShirtThreshold = { ( actionGroup ) => {
123+ setAlertParams ( 'thresholds' , {
124+ ...thresholds ,
125+ [ actionGroup . id ] : actionGroup . conditions ,
126+ } ) ;
127+ } }
128+ />
129+ </ AlertConditionsGroup >
130+ </ AlertConditions >
131+ </ EuiFlexItem >
132+ </ EuiFlexGroup >
133+ < EuiSpacer />
70134 </ Fragment >
71135 ) ;
72136} ;
137+
138+ interface TShirtSelectorProps {
139+ actionGroup ?: ActionGroupWithCondition < number > ;
140+ setTShirtThreshold : ( actionGroup : ActionGroupWithCondition < number > ) => void ;
141+ }
142+ const TShirtSelector = ( { actionGroup, setTShirtThreshold } : TShirtSelectorProps ) => {
143+ const [ isOpen , setIsOpen ] = useState ( false ) ;
144+
145+ if ( ! actionGroup ) {
146+ return null ;
147+ }
148+
149+ return (
150+ < EuiPopover
151+ panelPaddingSize = "s"
152+ button = {
153+ < EuiExpression
154+ description = { 'Is Above' }
155+ value = { actionGroup . conditions }
156+ isActive = { isOpen }
157+ onClick = { ( ) => setIsOpen ( true ) }
158+ />
159+ }
160+ isOpen = { isOpen }
161+ closePopover = { ( ) => setIsOpen ( false ) }
162+ ownFocus
163+ anchorPosition = "downLeft"
164+ >
165+ < EuiFlexGroup >
166+ < EuiFlexItem grow = { false } style = { { width : 150 } } >
167+ { 'Is Above' }
168+ </ EuiFlexItem >
169+ < EuiFlexItem grow = { false } style = { { width : 100 } } >
170+ < EuiFieldNumber
171+ compressed
172+ value = { actionGroup . conditions }
173+ onChange = { ( e ) => {
174+ const conditions = parseInt ( e . target . value , 10 ) ;
175+ if ( e . target . value && ! isNaN ( conditions ) ) {
176+ setTShirtThreshold ( {
177+ ...actionGroup ,
178+ conditions,
179+ } ) ;
180+ }
181+ } }
182+ />
183+ </ EuiFlexItem >
184+ </ EuiFlexGroup >
185+ </ EuiPopover >
186+ ) ;
187+ } ;
0 commit comments