Browse Source

Merge branch 'lts-4.3' into lts-4.4

pull/16372/head
Vladyslav_Prykhodko 2 months ago
parent
commit
073cb37c0e
  1. 4
      application/src/main/java/org/thingsboard/server/actors/tenant/TenantActor.java
  2. 4
      application/src/main/java/org/thingsboard/server/service/cf/ctx/state/alarm/AlarmRuleState.java
  3. 21
      ui-ngx/src/app/core/services/calculated-field-form.service.ts
  4. 54
      ui-ngx/src/app/modules/home/components/alarm-rules/cf-alarm-rule-condition.component.ts
  5. 41
      ui-ngx/src/app/modules/home/components/alarm-rules/cf-alarm-rule.component.ts
  6. 8
      ui-ngx/src/app/modules/home/components/alarm-rules/create-cf-alarm-rules.component.ts
  7. 17
      ui-ngx/src/app/shared/models/alarm-rule.models.ts

4
application/src/main/java/org/thingsboard/server/actors/tenant/TenantActor.java

@ -199,8 +199,8 @@ public class TenantActor extends RuleChainManagerActor {
private void forwardToCfActor(TenantAwareMsg msg, boolean priority) {
if (cfActor == null) {
if (msg instanceof CalculatedFieldStateRestoreMsg) {
log.warn("[{}] CF Actor is not initialized. ToCalculatedFieldSystemMsg: [{}]", tenantId, msg);
if (msg instanceof CalculatedFieldStateRestoreMsg restoreMsg) {
log.warn("[{}] CF Actor is not initialized. Skipping CF state restore: [{}]", tenantId, restoreMsg.getId());
} else {
log.debug("[{}] CF Actor is not initialized. ToCalculatedFieldSystemMsg: [{}]", tenantId, msg);
}

4
application/src/main/java/org/thingsboard/server/service/cf/ctx/state/alarm/AlarmRuleState.java

@ -4,6 +4,8 @@ package org.thingsboard.server.service.cf.ctx.state.alarm;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.common.util.KvUtil;
@ -38,6 +40,8 @@ public class AlarmRuleState {
private final AlarmSeverity severity;
private AlarmRule alarmRule;
@ToString.Exclude
@EqualsAndHashCode.Exclude
private AlarmCalculatedFieldState state;
private AlarmCondition condition;

21
ui-ngx/src/app/core/services/calculated-field-form.service.ts

@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
import { DestroyRef, inject, Injectable } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AbstractControl, FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { pairwise, switchMap } from 'rxjs/operators';
import { Observable } from 'rxjs';
import {
@ -15,6 +15,7 @@ import {
import { isDefined } from '@core/utils';
import { CalculatedFieldsService } from '@core/http/calculated-fields.service';
import { CalculatedFieldsTableEntity } from '@home/components/calculated-fields/calculated-fields-table-config';
import { AlarmRule, areAlarmRuleArgumentsValid } from '@shared/models/alarm-rule.models';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { getTimezoneInfo } from '@shared/models/time/time.models';
@ -49,10 +50,26 @@ export class CalculatedFieldFormService {
propagateRelationTypes: [null],
createRules: [null, Validators.required],
clearRule: [null],
}),
}, {validators: this.alarmRulesArgumentsValidator()}),
});
}
private alarmRulesArgumentsValidator(): ValidatorFn {
return (configuration: AbstractControl): ValidationErrors | null => {
const args = configuration.get('arguments').value;
const createRulesControl = configuration.get('createRules');
const clearRuleControl = configuration.get('clearRule');
const rules: AlarmRule[] = [];
if (createRulesControl.enabled) {
rules.push(...Object.values<AlarmRule>(createRulesControl.value ?? {}));
}
if (clearRuleControl.enabled) {
rules.push(clearRuleControl.value);
}
return rules.every(rule => areAlarmRuleArgumentsValid(rule, args)) ? null : {alarmRuleArguments: {valid: false}};
};
}
setupTypeChange(form: FormGroup, destroyRef: DestroyRef, isEditActive?: () => boolean): void {
form.get('type').valueChanges.pipe(
pairwise(),

54
ui-ngx/src/app/modules/home/components/alarm-rules/cf-alarm-rule-condition.component.ts

@ -29,7 +29,8 @@ import {
AlarmRuleExpressionType,
AlarmRuleSchedule,
AlarmRuleScheduleType,
checkPredicates
areAlarmRuleConditionFilterArgumentsValid,
isAlarmRuleConditionScheduleArgumentValid
} from "@shared/models/alarm-rule.models";
import { CalculatedFieldArgument } from "@shared/models/calculated-field.models";
import {
@ -93,7 +94,6 @@ export class CfAlarmRuleConditionComponent implements ControlValueAccessor, Vali
private modelValue: AlarmRuleCondition;
private propagateChange = (v: any) => { };
private onValidatorChange = () => { };
constructor(private dialog: MatDialog,
private fb: FormBuilder,
@ -108,10 +108,6 @@ export class CfAlarmRuleConditionComponent implements ControlValueAccessor, Vali
registerOnTouched(fn: any): void {
}
registerOnValidatorChange(fn: () => void): void {
this.onValidatorChange = fn;
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
if (this.disabled) {
@ -129,46 +125,18 @@ export class CfAlarmRuleConditionComponent implements ControlValueAccessor, Vali
ngOnChanges(changes: SimpleChanges) {
if (changes.arguments && !changes.arguments.firstChange) {
if (this.recalculateArgumentValidity()) {
this.onValidatorChange();
}
this.recalculateArgumentValidity();
}
}
private recalculateArgumentValidity(): boolean {
const prevFiltersValid = this.filtersArgumentsValid;
const prevSchedulerValid = this.schedulerArgumentsValid;
private recalculateArgumentValidity(): void {
if (!this.modelValue || !this.arguments) {
this.filtersArgumentsValid = true;
this.schedulerArgumentsValid = true;
} else {
this.filtersArgumentsValid = this.areFilterAndPredicateArgumentsValid(this.modelValue, this.arguments);
this.schedulerArgumentsValid = this.isScheduleArgumentValid(this.modelValue, Object.keys(this.arguments));
this.filtersArgumentsValid = areAlarmRuleConditionFilterArgumentsValid(this.modelValue, this.arguments);
this.schedulerArgumentsValid = isAlarmRuleConditionScheduleArgumentValid(this.modelValue, this.arguments);
}
return prevFiltersValid !== this.filtersArgumentsValid || prevSchedulerValid !== this.schedulerArgumentsValid;
}
private isScheduleArgumentValid(obj: any, validArguments: string[]): boolean {
const arg = obj?.schedule?.dynamicValueArgument;
return !arg || validArguments.includes(arg);
}
private areFilterAndPredicateArgumentsValid(obj: any, args: Record<string, CalculatedFieldArgument>): boolean {
const validSet = new Set(Object.keys(args));
const filters = obj?.expression?.filters || obj?.filters || [];
for (const filter of filters) {
if (filter.argument && !validSet.has(filter.argument)) {
return false;
}
}
for (const filter of filters) {
if (Array.isArray(filter.predicates)) {
if (!checkPredicates(filter.predicates, validSet)) {
return false;
}
}
}
return true;
}
public conditionSet(): boolean {
@ -176,9 +144,7 @@ export class CfAlarmRuleConditionComponent implements ControlValueAccessor, Vali
}
public validate(control: AbstractControl): ValidationErrors | null {
const hasCondition = this.conditionSet();
const argsValid = this.filtersArgumentsValid && this.schedulerArgumentsValid;
return (hasCondition && argsValid) ? null : { alarmRuleCondition: { valid: false } };
return this.conditionSet() ? null : { alarmRuleCondition: { valid: false } };
}
public openFilterDialog($event: Event) {
@ -266,7 +232,6 @@ export class CfAlarmRuleConditionComponent implements ControlValueAccessor, Vali
this.updateConditionInfo();
this.recalculateArgumentValidity();
this.propagateChange(this.modelValue);
this.onValidatorChange();
this.cd.detectChanges();
}
@ -285,10 +250,7 @@ export class CfAlarmRuleConditionComponent implements ControlValueAccessor, Vali
}
}).afterClosed().subscribe((result) => {
if (result) {
if (!this.modelValue) {
this.modelValue = {} as AlarmRuleCondition;
}
this.modelValue.schedule = result;
this.modelValue = {...this.modelValue, schedule: result} as AlarmRuleCondition;
this.updateModel();
}
});

41
ui-ngx/src/app/modules/home/components/alarm-rules/cf-alarm-rule.component.ts

@ -65,12 +65,11 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
alarmRuleFormGroup = this.fb.group({
condition: this.fb.control<AlarmRuleCondition | null>(null, Validators.required),
alarmDetails: [null],
dashboardId: [null]
alarmDetails: this.fb.control<string | null>(null),
dashboardId: this.fb.control<string | null>(null)
});
private propagateChange = (v: any) => { };
private onValidatorChange = () => { };
constructor(private dialog: MatDialog,
private fb: FormBuilder,
@ -84,20 +83,11 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
registerOnTouched(fn: any): void {
}
registerOnValidatorChange(fn: () => void): void {
this.onValidatorChange = fn;
}
ngOnInit() {
this.alarmRuleFormGroup.valueChanges.pipe(
takeUntilDestroyed(this.destroyRef)
).subscribe(() => {
this.updateModel();
});
this.alarmRuleFormGroup.statusChanges.pipe(
takeUntilDestroyed(this.destroyRef)
).subscribe(() => {
this.onValidatorChange();
).subscribe((value) => {
this.updateModel(value);
});
}
@ -111,12 +101,14 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
}
writeValue(value: AlarmRule): void {
this.modelValue = value;
const model = this.modelValue ? {
...this.modelValue,
dashboardId: this.modelValue.dashboardId?.id
} : null;
this.alarmRuleFormGroup.patchValue(model, {emitEvent: false});
this.alarmRuleFormGroup.reset(undefined, {emitEvent: false});
if (value) {
this.alarmRuleFormGroup.patchValue({
...value,
dashboardId: value.dashboardId?.id
}, {emitEvent: false});
}
this.modelValue = value ? this.toModel(this.alarmRuleFormGroup.value) : null;
}
public openEditDetailsDialog($event: Event) {
@ -146,9 +138,12 @@ export class CfAlarmRuleComponent implements ControlValueAccessor, OnInit, Valid
};
}
private updateModel() {
const value = this.alarmRuleFormGroup.value;
this.modelValue = {...value, dashboardId: value.dashboardId ? new DashboardId(value.dashboardId) : null} as AlarmRule;
private updateModel(value: typeof this.alarmRuleFormGroup.value): void {
this.modelValue = this.toModel(value);
this.propagateChange(this.modelValue);
}
private toModel(value: typeof this.alarmRuleFormGroup.value): AlarmRule {
return {...value, dashboardId: value.dashboardId ? new DashboardId(value.dashboardId) : null} as AlarmRule;
}
}

8
ui-ngx/src/app/modules/home/components/alarm-rules/create-cf-alarm-rules.component.ts

@ -63,16 +63,12 @@ export class CreateCfAlarmRulesComponent implements ControlValueAccessor, Valida
private usedSeverities: AlarmSeverity[] = [];
private propagateChange = (v: any) => { };
private onValidatorChange = () => { };
constructor(private fb: FormBuilder,
private destroyRef: DestroyRef) {
this.createAlarmRulesFormGroup.valueChanges.pipe(
takeUntilDestroyed(this.destroyRef)
).subscribe(() => this.updateModel());
this.createAlarmRulesFormGroup.statusChanges.pipe(
takeUntilDestroyed(this.destroyRef)
).subscribe(() => this.onValidatorChange());
}
registerOnChange(fn: any): void {
@ -82,10 +78,6 @@ export class CreateCfAlarmRulesComponent implements ControlValueAccessor, Valida
registerOnTouched(fn: any): void {
}
registerOnValidatorChange(fn: () => void): void {
this.onValidatorChange = fn;
}
createAlarmRulesFormArray(): UntypedFormArray {
return this.createAlarmRulesFormGroup.get('createAlarmRules') as UntypedFormArray;
}

17
ui-ngx/src/app/shared/models/alarm-rule.models.ts

@ -330,6 +330,23 @@ export function areFiltersAndPredicateArgumentsValid(obj: any, args: Record<stri
return true;
}
export function areAlarmRuleConditionFilterArgumentsValid(condition: AlarmRuleCondition,
args: Record<string, CalculatedFieldArgument>): boolean {
return areFiltersAndPredicateArgumentsValid(condition?.expression?.filters, args);
}
export function isAlarmRuleConditionScheduleArgumentValid(condition: AlarmRuleCondition,
args: Record<string, CalculatedFieldArgument>): boolean {
const arg = condition?.schedule?.dynamicValueArgument;
return !arg || Object.keys(args).includes(arg);
}
export function areAlarmRuleArgumentsValid(alarmRule: AlarmRule, args: Record<string, CalculatedFieldArgument>): boolean {
const condition = alarmRule?.condition;
return !condition || !args ||
(areAlarmRuleConditionFilterArgumentsValid(condition, args) && isAlarmRuleConditionScheduleArgumentValid(condition, args));
}
export function isPredicateArgumentsValid(predicates: any, args: Record<string, CalculatedFieldArgument>): boolean {
const validSet = new Set(Object.keys(args));
if (Array.isArray(predicates)) {

Loading…
Cancel
Save