|
|
|
@ -14,16 +14,15 @@ |
|
|
|
/// limitations under the License.
|
|
|
|
///
|
|
|
|
|
|
|
|
import { Component, DestroyRef, Inject, ViewChild, ViewEncapsulation } from '@angular/core'; |
|
|
|
import { Component, DestroyRef, Inject, ViewEncapsulation } from '@angular/core'; |
|
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|
|
|
import { Store } from '@ngrx/store'; |
|
|
|
import { AppState } from '@core/core.state'; |
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|
|
|
import { FormGroup } from '@angular/forms'; |
|
|
|
import { Router } from '@angular/router'; |
|
|
|
import { DialogComponent } from '@shared/components/dialog.component'; |
|
|
|
import { CalculatedField, CalculatedFieldArgument, CalculatedFieldType } from '@shared/models/calculated-field.models'; |
|
|
|
import { oneSpaceInsideRegex } from '@shared/models/regex.constants'; |
|
|
|
import { AliasEntityType, EntityType, entityTypeTranslations } from '@shared/models/entity-type.models'; |
|
|
|
import { EntityType, entityTypeTranslations } from '@shared/models/entity-type.models'; |
|
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
|
|
|
import { ScriptLanguage } from '@shared/models/rule-node.models'; |
|
|
|
import { CalculatedFieldsService } from '@core/http/calculated-fields.service'; |
|
|
|
@ -39,13 +38,11 @@ import { |
|
|
|
} from "@shared/models/alarm-rule.models"; |
|
|
|
import { deepTrim } from "@core/utils"; |
|
|
|
import { combineLatest, Observable } from "rxjs"; |
|
|
|
import { debounceTime, startWith, switchMap } from "rxjs/operators"; |
|
|
|
import { EntityTypeSelectComponent } from "@shared/components/entity/entity-type-select.component"; |
|
|
|
import { EntityAutocompleteComponent } from "@shared/components/entity/entity-autocomplete.component"; |
|
|
|
import { EntityService } from "@core/http/entity.service"; |
|
|
|
import { debounceTime, startWith } from "rxjs/operators"; |
|
|
|
import { RelationTypes } from "@shared/models/relation.models"; |
|
|
|
import { StringItemsOption } from "@shared/components/string-items-list.component"; |
|
|
|
import { BaseData } from "@shared/models/base-data"; |
|
|
|
import { CalculatedFieldFormService } from '@core/services/calculated-field-form.service'; |
|
|
|
|
|
|
|
export interface AlarmRuleDialogData { |
|
|
|
value?: CalculatedField; |
|
|
|
@ -67,24 +64,7 @@ export interface AlarmRuleDialogData { |
|
|
|
}) |
|
|
|
export class AlarmRuleDialogComponent extends DialogComponent<AlarmRuleDialogComponent, CalculatedField> { |
|
|
|
|
|
|
|
fieldFormGroup = this.fb.group({ |
|
|
|
name: ['', [Validators.required, Validators.pattern(oneSpaceInsideRegex), Validators.maxLength(255)]], |
|
|
|
type: [CalculatedFieldType.ALARM], |
|
|
|
debugSettings: [], |
|
|
|
entityId: this.fb.group({ |
|
|
|
entityType: this.fb.control<EntityType | AliasEntityType | null>(EntityType.DEVICE_PROFILE, Validators.required), |
|
|
|
id: [null as null | string, Validators.required], |
|
|
|
}), |
|
|
|
configuration: this.fb.group({ |
|
|
|
arguments: this.fb.control({}, Validators.required), |
|
|
|
propagate: [false], |
|
|
|
propagateToOwner: [false], |
|
|
|
propagateToTenant: [false], |
|
|
|
propagateRelationTypes: [null], |
|
|
|
createRules: [null, Validators.required], |
|
|
|
clearRule: [null], |
|
|
|
}), |
|
|
|
}); |
|
|
|
fieldFormGroup: FormGroup ; |
|
|
|
|
|
|
|
additionalDebugActionConfig = this.data.value?.id ? { |
|
|
|
...this.data.additionalDebugActionConfig, |
|
|
|
@ -105,18 +85,15 @@ export class AlarmRuleDialogComponent extends DialogComponent<AlarmRuleDialogCom |
|
|
|
disabledArguments = false; |
|
|
|
isLoading = false; |
|
|
|
|
|
|
|
@ViewChild('entityTypeSelect') entityTypeSelect: EntityTypeSelectComponent; |
|
|
|
@ViewChild('entityAutocompleteComponent') entityAutocompleteComponent: EntityAutocompleteComponent; |
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>, |
|
|
|
protected router: Router, |
|
|
|
@Inject(MAT_DIALOG_DATA) public data: AlarmRuleDialogData, |
|
|
|
protected dialogRef: MatDialogRef<AlarmRuleDialogComponent, CalculatedField>, |
|
|
|
private calculatedFieldsService: CalculatedFieldsService, |
|
|
|
private entityService: EntityService, |
|
|
|
private destroyRef: DestroyRef, |
|
|
|
private fb: FormBuilder) { |
|
|
|
private cfFormService: CalculatedFieldFormService) { |
|
|
|
super(store, router, dialogRef); |
|
|
|
this.fieldFormGroup = this.cfFormService.buildAlarmRuleForm(); |
|
|
|
this.applyDialogData(); |
|
|
|
this.updateRulesValidators(); |
|
|
|
|
|
|
|
@ -132,7 +109,7 @@ export class AlarmRuleDialogComponent extends DialogComponent<AlarmRuleDialogCom |
|
|
|
|
|
|
|
if (!this.data.entityId) { |
|
|
|
combineLatest([ |
|
|
|
this.fieldFormGroup.get('entityId.id')!.valueChanges.pipe(startWith(this.fieldFormGroup.get('entityId.id')!.value)), |
|
|
|
this.fieldFormGroup.get('entityId')!.valueChanges.pipe(startWith(this.fieldFormGroup.get('entityId')!.value)), |
|
|
|
this.fieldFormGroup.get('name')!.valueChanges.pipe(startWith(this.fieldFormGroup.get('name')!.value)) |
|
|
|
]).pipe( |
|
|
|
debounceTime(50), |
|
|
|
@ -196,8 +173,6 @@ export class AlarmRuleDialogComponent extends DialogComponent<AlarmRuleDialogCom |
|
|
|
}); |
|
|
|
} else { |
|
|
|
this.fieldFormGroup.get('name').markAsTouched(); |
|
|
|
this.entityTypeSelect?.markAsTouched(); |
|
|
|
this.entityAutocompleteComponent?.markAsTouched(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -207,18 +182,13 @@ export class AlarmRuleDialogComponent extends DialogComponent<AlarmRuleDialogCom |
|
|
|
} |
|
|
|
|
|
|
|
onTestScript(expression: string): Observable<string> { |
|
|
|
const calculatedFieldId = this.data.value?.id?.id; |
|
|
|
if (calculatedFieldId) { |
|
|
|
return this.calculatedFieldsService.getLatestCalculatedFieldDebugEvent(calculatedFieldId, {ignoreLoading: true}) |
|
|
|
.pipe( |
|
|
|
switchMap(event => { |
|
|
|
const args = event?.arguments ? JSON.parse(event.arguments) : null; |
|
|
|
return this.data.getTestScriptDialogFn(this.fromGroupValue, expression, args, false); |
|
|
|
}), |
|
|
|
takeUntilDestroyed(this.destroyRef) |
|
|
|
) |
|
|
|
} |
|
|
|
return this.data.getTestScriptDialogFn(this.fromGroupValue, expression, null, false); |
|
|
|
return this.cfFormService.testScript( |
|
|
|
this.data.value?.id?.id, |
|
|
|
this.fromGroupValue, |
|
|
|
this.data.getTestScriptDialogFn, |
|
|
|
this.destroyRef, |
|
|
|
expression |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private updateRulesValidators(): void { |
|
|
|
@ -250,5 +220,4 @@ export class AlarmRuleDialogComponent extends DialogComponent<AlarmRuleDialogCom |
|
|
|
changeEntity(entity: BaseData<EntityId>): void { |
|
|
|
this.entityName = entity.name; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|