11import { HttpResponse } from '@angular/common/http' ;
22import { Component , EventEmitter , Input , OnDestroy , OnInit , Output } from '@angular/core' ;
3- import { FormBuilder , FormGroup , Validators } from '@angular/forms' ;
3+ import { AbstractControl , FormArray , FormBuilder , FormGroup , Validators } from '@angular/forms' ;
44import { NgbActiveModal , NgbModal } from '@ng-bootstrap/ng-bootstrap' ;
55import { UUID } from 'angular2-uuid' ;
66import { Observable , Subject } from 'rxjs' ;
@@ -20,7 +20,7 @@ import {
2020 ALERT_INCIDENT_FLAG_FIELD ,
2121 ALERT_INCIDENT_MODULE_FIELD ,
2222 ALERT_INCIDENT_OBSERVATION_FIELD ,
23- ALERT_INCIDENT_USER_FIELD ,
23+ ALERT_INCIDENT_USER_FIELD , ALERT_NAME_FIELD ,
2424 ALERT_NOTE_FIELD ,
2525 ALERT_OBSERVATION_FIELD ,
2626 ALERT_REFERENCE_FIELD ,
@@ -142,18 +142,12 @@ export class AlertRuleCreateComponent implements OnInit, OnDestroy {
142142 ngOnInit ( ) {
143143
144144 this . initForm ( ) ;
145+ this . createDefaultFilters ( ) ;
145146 this . getTags ( ) ;
146147 this . formRule . get ( 'name' ) . valueChanges . pipe ( debounceTime ( 3000 ) ) . subscribe ( ruleName => {
147148 this . searchRule ( ruleName ) ;
148149 } ) ;
149150
150- if ( ! this . alert ) {
151- this . loading = true ;
152- this . alertService . notifyRefresh ( true ) ;
153- } else {
154- this . createDefaultFilters ( ) ;
155- }
156-
157151 if ( this . rule ) {
158152 this . filters = [ ... this . rule . conditions ] ;
159153 this . selected = this . rule . tags . length > 0 ? [ ...this . rule . tags ] : [ ] ;
@@ -173,18 +167,52 @@ export class AlertRuleCreateComponent implements OnInit, OnDestroy {
173167 id : [ this . rule ? this . rule . id : null ] ,
174168 name : [ this . rule ? this . rule . name : '' , Validators . required ] ,
175169 description : [ this . rule ? this . rule . description : '' , Validators . required ] ,
176- conditions : [ this . rule ? this . rule . conditions : [ ] , Validators . required ] ,
170+ conditions : this . fb . array ( [ ] , Validators . required ) ,
177171 tags : [ this . rule ? this . rule . tags : null , Validators . required ] ,
178172 } ) ;
179173 }
180174
181175 createDefaultFilters ( ) {
176+ if ( this . alert ) {
177+ this . initConditionsFromAlert ( ) ;
178+ } else {
179+ this . initConditionsFromAction ( ) ;
180+ }
181+
182+ this . subscribeToFieldChanges ( ) ;
183+ }
184+
185+ private initConditionsFromAlert ( ) {
182186 for ( const field of this . fields ) {
183- if ( this . getFieldValue ( field . field ) ) {
184- this . filters . push ( { field : field . field , operator : ElasticOperatorsEnum . IS , value : this . getFieldValue ( field . field ) } ) ;
187+ const value = this . getFieldValue ( field . field ) ;
188+ if ( value ) {
189+ const condition = this . buildCondition ( field . field , value ) ;
190+ this . filters . push ( condition ) ;
191+ this . ruleConditions . push ( this . createConditionGroup ( condition ) ) ;
185192 }
186193 }
187- this . formRule . get ( 'conditions' ) . setValue ( this . filters ) ;
194+ }
195+
196+ private initConditionsFromAction ( ) {
197+ if ( this . action === 'create' ) {
198+ const field = this . fields [ 0 ] ;
199+ const condition = this . buildCondition ( field . field , '' ) ;
200+ this . filters . push ( condition ) ;
201+ this . ruleConditions . push ( this . createConditionGroup ( condition ) ) ;
202+ } else if ( this . action === 'update' ) {
203+ this . filters = [ ...this . rule . conditions ] ;
204+ this . rule . conditions . forEach ( condition => {
205+ this . ruleConditions . push ( this . createConditionGroup ( condition ) ) ;
206+ } ) ;
207+ }
208+ }
209+
210+ private buildCondition ( field : string , value : any ) {
211+ return {
212+ field,
213+ operator : ElasticOperatorsEnum . IS ,
214+ value
215+ } ;
188216 }
189217
190218 getFieldValue ( field : string ) : any {
@@ -262,8 +290,8 @@ export class AlertRuleCreateComponent implements OnInit, OnDestroy {
262290
263291 }
264292
265- deleteFilter ( filter : ElasticFilterType ) {
266- const index = this . filters . indexOf ( filter ) ;
293+ deleteFilter ( elasticFilterType : ElasticFilterType ) {
294+ const index = this . filters . indexOf ( elasticFilterType ) ;
267295 if ( index !== - 1 ) {
268296 this . filters . splice ( index , 1 ) ;
269297 this . formRule . get ( 'conditions' ) . setValue ( this . filters ) ;
@@ -326,8 +354,8 @@ export class AlertRuleCreateComponent implements OnInit, OnDestroy {
326354 return this . selected . findIndex ( value => value . tagName . includes ( 'False positive' ) ) !== - 1 ;
327355 }
328356
329- getOperators ( filter : ElasticFilterType ) {
330- const field = this . fields . find ( f => f . field === filter . field ) ;
357+ getOperators ( conditionField : string ) {
358+ const field = this . fields . find ( f => f . field === conditionField ) ;
331359 if ( field ) {
332360 return this . operatorService . getOperators ( { name : field . field , type : field . type } , this . operators ) ;
333361 }
@@ -391,6 +419,48 @@ export class AlertRuleCreateComponent implements OnInit, OnDestroy {
391419 this . activeModal . close ( ) ;
392420 }
393421
422+ get ruleConditions ( ) {
423+ return this . formRule . get ( 'conditions' ) as FormArray ;
424+ }
425+
426+ removeRuleCondition ( index : number ) {
427+ this . ruleConditions . removeAt ( index ) ;
428+ }
429+
430+ createConditionGroup ( condition : any ) : FormGroup {
431+ return this . fb . group ( {
432+ field : [ condition . field , Validators . required ] ,
433+ operator : [ condition . operator , Validators . required ] ,
434+ value : [ condition . value ]
435+ } ) ;
436+ }
437+
438+ subscribeToFieldChanges ( ) {
439+ this . ruleConditions . controls . forEach ( ( group : AbstractControl , index : number ) => {
440+ const fieldControl = group . get ( 'field' ) ;
441+ const operatorControl = group . get ( 'operator' ) ;
442+
443+ fieldControl . valueChanges
444+ . pipe ( takeUntil ( this . destroy$ ) )
445+ . subscribe ( newField => {
446+ const validOperators = this . getOperators ( newField ) . map ( op => op . operator ) ;
447+ if ( ! validOperators . includes ( operatorControl . value ) ) {
448+ operatorControl . setValue ( null ) ;
449+ }
450+ } ) ;
451+ } ) ;
452+ }
453+
454+ addRuleCondition ( ) {
455+ this . ruleConditions . push (
456+ this . createConditionGroup ( {
457+ field : this . fields [ 0 ] . field ,
458+ operator : null ,
459+ value : null
460+ } ) ) ;
461+ this . subscribeToFieldChanges ( ) ;
462+ }
463+
394464 ngOnDestroy ( ) : void {
395465 this . destroy$ . next ( ) ;
396466 this . destroy$ . complete ( ) ;
0 commit comments