23 changed files with 828 additions and 182 deletions
@ -0,0 +1,45 @@ |
|||
///
|
|||
/// Copyright © 2016-2025 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { NgModule } from '@angular/core'; |
|||
import { CommonModule } from '@angular/common'; |
|||
import { SharedModule } from '@shared/shared.module'; |
|||
import { |
|||
CalculatedFieldArgumentPanelComponent |
|||
} from '@home/components/calculated-fields/components/calculated-field-arguments/calculated-field-argument-panel.component'; |
|||
import { |
|||
CalculatedFieldArgumentsTableComponent |
|||
} from '@home/components/calculated-fields/components/calculated-field-arguments/calculated-field-arguments-table.component'; |
|||
import { |
|||
PropagateArgumentsTableComponent |
|||
} from '@home/components/calculated-fields/components/calculated-field-arguments/propagate-arguments-table.component'; |
|||
|
|||
@NgModule({ |
|||
imports: [ |
|||
CommonModule, |
|||
SharedModule, |
|||
], |
|||
declarations: [ |
|||
CalculatedFieldArgumentPanelComponent, |
|||
CalculatedFieldArgumentsTableComponent, |
|||
PropagateArgumentsTableComponent |
|||
], |
|||
exports: [ |
|||
CalculatedFieldArgumentsTableComponent, |
|||
PropagateArgumentsTableComponent |
|||
] |
|||
}) |
|||
export class CalculatedFieldArgumentsTableModule {} |
|||
@ -0,0 +1,116 @@ |
|||
///
|
|||
/// Copyright © 2016-2025 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { |
|||
ChangeDetectorRef, |
|||
Component, |
|||
DestroyRef, |
|||
forwardRef, |
|||
OnInit, |
|||
Renderer2, |
|||
ViewContainerRef, |
|||
} from '@angular/core'; |
|||
import { FormBuilder, NG_VALIDATORS, NG_VALUE_ACCESSOR, } from '@angular/forms'; |
|||
import { TbPopoverService } from '@shared/components/popover.service'; |
|||
import { EntityService } from '@core/http/entity.service'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { |
|||
CalculatedFieldArgumentsTableComponent |
|||
} from '@home/components/calculated-fields/components/calculated-field-arguments/calculated-field-arguments-table.component'; |
|||
import { ArgumentEntityType, ArgumentType, CalculatedFieldArgumentValue } from '@shared/models/calculated-field.models'; |
|||
import { isDefined } from '@core/utils'; |
|||
import { NULL_UUID } from '@shared/models/id/has-uuid'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-propagate-arguments-table', |
|||
templateUrl: './calculated-field-arguments-table.component.html', |
|||
styleUrls: [`calculated-field-arguments-table.component.scss`], |
|||
providers: [ |
|||
{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => PropagateArgumentsTableComponent), |
|||
multi: true |
|||
}, |
|||
{ |
|||
provide: NG_VALIDATORS, |
|||
useExisting: forwardRef(() => PropagateArgumentsTableComponent), |
|||
multi: true |
|||
} |
|||
], |
|||
}) |
|||
export class PropagateArgumentsTableComponent extends CalculatedFieldArgumentsTableComponent implements OnInit { |
|||
|
|||
constructor( |
|||
protected fb: FormBuilder, |
|||
protected popoverService: TbPopoverService, |
|||
protected viewContainerRef: ViewContainerRef, |
|||
protected cd: ChangeDetectorRef, |
|||
protected renderer: Renderer2, |
|||
protected entityService: EntityService, |
|||
protected destroyRef: DestroyRef, |
|||
protected store: Store<AppState> |
|||
) { |
|||
super(fb, popoverService, viewContainerRef, cd, renderer, entityService, destroyRef, store) |
|||
} |
|||
|
|||
ngOnInit() { |
|||
this.updatedValue(); |
|||
} |
|||
|
|||
protected changeIsScriptMode(): void { |
|||
this.updatedValue(); |
|||
super.changeIsScriptMode(); |
|||
} |
|||
|
|||
private updatedValue() { |
|||
if (this.isScript) { |
|||
this.argumentNameColumn = 'common.name'; |
|||
this.argumentNameColumnCopy = 'calculated-fields.copy-argument-name'; |
|||
this.displayColumns = ['name', 'entityType', 'target', 'type', 'key', 'actions']; |
|||
this.panelAdditionalCtx = null; |
|||
} else { |
|||
this.argumentNameColumn = 'calculated-fields.output-key'; |
|||
this.argumentNameColumnCopy = 'calculated-fields.copy-output-key'; |
|||
this.displayColumns = ['name', 'type', 'key', 'actions']; |
|||
this.panelAdditionalCtx = { |
|||
argumentEntityTypes: [ArgumentEntityType.Current], |
|||
isOutputKey: true |
|||
}; |
|||
} |
|||
} |
|||
|
|||
protected isEditButtonShowBadge(argument: CalculatedFieldArgumentValue): boolean { |
|||
if (!this.isScript && isDefined(argument?.refEntityId)) { |
|||
return false; |
|||
} |
|||
return super.isEditButtonShowBadge(argument); |
|||
} |
|||
|
|||
protected updateErrorText(): void { |
|||
if (!this.isScript && this.argumentsFormArray.controls.some(control => isDefined(control.value?.refEntityId))) { |
|||
this.errorText = 'calculated-fields.hint.arguments-propagate-argument-entity-type'; |
|||
} else if (!this.isScript && this.argumentsFormArray.controls.some(control => control.value.refEntityKey.type === ArgumentType.Rolling)) { |
|||
this.errorText = 'calculated-fields.hint.arguments-propagate-arguments-with-rolling'; |
|||
} else if (this.argumentsFormArray.controls.some(control => control.value.refEntityId?.id === NULL_UUID)) { |
|||
this.errorText = 'calculated-fields.hint.arguments-entity-not-found'; |
|||
} else if (!this.argumentsFormArray.controls.length) { |
|||
this.errorText = 'calculated-fields.hint.arguments-empty'; |
|||
} else { |
|||
this.errorText = ''; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,99 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2025 The Thingsboard Authors |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
|||
|
|||
--> |
|||
<div [formGroup]="propagateConfiguration" class="tb-form-panel no-border no-padding"> |
|||
<div class="tb-form-panel"> |
|||
<div class="tb-form-panel-title" tbTruncateWithTooltip tb-hint-tooltip-icon="{{ 'calculated-fields.hint.propagation-path-related-entities' | translate }}"> |
|||
{{ 'calculated-fields.propagation-path-related-entities' | translate }} |
|||
</div> |
|||
<div class="flex gap-3 xs:flex-col"> |
|||
<mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic" hideRequiredMarker> |
|||
<mat-label>{{ 'calculated-fields.direction' | translate }}</mat-label> |
|||
<mat-select formControlName="direction"> |
|||
@for (direction of Directions; track direction) { |
|||
<mat-option [value]="direction">{{ PropagationDirectionTranslations.get(direction) | translate }}</mat-option> |
|||
} |
|||
</mat-select> |
|||
</mat-form-field> |
|||
<tb-string-autocomplete [fetchOptionsFn]="fetchOptions.bind(this)" |
|||
class="flex-1" |
|||
panelWidth="" |
|||
additionalClass="" |
|||
required |
|||
[label]="'calculated-fields.relation-type' | translate" |
|||
[errorText]="'calculated-fields.hint.relation-type-required' | translate" |
|||
formControlName="relationType"> |
|||
</tb-string-autocomplete> |
|||
</div> |
|||
</div> |
|||
<div class="tb-form-panel"> |
|||
<div class="flex flex-row items-center justify-between xs:flex-col xs:items-start xs:gap-3"> |
|||
<div class="tb-form-panel-title" tb-hint-tooltip-icon="{{ 'calculated-fields.hint.data-propagate' | translate }}"> |
|||
{{ 'calculated-fields.data-propagate' | translate }} |
|||
</div> |
|||
<tb-toggle-select formControlName="applyExpressionToResolvedArguments"> |
|||
<tb-toggle-option [value]="false">{{ 'calculated-fields.propagate-type.arguments-only' | translate }}</tb-toggle-option> |
|||
<tb-toggle-option [value]="true">{{ 'calculated-fields.propagate-type.expression-result' | translate }}</tb-toggle-option> |
|||
</tb-toggle-select> |
|||
</div> |
|||
<tb-propagate-arguments-table formControlName="arguments" |
|||
[entityId]="entityId" |
|||
[tenantId]="tenantId" |
|||
[entityName]="entityName" |
|||
[isScript]="this.propagateConfiguration.get('applyExpressionToResolvedArguments').value"/> |
|||
</div> |
|||
<div class="tb-form-panel no-gap" [class.!hidden]="!this.propagateConfiguration.get('applyExpressionToResolvedArguments').value"> |
|||
<div class="tb-form-panel-title tb-required"> |
|||
{{ 'calculated-fields.expression' | translate }} |
|||
</div> |
|||
<div> |
|||
<tb-js-func required |
|||
formControlName="expression" |
|||
functionName="calculate" |
|||
[functionArgs]="functionArgs$ | async" |
|||
[disableUndefinedCheck]="true" |
|||
[scriptLanguage]="ScriptLanguage.TBEL" |
|||
[highlightRules]="argumentsHighlightRules$ | async" |
|||
[editorCompleter]="argumentsEditorCompleter$ | async" |
|||
[helpPopupStyle]="{ width: '1200px' }" |
|||
helpId="calculated-field/expression_fn"> |
|||
<div toolbarPrefixButton |
|||
class="tb-primary-background tbel-script-lang-chip">{{ 'api-usage.tbel' | translate }} |
|||
</div> |
|||
<button toolbarSuffixButton |
|||
mat-icon-button |
|||
matTooltip="{{ 'calculated-fields.test-expression-function' | translate }}" |
|||
matTooltipPosition="above" |
|||
class="tb-mat-32" |
|||
[disabled]="propagateConfiguration.get('arguments').invalid" |
|||
(click)="onTestScript()"> |
|||
<mat-icon class="material-icons" color="primary">bug_report</mat-icon> |
|||
</button> |
|||
</tb-js-func> |
|||
<div> |
|||
<button mat-button mat-raised-button color="primary" |
|||
type="button" |
|||
(click)="onTestScript()" |
|||
[disabled]="propagateConfiguration.get('arguments').invalid"> |
|||
{{ 'calculated-fields.test-expression-function' | translate }} |
|||
</button> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
<tb-calculate-field-output formControlName="output" [entityId]="entityId"> |
|||
</tb-calculate-field-output> |
|||
</div> |
|||
@ -0,0 +1,174 @@ |
|||
///
|
|||
/// Copyright © 2016-2025 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { Component, forwardRef, Input } from '@angular/core'; |
|||
import { |
|||
ControlValueAccessor, |
|||
FormBuilder, |
|||
NG_VALIDATORS, |
|||
NG_VALUE_ACCESSOR, |
|||
ValidationErrors, |
|||
Validator, |
|||
Validators |
|||
} from '@angular/forms'; |
|||
import { EntityId } from '@shared/models/id/entity-id'; |
|||
import { Observable, of } from 'rxjs'; |
|||
import { |
|||
calculatedFieldDefaultScript, |
|||
CalculatedFieldOutput, |
|||
CalculatedFieldPropagationConfiguration, |
|||
CalculatedFieldType, |
|||
getCalculatedFieldArgumentsEditorCompleter, |
|||
getCalculatedFieldArgumentsHighlights, |
|||
OutputType, |
|||
PropagationDirectionTranslations, |
|||
PropagationWithExpression |
|||
} from '@shared/models/calculated-field.models'; |
|||
import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; |
|||
import { map } from 'rxjs/operators'; |
|||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
|||
import { ScriptLanguage } from '@app/shared/models/rule-node.models'; |
|||
import { EntitySearchDirection } from '@shared/models/relation.models'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-propagation-configuration', |
|||
templateUrl: './propagation-configuration.component.html', |
|||
providers: [ |
|||
{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => PropagationConfigurationComponent), |
|||
multi: true |
|||
}, |
|||
{ |
|||
provide: NG_VALIDATORS, |
|||
useExisting: forwardRef(() => PropagationConfigurationComponent), |
|||
multi: true |
|||
} |
|||
], |
|||
}) |
|||
export class PropagationConfigurationComponent implements ControlValueAccessor, Validator { |
|||
|
|||
@Input({required: true}) |
|||
entityId: EntityId; |
|||
|
|||
@Input({required: true}) |
|||
tenantId: string; |
|||
|
|||
@Input({required: true}) |
|||
entityName: string; |
|||
|
|||
@Input({required: true}) |
|||
testScript: () => Observable<string>; |
|||
|
|||
propagateConfiguration = this.fb.group({ |
|||
arguments: this.fb.control({}), |
|||
applyExpressionToResolvedArguments: [false], |
|||
direction: [EntitySearchDirection.TO, Validators.required], |
|||
relationType: ['Contains', Validators.required], |
|||
expression: [calculatedFieldDefaultScript], |
|||
output: this.fb.control<CalculatedFieldOutput>({ |
|||
scope: AttributeScope.SERVER_SCOPE, |
|||
type: OutputType.Timeseries, |
|||
}), |
|||
}); |
|||
|
|||
readonly ScriptLanguage = ScriptLanguage; |
|||
readonly CalculatedFieldType = CalculatedFieldType; |
|||
readonly OutputType = OutputType; |
|||
readonly Directions = Object.values(EntitySearchDirection) as Array<EntitySearchDirection>; |
|||
readonly PropagationDirectionTranslations = PropagationDirectionTranslations; |
|||
|
|||
functionArgs$ = this.propagateConfiguration.get('arguments').valueChanges.pipe( |
|||
map(argumentsObj => ['ctx', ...Object.keys(argumentsObj)]) |
|||
); |
|||
|
|||
argumentsEditorCompleter$ = this.propagateConfiguration.get('arguments').valueChanges.pipe( |
|||
map(argumentsObj => getCalculatedFieldArgumentsEditorCompleter(argumentsObj ?? {})) |
|||
); |
|||
|
|||
argumentsHighlightRules$ = this.propagateConfiguration.get('arguments').valueChanges.pipe( |
|||
map(argumentsObj => getCalculatedFieldArgumentsHighlights(argumentsObj)) |
|||
); |
|||
|
|||
private propagateChange: (config: CalculatedFieldPropagationConfiguration) => void = () => { }; |
|||
|
|||
constructor(private fb: FormBuilder) { |
|||
this.propagateConfiguration.get('applyExpressionToResolvedArguments').valueChanges.pipe( |
|||
takeUntilDestroyed() |
|||
).subscribe(() => { |
|||
this.updatedFormWithScript(); |
|||
}) |
|||
|
|||
this.propagateConfiguration.valueChanges.pipe( |
|||
takeUntilDestroyed() |
|||
).subscribe((value: CalculatedFieldPropagationConfiguration) => { |
|||
this.updatedModel(value); |
|||
}) |
|||
} |
|||
|
|||
validate(): ValidationErrors | null { |
|||
return this.propagateConfiguration.valid || this.propagateConfiguration.status === "DISABLED" ? null : {invalidPropagateConfig: false}; |
|||
} |
|||
|
|||
writeValue(value: PropagationWithExpression): void { |
|||
value.expression = value.expression ?? calculatedFieldDefaultScript; |
|||
this.propagateConfiguration.patchValue(value, {emitEvent: false}); |
|||
this.updatedFormWithScript(); |
|||
setTimeout(() => { |
|||
this.propagateConfiguration.get('arguments').updateValueAndValidity({onlySelf: true}); |
|||
}); |
|||
} |
|||
|
|||
registerOnChange(fn: (config: CalculatedFieldPropagationConfiguration) => void): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(_: any): void { } |
|||
|
|||
setDisabledState(isDisabled: boolean): void { |
|||
if (isDisabled) { |
|||
this.propagateConfiguration.disable({emitEvent: false}); |
|||
} else { |
|||
this.propagateConfiguration.enable({emitEvent: false}); |
|||
this.updatedFormWithScript(); |
|||
} |
|||
} |
|||
|
|||
onTestScript() { |
|||
this.testScript().subscribe((expression) => { |
|||
this.propagateConfiguration.get('expression').setValue(expression); |
|||
this.propagateConfiguration.get('expression').markAsDirty(); |
|||
}) |
|||
} |
|||
|
|||
fetchOptions(searchText: string): Observable<Array<string>> { |
|||
const search = searchText ? searchText?.toLowerCase() : ''; |
|||
return of(['Contains', 'Manages']).pipe(map(name => name?.filter(option => option.toLowerCase().includes(search)))); |
|||
} |
|||
|
|||
private updatedModel(value: CalculatedFieldPropagationConfiguration): void { |
|||
value.type = CalculatedFieldType.PROPAGATION; |
|||
this.propagateChange(value); |
|||
} |
|||
|
|||
private updatedFormWithScript() { |
|||
if (this.propagateConfiguration.get('applyExpressionToResolvedArguments').value) { |
|||
this.propagateConfiguration.get('expression').enable({emitEvent: false}); |
|||
} else { |
|||
this.propagateConfiguration.get('expression').disable({emitEvent: false}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
///
|
|||
/// Copyright © 2016-2025 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { NgModule } from '@angular/core'; |
|||
import { CommonModule } from '@angular/common'; |
|||
import { SharedModule } from '@shared/shared.module'; |
|||
import { |
|||
CalculatedFieldOutputModule |
|||
} from '@home/components/calculated-fields/components/output/calculated-field-output.module'; |
|||
import { |
|||
CalculatedFieldArgumentsTableModule |
|||
} from '@home/components/calculated-fields/components/calculated-field-arguments/calculated-field-arguments-table.module'; |
|||
import { |
|||
PropagationConfigurationComponent |
|||
} from '@home/components/calculated-fields/components/propagation-configuration/propagation-configuration.component'; |
|||
|
|||
@NgModule({ |
|||
imports: [ |
|||
CommonModule, |
|||
SharedModule, |
|||
CalculatedFieldOutputModule, |
|||
CalculatedFieldArgumentsTableModule, |
|||
], |
|||
declarations: [ |
|||
PropagationConfigurationComponent, |
|||
], |
|||
exports: [ |
|||
PropagationConfigurationComponent, |
|||
] |
|||
}) |
|||
export class PropagationConfigurationModule { } |
|||
Loading…
Reference in new issue