From 95878afcfb22102d3c90db196a40739662869176 Mon Sep 17 00:00:00 2001 From: rusikv Date: Tue, 27 Jun 2023 17:30:56 +0300 Subject: [PATCH 1/5] Test rule node script function with selected event implementation --- .../script/node-script-test.service.ts | 52 ++++++++----------- .../components/details-panel.component.ts | 10 +++- .../components/event/event-table-config.ts | 26 ++++++++-- .../components/event/event-table.component.ts | 34 ++++++++++-- .../rulechain/rule-node-config.component.ts | 8 +++ .../rule-node-details.component.html | 3 +- .../rulechain/rule-node-details.component.ts | 4 ++ .../rulechain/rulechain-page.component.html | 13 +++-- .../rulechain/rulechain-page.component.ts | 16 +++++- .../src/app/shared/models/rule-node.models.ts | 35 ++++++++++--- .../assets/locale/locale.constant-en_US.json | 11 +++- 11 files changed, 160 insertions(+), 52 deletions(-) diff --git a/ui-ngx/src/app/core/services/script/node-script-test.service.ts b/ui-ngx/src/app/core/services/script/node-script-test.service.ts index 1a33275111..c3e0da73ad 100644 --- a/ui-ngx/src/app/core/services/script/node-script-test.service.ts +++ b/ui-ngx/src/app/core/services/script/node-script-test.service.ts @@ -23,8 +23,8 @@ import { NodeScriptTestDialogComponent, NodeScriptTestDialogData } from '@shared/components/dialog/node-script-test-dialog.component'; -import { sortObjectKeys } from '@core/utils'; import { ScriptLanguage } from '@shared/models/rule-node.models'; +import { DebugRuleNodeEventBody } from '@shared/models/event.models'; @Injectable({ providedIn: 'root' @@ -37,56 +37,50 @@ export class NodeScriptTestService { testNodeScript(script: string, scriptType: string, functionTitle: string, functionName: string, argNames: string[], ruleNodeId: string, helpId?: string, - scriptLang?: ScriptLanguage): Observable { - if (ruleNodeId) { + scriptLang?: ScriptLanguage, debugEventBody?: DebugRuleNodeEventBody): Observable { + if (ruleNodeId && !debugEventBody) { return this.ruleChainService.getLatestRuleNodeDebugInput(ruleNodeId).pipe( switchMap((debugIn) => { - let msg: any; - let metadata: {[key: string]: string}; - let msgType: string; - if (debugIn) { - if (debugIn.data) { - try { - msg = JSON.parse(debugIn.data); - } catch (e) {} - } - if (debugIn.metadata) { - try { - metadata = JSON.parse(debugIn.metadata); - } catch (e) {} - } - msgType = debugIn.msgType; - } return this.openTestScriptDialog(script, scriptType, functionTitle, - functionName, argNames, msg, metadata, msgType, helpId, scriptLang); + functionName, argNames, debugIn, helpId, scriptLang); }) ); } else { return this.openTestScriptDialog(script, scriptType, functionTitle, - functionName, argNames, null, null, null, helpId, scriptLang); + functionName, argNames, debugEventBody, helpId, scriptLang); } } - private openTestScriptDialog(script: string, scriptType: string, - functionTitle: string, functionName: string, argNames: string[], - msg?: any, metadata?: {[key: string]: string}, msgType?: string, helpId?: string, + private openTestScriptDialog(script: string, scriptType: string, functionTitle: string, functionName: string, + argNames: string[], eventBody: DebugRuleNodeEventBody, helpId?: string, scriptLang?: ScriptLanguage): Observable { - if (!msg) { + let msg: any; + let metadata: {[key: string]: string}; + let msgType: string; + if (eventBody && eventBody.data) { + try { + msg = JSON.parse(eventBody.data); + } catch (e) {} + } else { msg = { temperature: 22.4, humidity: 78 }; } - if (!metadata) { + if (eventBody && eventBody.metadata) { + try { + metadata = JSON.parse(eventBody.metadata); + } catch (e) {} + } else { metadata = { deviceName: 'Test Device', deviceType: 'default', ts: new Date().getTime() + '' }; - } else { - metadata = sortObjectKeys(metadata); } - if (!msgType) { + if (eventBody && eventBody.msgType) { + msgType = eventBody.msgType; + } else { msgType = 'POST_TELEMETRY_REQUEST'; } return this.dialog.open(NodeScriptTestDialogComponent, diff --git a/ui-ngx/src/app/modules/home/components/details-panel.component.ts b/ui-ngx/src/app/modules/home/components/details-panel.component.ts index b2f8a059f3..ac8d6e3bc3 100644 --- a/ui-ngx/src/app/modules/home/components/details-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/details-panel.component.ts @@ -15,7 +15,6 @@ /// import { - ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, @@ -56,7 +55,12 @@ export class DetailsPanelComponent extends PageComponent implements OnDestroy { } this.theFormValue = value; if (this.theFormValue !== null) { - this.formSubscription = this.theFormValue.valueChanges.subscribe(() => this.cd.detectChanges()); + this.formSubscription = this.theFormValue.valueChanges.subscribe(() => { + if (this.isReadOnly) { + this.switchToFirstTab.emit(); + } + this.cd.detectChanges() + }); } } } @@ -73,6 +77,8 @@ export class DetailsPanelComponent extends PageComponent implements OnDestroy { applyDetails = new EventEmitter(); @Output() closeSearch = new EventEmitter(); + @Output() + switchToFirstTab = new EventEmitter() isEditValue = false; showSearchPane = false; diff --git a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts index a9816a130c..51bd99fde0 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts @@ -21,7 +21,7 @@ import { EntityTableColumn, EntityTableConfig } from '@home/models/entity/entities-table-config.models'; -import { DebugEventType, Event, EventType, FilterEventBody } from '@shared/models/event.models'; +import { DebugEventType, DebugRuleNodeEventBody, Event, EventType, FilterEventBody } from '@shared/models/event.models'; import { TimePageLink } from '@shared/models/page/page-link'; import { TranslateService } from '@ngx-translate/core'; import { DatePipe } from '@angular/common'; @@ -30,7 +30,7 @@ import { EntityId } from '@shared/models/id/entity-id'; import { EventService } from '@app/core/http/event.service'; import { EventTableHeaderComponent } from '@home/components/event/event-table-header.component'; import { EntityTypeResource } from '@shared/models/entity-type.models'; -import { Observable } from 'rxjs'; +import { BehaviorSubject, Observable } from 'rxjs'; import { PageData } from '@shared/models/page/page-data'; import { Direction } from '@shared/models/page/sort-order'; import { DialogService } from '@core/services/dialog.service'; @@ -49,6 +49,8 @@ import { EventFilterPanelData, FilterEntityColumn } from '@home/components/event/event-filter-panel.component'; +import { NodeScriptTestService } from '@core/services/script/node-script-test.service'; +import { ruleNodeClazzFunctionNameTranslations } from '@shared/models/rule-node.models'; export class EventTableConfig extends EntityTableConfig { @@ -72,6 +74,8 @@ export class EventTableConfig extends EntityTableConfig { eventTypes: Array; + debugEventSelectedSubject = new BehaviorSubject(null); + constructor(private eventService: EventService, private dialogService: DialogService, private translate: TranslateService, @@ -84,7 +88,11 @@ export class EventTableConfig extends EntityTableConfig { private debugEventTypes: Array = null, private overlay: Overlay, private viewContainerRef: ViewContainerRef, - private cd: ChangeDetectorRef) { + private cd: ChangeDetectorRef, + private nodeScriptTestService: NodeScriptTestService, + private isRuleNodeDebugModeEnabled: boolean, + private editingRuleNodeHasScript: boolean, + private rulenodeClazz: string) { super(); this.loadDataOnInit = false; this.tableTitle = ''; @@ -317,6 +325,18 @@ export class EventTableConfig extends EntityTableConfig { onAction: ($event, entity) => this.showContent($event, entity.body.error, 'event.error') }, + '48px'), + new EntityActionTableColumn('test', '', + { + name: this.translate.instant('rulenode.test-function', + {function: this.translate.instant(ruleNodeClazzFunctionNameTranslations[this.rulenodeClazz])}), + icon: 'bug_report', + isEnabled: (entity) => this.isRuleNodeDebugModeEnabled && entity.body.type === 'IN' && + this.editingRuleNodeHasScript, + onAction: ($event, entity) => { + this.debugEventSelectedSubject.next(entity.body); + } + }, '48px') ); break; diff --git a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts index fbdd0a07da..4781b4811d 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts @@ -17,10 +17,10 @@ import { AfterViewInit, ChangeDetectorRef, - Component, + Component, EventEmitter, Input, OnDestroy, - OnInit, + OnInit, Output, ViewChild, ViewContainerRef } from '@angular/core'; @@ -32,9 +32,10 @@ import { EntitiesTableComponent } from '@home/components/entity/entities-table.c import { EventTableConfig } from './event-table-config'; import { EventService } from '@core/http/event.service'; import { DialogService } from '@core/services/dialog.service'; -import { DebugEventType, EventType } from '@shared/models/event.models'; +import { DebugEventType, DebugRuleNodeEventBody, EventType } from '@shared/models/event.models'; import { Overlay } from '@angular/cdk/overlay'; import { Subscription } from 'rxjs'; +import { NodeScriptTestService } from '@core/services/script/node-script-test.service'; @Component({ selector: 'tb-event-table', @@ -83,6 +84,18 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { } } + @Input() + isRuleNodeDebugModeEnabled: boolean; + + @Input() + editingRuleNodeHasScript: boolean; + + @Input() + rulenodeClazz: string; + + @Output() + debugEventSelected = new EventEmitter(null); + @ViewChild(EntitiesTableComponent, {static: true}) entitiesTable: EntitiesTableComponent; eventTableConfig: EventTableConfig; @@ -96,7 +109,8 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { private dialog: MatDialog, private overlay: Overlay, private viewContainerRef: ViewContainerRef, - private cd: ChangeDetectorRef) { + private cd: ChangeDetectorRef, + private nodeScriptTestService: NodeScriptTestService) { } ngOnInit() { @@ -114,8 +128,18 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { this.debugEventTypes, this.overlay, this.viewContainerRef, - this.cd + this.cd, + this.nodeScriptTestService, + this.isRuleNodeDebugModeEnabled, + this.editingRuleNodeHasScript, + this.rulenodeClazz ); + + this.eventTableConfig.debugEventSelectedSubject.subscribe((debugEventBody: DebugRuleNodeEventBody) => { + if (debugEventBody) { + this.debugEventSelected.emit(debugEventBody); + } + }) } ngAfterViewInit() { diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts index 73f0d5e14a..788a495607 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts @@ -38,6 +38,7 @@ import { TranslateService } from '@ngx-translate/core'; import { JsonObjectEditComponent } from '@shared/components/json-object-edit.component'; import { deepClone } from '@core/utils'; import { RuleChainType } from '@shared/models/rule-chain.models'; +import { DebugRuleNodeEventBody } from '@shared/models/event.models'; @Component({ selector: 'tb-rule-node-config', @@ -92,6 +93,13 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On return this.nodeDefinitionValue; } + @Input() + set debugEventBody(debugEventBody: DebugRuleNodeEventBody) { + if (debugEventBody) { + this.definedConfigComponent?.testScript(debugEventBody); + } + } + definedDirectiveError: string; ruleNodeConfigFormGroup: UntypedFormGroup; diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html index b2c73a4bdc..c215b0d21d 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html @@ -50,7 +50,8 @@ [ruleNodeId]="ruleNode.ruleNodeId?.id" [ruleChainId]="ruleChainId" [ruleChainType]="ruleChainType" - [nodeDefinition]="ruleNode.component.configurationDescriptor.nodeDefinition"> + [nodeDefinition]="ruleNode.component.configurationDescriptor.nodeDefinition" + [debugEventBody]="debugEventBody">
diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts index dad1e715ec..455e70270c 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts @@ -27,6 +27,7 @@ import { RuleNodeConfigComponent } from './rule-node-config.component'; import { Router } from '@angular/router'; import { RuleChainType } from '@app/shared/models/rule-chain.models'; import { ComponentClusteringMode } from '@shared/models/component-descriptor.models'; +import { DebugRuleNodeEventBody } from '@shared/models/event.models'; @Component({ selector: 'tb-rule-node', @@ -55,6 +56,9 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O @Input() isAdd = false; + @Input() + debugEventBody: DebugRuleNodeEventBody; + ruleNodeType = RuleNodeType; entityType = EntityType; diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html index 4cdaed942d..15695d9d87 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html @@ -100,7 +100,8 @@ (closeDetails)="onEditRuleNodeClosed()" (toggleDetailsEditMode)="onRevertRuleNodeEdit()" (applyDetails)="saveRuleNode()" - [theForm]="tbRuleNode.ruleNodeFormGroup"> + [theForm]="tbRuleNode.ruleNodeFormGroup" + (switchToFirstTab)="onSwitchToFirstTab()">
@@ -111,7 +112,8 @@ [ruleChainId]="ruleChain.id?.id" [ruleChainType]="ruleChainType" [isEdit]="true" - [isReadOnly]="false"> + [isReadOnly]="false" + [debugEventBody]="debugEventBody"> @@ -119,7 +121,12 @@ [defaultEventType]="debugEventTypes.DEBUG_RULE_NODE" [active]="eventsTab.isActive" [tenantId]="ruleChain.tenantId.id" - [entityId]="editingRuleNode.ruleNodeId"> + [entityId]="editingRuleNode.ruleNodeId" + [isRuleNodeDebugModeEnabled]="editingRuleNode?.debugMode" + [editingRuleNodeHasScript]="editingRuleNodeHasScript" + [rulenodeClazz]="editingRuleNode.component.clazz" + (debugEventSelected)="onDebugEventSelected($event)"> +
diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts index d9c6b1b3bb..ff9d9060c8 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts @@ -87,7 +87,7 @@ import { DialogComponent } from '@shared/components/dialog.component'; import { MatMenuTrigger } from '@angular/material/menu'; import { ItemBufferService, RuleNodeConnection } from '@core/services/item-buffer.service'; import { Hotkey } from 'angular2-hotkeys'; -import { DebugEventType, EventType } from '@shared/models/event.models'; +import { DebugEventType, DebugRuleNodeEventBody, EventType } from '@shared/models/event.models'; import { MatMiniFabButton } from '@angular/material/button'; import { TbPopoverService } from '@shared/components/popover.service'; import { VersionControlComponent } from '@home/components/vc/version-control.component'; @@ -153,6 +153,8 @@ export class RuleChainPageComponent extends PageComponent editingRuleNodeAllowCustomLabels = false; editingRuleNodeLinkLabels: {[label: string]: LinkLabel}; editingRuleNodeSourceRuleChainId: string; + debugEventBody: DebugRuleNodeEventBody; + editingRuleNodeHasScript: boolean = false; @ViewChild('tbRuleNode') ruleNodeComponent: RuleNodeDetailsComponent; @ViewChild('tbRuleNodeLink') ruleNodeLinkComponent: RuleNodeLinkComponent; @@ -1110,6 +1112,7 @@ export class RuleChainPageComponent extends PageComponent this.isEditingRuleNode = true; this.editingRuleNodeIndex = this.ruleChainModel.nodes.indexOf(node); this.editingRuleNode = deepClone(node, ['component']); + this.editingRuleNodeHasScript = this.editingRuleNode.configuration.hasOwnProperty('scriptLang'); setTimeout(() => { this.ruleNodeComponent.ruleNodeFormGroup.markAsPristine(); }, 0); @@ -1258,6 +1261,7 @@ export class RuleChainPageComponent extends PageComponent onEditRuleNodeClosed() { this.editingRuleNode = null; this.isEditingRuleNode = false; + this.debugEventBody = null; } onEditRuleNodeLinkClosed() { @@ -1277,6 +1281,16 @@ export class RuleChainPageComponent extends PageComponent this.editingRuleNodeLink = deepClone(edge); } + onDebugEventSelected(debugEventBody: DebugRuleNodeEventBody) { + if (debugEventBody) { + this.debugEventBody = debugEventBody; + } + } + + onSwitchToFirstTab() { + this.selectedRuleNodeTabIndex = 0; + } + saveRuleNode() { this.ruleNodeComponent.validate(); if (this.ruleNodeComponent.ruleNodeFormGroup.valid) { diff --git a/ui-ngx/src/app/shared/models/rule-node.models.ts b/ui-ngx/src/app/shared/models/rule-node.models.ts index d4688546d7..84ea8ddd3e 100644 --- a/ui-ngx/src/app/shared/models/rule-node.models.ts +++ b/ui-ngx/src/app/shared/models/rule-node.models.ts @@ -26,6 +26,7 @@ import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { AbstractControl, UntypedFormGroup } from '@angular/forms'; import { RuleChainType } from '@shared/models/rule-chain.models'; +import { DebugRuleNodeEventBody } from '@shared/models/event.models'; export interface RuleNodeConfiguration { [key: string]: any; @@ -75,6 +76,7 @@ export interface IRuleNodeConfigurationComponent { configuration: RuleNodeConfiguration; configurationChanged: Observable; validate(); + testScript? (debugEventBody: DebugRuleNodeEventBody); [key: string]: any; } @@ -427,13 +429,22 @@ export const messageTypeNames = new Map( export const ruleChainNodeClazz = 'org.thingsboard.rule.engine.flow.TbRuleChainInputNode'; export const outputNodeClazz = 'org.thingsboard.rule.engine.flow.TbRuleChainOutputNode'; +export enum RuleNodeClazz { + TbJsFilterNode = 'org.thingsboard.rule.engine.filter.TbJsFilterNode', + TbLogNode = 'org.thingsboard.rule.engine.action.TbLogNode', + TbJsSwitchNode = 'org.thingsboard.rule.engine.filter.TbJsSwitchNode', + TbClearAlarmNode = 'org.thingsboard.rule.engine.action.TbClearAlarmNode', + TbCreateAlarmNode = 'org.thingsboard.rule.engine.action.TbCreateAlarmNode', + TbTransformMsgNode = 'org.thingsboard.rule.engine.transform.TbTransformMsgNode', + TbMsgGeneratorNode = 'org.thingsboard.rule.engine.debug.TbMsgGeneratorNode' +} const ruleNodeClazzHelpLinkMap = { 'org.thingsboard.rule.engine.filter.TbCheckRelationNode': 'ruleNodeCheckRelation', 'org.thingsboard.rule.engine.filter.TbCheckMessageNode': 'ruleNodeCheckExistenceFields', 'org.thingsboard.rule.engine.geo.TbGpsGeofencingFilterNode': 'ruleNodeGpsGeofencingFilter', - 'org.thingsboard.rule.engine.filter.TbJsFilterNode': 'ruleNodeJsFilter', - 'org.thingsboard.rule.engine.filter.TbJsSwitchNode': 'ruleNodeJsSwitch', + [RuleNodeClazz.TbJsFilterNode]: 'ruleNodeJsFilter', + [RuleNodeClazz.TbJsSwitchNode]: 'ruleNodeJsSwitch', 'org.thingsboard.rule.engine.filter.TbAssetTypeSwitchNode': 'ruleNodeAssetProfileSwitch', 'org.thingsboard.rule.engine.filter.TbDeviceTypeSwitchNode': 'ruleNodeDeviceProfileSwitch', 'org.thingsboard.rule.engine.filter.TbCheckAlarmStatusNode': 'ruleNodeCheckAlarmStatus', @@ -452,18 +463,18 @@ const ruleNodeClazzHelpLinkMap = { 'org.thingsboard.rule.engine.metadata.TbGetTenantDetailsNode': 'ruleNodeTenantDetails', 'org.thingsboard.rule.engine.metadata.CalculateDeltaNode': 'ruleNodeCalculateDelta', 'org.thingsboard.rule.engine.transform.TbChangeOriginatorNode': 'ruleNodeChangeOriginator', - 'org.thingsboard.rule.engine.transform.TbTransformMsgNode': 'ruleNodeTransformMsg', + [RuleNodeClazz.TbTransformMsgNode]: 'ruleNodeTransformMsg', 'org.thingsboard.rule.engine.mail.TbMsgToEmailNode': 'ruleNodeMsgToEmail', 'org.thingsboard.rule.engine.action.TbAssignToCustomerNode': 'ruleNodeAssignToCustomer', 'org.thingsboard.rule.engine.action.TbUnassignFromCustomerNode': 'ruleNodeUnassignFromCustomer', - 'org.thingsboard.rule.engine.action.TbClearAlarmNode': 'ruleNodeClearAlarm', - 'org.thingsboard.rule.engine.action.TbCreateAlarmNode': 'ruleNodeCreateAlarm', + [RuleNodeClazz.TbClearAlarmNode]: 'ruleNodeClearAlarm', + [RuleNodeClazz.TbCreateAlarmNode]: 'ruleNodeCreateAlarm', 'org.thingsboard.rule.engine.action.TbCreateRelationNode': 'ruleNodeCreateRelation', 'org.thingsboard.rule.engine.action.TbDeleteRelationNode': 'ruleNodeDeleteRelation', 'org.thingsboard.rule.engine.delay.TbMsgDelayNode': 'ruleNodeMsgDelay', - 'org.thingsboard.rule.engine.debug.TbMsgGeneratorNode': 'ruleNodeMsgGenerator', + [RuleNodeClazz.TbMsgGeneratorNode]: 'ruleNodeMsgGenerator', 'org.thingsboard.rule.engine.geo.TbGpsGeofencingActionNode': 'ruleNodeGpsGeofencingEvents', - 'org.thingsboard.rule.engine.action.TbLogNode': 'ruleNodeLog', + [RuleNodeClazz.TbLogNode]: 'ruleNodeLog', 'org.thingsboard.rule.engine.rpc.TbSendRPCReplyNode': 'ruleNodeRpcCallReply', 'org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode': 'ruleNodeRpcCallRequest', 'org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode': 'ruleNodeSaveAttributes', @@ -499,3 +510,13 @@ export function getRuleNodeHelpLink(component: RuleNodeComponentDescriptor): str } return 'ruleEngine'; } + +export const ruleNodeClazzFunctionNameTranslations = { + [RuleNodeClazz.TbJsFilterNode]: 'rulenode.function-name.filter', + [RuleNodeClazz.TbLogNode]: 'rulenode.function-name.to-string', + [RuleNodeClazz.TbJsSwitchNode]: 'rulenode.function-name.switch', + [RuleNodeClazz.TbClearAlarmNode]: 'rulenode.function-name.details', + [RuleNodeClazz.TbCreateAlarmNode]: 'rulenode.function-name.details', + [RuleNodeClazz.TbTransformMsgNode]: 'rulenode.function-name.transform', + [RuleNodeClazz.TbMsgGeneratorNode]: 'rulenode.function-name.generate' +} diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 341408a615..af513bbaf6 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -3464,7 +3464,16 @@ "output": "Output", "test": "Test", "help": "Help", - "reset-debug-mode": "Reset debug mode in all nodes" + "reset-debug-mode": "Reset debug mode in all nodes", + "test-function": "Test '{{function}}' function with this message", + "function-name": { + "filter": "Filter", + "to-string": "ToString", + "details": "Details", + "generate": "Generate", + "switch": "Switch", + "transform": "Transform" + } }, "timezone": { "timezone": "Timezone", From 552be228a35ad49ca0d8939a25a12324457cd1e7 Mon Sep 17 00:00:00 2001 From: rusikv Date: Mon, 3 Jul 2023 17:13:11 +0300 Subject: [PATCH 2/5] Refactoring --- .../script/node-script-test.service.ts | 6 ++- .../components/details-panel.component.ts | 5 -- .../entity/entities-table.component.ts | 4 ++ .../components/event/event-table-config.ts | 45 +++++++++-------- .../components/event/event-table.component.ts | 36 ++++++++------ .../entity/entity-table-component.models.ts | 1 + .../rulechain/rule-node-config.component.ts | 25 ++++++---- .../rule-node-details.component.html | 2 +- .../rulechain/rule-node-details.component.ts | 7 ++- .../rulechain/rulechain-page.component.html | 9 ++-- .../rulechain/rulechain-page.component.ts | 27 +++++++---- .../src/app/shared/models/rule-node.models.ts | 48 ++++++++----------- .../assets/locale/locale.constant-en_US.json | 10 +--- 13 files changed, 118 insertions(+), 107 deletions(-) diff --git a/ui-ngx/src/app/core/services/script/node-script-test.service.ts b/ui-ngx/src/app/core/services/script/node-script-test.service.ts index c3e0da73ad..fa30934f06 100644 --- a/ui-ngx/src/app/core/services/script/node-script-test.service.ts +++ b/ui-ngx/src/app/core/services/script/node-script-test.service.ts @@ -61,7 +61,8 @@ export class NodeScriptTestService { try { msg = JSON.parse(eventBody.data); } catch (e) {} - } else { + } + if (!msg) { msg = { temperature: 22.4, humidity: 78 @@ -71,7 +72,8 @@ export class NodeScriptTestService { try { metadata = JSON.parse(eventBody.metadata); } catch (e) {} - } else { + } + if (!metadata) { metadata = { deviceName: 'Test Device', deviceType: 'default', diff --git a/ui-ngx/src/app/modules/home/components/details-panel.component.ts b/ui-ngx/src/app/modules/home/components/details-panel.component.ts index ac8d6e3bc3..66facf08d4 100644 --- a/ui-ngx/src/app/modules/home/components/details-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/details-panel.component.ts @@ -56,9 +56,6 @@ export class DetailsPanelComponent extends PageComponent implements OnDestroy { this.theFormValue = value; if (this.theFormValue !== null) { this.formSubscription = this.theFormValue.valueChanges.subscribe(() => { - if (this.isReadOnly) { - this.switchToFirstTab.emit(); - } this.cd.detectChanges() }); } @@ -77,8 +74,6 @@ export class DetailsPanelComponent extends PageComponent implements OnDestroy { applyDetails = new EventEmitter(); @Output() closeSearch = new EventEmitter(); - @Output() - switchToFirstTab = new EventEmitter() isEditValue = false; showSearchPane = false; diff --git a/ui-ngx/src/app/modules/home/components/entity/entities-table.component.ts b/ui-ngx/src/app/modules/home/components/entity/entities-table.component.ts index b868fd1e47..46a7c966c8 100644 --- a/ui-ngx/src/app/modules/home/components/entity/entities-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/entity/entities-table.component.ts @@ -637,6 +637,10 @@ export class EntitiesTableComponent extends PageComponent implements IEntitiesTa } } + cellActionDescriptorsUpdated() { + this.cellActionDescriptors = [...this.entitiesTableConfig.cellActionDescriptors]; + } + headerCellStyle(column: EntityColumn>) { const index = this.entitiesTableConfig.columns.indexOf(column); let res = this.headerCellStyleCache[index]; diff --git a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts index 51bd99fde0..7382633e33 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts @@ -30,7 +30,7 @@ import { EntityId } from '@shared/models/id/entity-id'; import { EventService } from '@app/core/http/event.service'; import { EventTableHeaderComponent } from '@home/components/event/event-table-header.component'; import { EntityTypeResource } from '@shared/models/entity-type.models'; -import { BehaviorSubject, Observable } from 'rxjs'; +import { Observable } from 'rxjs'; import { PageData } from '@shared/models/page/page-data'; import { Direction } from '@shared/models/page/sort-order'; import { DialogService } from '@core/services/dialog.service'; @@ -41,7 +41,7 @@ import { } from '@home/components/event/event-content-dialog.component'; import { isEqual, sortObjectKeys } from '@core/utils'; import { ConnectedPosition, Overlay, OverlayConfig, OverlayRef } from '@angular/cdk/overlay'; -import { ChangeDetectorRef, Injector, StaticProvider, ViewContainerRef } from '@angular/core'; +import { ChangeDetectorRef, EventEmitter, Injector, StaticProvider, ViewContainerRef } from '@angular/core'; import { ComponentPortal } from '@angular/cdk/portal'; import { EVENT_FILTER_PANEL_DATA, @@ -50,7 +50,6 @@ import { FilterEntityColumn } from '@home/components/event/event-filter-panel.component'; import { NodeScriptTestService } from '@core/services/script/node-script-test.service'; -import { ruleNodeClazzFunctionNameTranslations } from '@shared/models/rule-node.models'; export class EventTableConfig extends EntityTableConfig { @@ -63,6 +62,7 @@ export class EventTableConfig extends EntityTableConfig { set eventType(eventType: EventType | DebugEventType) { if (this.eventTypeValue !== eventType) { this.eventTypeValue = eventType; + this.updateCellAction(); this.updateColumns(true); this.updateFilterColumns(); } @@ -74,8 +74,6 @@ export class EventTableConfig extends EntityTableConfig { eventTypes: Array; - debugEventSelectedSubject = new BehaviorSubject(null); - constructor(private eventService: EventService, private dialogService: DialogService, private translate: TranslateService, @@ -90,9 +88,8 @@ export class EventTableConfig extends EntityTableConfig { private viewContainerRef: ViewContainerRef, private cd: ChangeDetectorRef, private nodeScriptTestService: NodeScriptTestService, - private isRuleNodeDebugModeEnabled: boolean, - private editingRuleNodeHasScript: boolean, - private rulenodeClazz: string) { + public testButtonLabel?: string, + private debugEventSelected?: EventEmitter) { super(); this.loadDataOnInit = false; this.tableTitle = ''; @@ -128,6 +125,7 @@ export class EventTableConfig extends EntityTableConfig { this.defaultSortOrder = {property: 'createdTime', direction: Direction.DESC}; this.updateColumns(); + this.updateCellAction(); this.updateFilterColumns(); this.headerActionDescriptors.push({ @@ -325,18 +323,6 @@ export class EventTableConfig extends EntityTableConfig { onAction: ($event, entity) => this.showContent($event, entity.body.error, 'event.error') }, - '48px'), - new EntityActionTableColumn('test', '', - { - name: this.translate.instant('rulenode.test-function', - {function: this.translate.instant(ruleNodeClazzFunctionNameTranslations[this.rulenodeClazz])}), - icon: 'bug_report', - isEnabled: (entity) => this.isRuleNodeDebugModeEnabled && entity.body.type === 'IN' && - this.editingRuleNodeHasScript, - onAction: ($event, entity) => { - this.debugEventSelectedSubject.next(entity.body); - } - }, '48px') ); break; @@ -369,6 +355,25 @@ export class EventTableConfig extends EntityTableConfig { } } + updateCellAction() { + this.cellActionDescriptors = []; + switch (this.eventType) { + case DebugEventType.DEBUG_RULE_NODE: + if (this.testButtonLabel) { + this.cellActionDescriptors.push({ + name: this.translate.instant('rulenode.test-with-this-message', {test: this.testButtonLabel}), + icon: 'bug_report', + isEnabled: (entity) => entity.body.type === 'IN', + onAction: ($event, entity) => { + this.debugEventSelected.next(entity.body); + } + }); + } + break; + } + this.getTable()?.cellActionDescriptorsUpdated(); + } + showContent($event: MouseEvent, content: string, title: string, contentType: ContentType = null, sortKeys = false): void { if ($event) { $event.stopPropagation(); diff --git a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts index 4781b4811d..80394de979 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts @@ -36,6 +36,7 @@ import { DebugEventType, DebugRuleNodeEventBody, EventType } from '@shared/model import { Overlay } from '@angular/cdk/overlay'; import { Subscription } from 'rxjs'; import { NodeScriptTestService } from '@core/services/script/node-script-test.service'; +import { isNotEmptyStr } from '@core/utils'; @Component({ selector: 'tb-event-table', @@ -60,6 +61,10 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { dirtyValue = false; entityIdValue: EntityId; + get active(): boolean { + return this.activeValue; + } + @Input() set active(active: boolean) { if (this.activeValue !== active) { @@ -84,14 +89,24 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { } } - @Input() - isRuleNodeDebugModeEnabled: boolean; + private ruleNodeTestButtonLabelValue: string; - @Input() - editingRuleNodeHasScript: boolean; + get ruleNodeTestButtonLabel(): string { + return this.ruleNodeTestButtonLabelValue; + } @Input() - rulenodeClazz: string; + set ruleNodeTestButtonLabel(value: string) { + if (isNotEmptyStr(value)) { + this.ruleNodeTestButtonLabelValue = value; + } else { + this.ruleNodeTestButtonLabelValue = ''; + } + if (this.eventTableConfig) { + this.eventTableConfig.testButtonLabel = this.ruleNodeTestButtonLabel; + this.eventTableConfig.updateCellAction(); + } + } @Output() debugEventSelected = new EventEmitter(null); @@ -130,16 +145,9 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { this.viewContainerRef, this.cd, this.nodeScriptTestService, - this.isRuleNodeDebugModeEnabled, - this.editingRuleNodeHasScript, - this.rulenodeClazz + this.ruleNodeTestButtonLabel, + this.debugEventSelected ); - - this.eventTableConfig.debugEventSelectedSubject.subscribe((debugEventBody: DebugRuleNodeEventBody) => { - if (debugEventBody) { - this.debugEventSelected.emit(debugEventBody); - } - }) } ngAfterViewInit() { diff --git a/ui-ngx/src/app/modules/home/models/entity/entity-table-component.models.ts b/ui-ngx/src/app/modules/home/models/entity/entity-table-component.models.ts index 033747f6d3..a6e5ada7bf 100644 --- a/ui-ngx/src/app/modules/home/models/entity/entity-table-component.models.ts +++ b/ui-ngx/src/app/modules/home/models/entity/entity-table-component.models.ts @@ -80,6 +80,7 @@ export interface IEntitiesTableComponent { exitFilterMode(): void; resetSortAndFilter(update?: boolean, preserveTimewindow?: boolean): void; columnsUpdated(resetData?: boolean): void; + cellActionDescriptorsUpdated(): void; headerCellStyle(column: EntityColumn>): any; clearCellCache(col: number, row: number): void; cellContent(entity: BaseData, column: EntityColumn>, row: number): any; diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts index 788a495607..6c19507301 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts @@ -18,14 +18,22 @@ import { AfterViewInit, Component, ComponentRef, + EventEmitter, forwardRef, Input, OnDestroy, OnInit, + Output, ViewChild, ViewContainerRef } from '@angular/core'; -import { ControlValueAccessor, UntypedFormBuilder, UntypedFormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms'; +import { + ControlValueAccessor, + NG_VALUE_ACCESSOR, + UntypedFormBuilder, + UntypedFormGroup, + Validators +} from '@angular/forms'; import { IRuleNodeConfigurationComponent, RuleNodeConfiguration, @@ -38,7 +46,6 @@ import { TranslateService } from '@ngx-translate/core'; import { JsonObjectEditComponent } from '@shared/components/json-object-edit.component'; import { deepClone } from '@core/utils'; import { RuleChainType } from '@shared/models/rule-chain.models'; -import { DebugRuleNodeEventBody } from '@shared/models/event.models'; @Component({ selector: 'tb-rule-node-config', @@ -77,6 +84,9 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On @Input() ruleChainType: RuleChainType; + @Output() + initRuleNode = new EventEmitter(); + nodeDefinitionValue: RuleNodeDefinition; @Input() @@ -86,6 +96,7 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On if (this.nodeDefinitionValue) { this.validateDefinedDirective(); } + setTimeout(() => this.initRuleNode.emit()); } } @@ -93,21 +104,15 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On return this.nodeDefinitionValue; } - @Input() - set debugEventBody(debugEventBody: DebugRuleNodeEventBody) { - if (debugEventBody) { - this.definedConfigComponent?.testScript(debugEventBody); - } - } - definedDirectiveError: string; ruleNodeConfigFormGroup: UntypedFormGroup; changeSubscription: Subscription; + definedConfigComponent: IRuleNodeConfigurationComponent; + private definedConfigComponentRef: ComponentRef; - private definedConfigComponent: IRuleNodeConfigurationComponent; private configuration: RuleNodeConfiguration; diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html index c215b0d21d..2f925ded60 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html @@ -51,7 +51,7 @@ [ruleChainId]="ruleChainId" [ruleChainType]="ruleChainType" [nodeDefinition]="ruleNode.component.configurationDescriptor.nodeDefinition" - [debugEventBody]="debugEventBody"> + (initRuleNode)="initRuleNode.emit($event)">
diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts index 455e70270c..7b0f426c35 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts @@ -14,7 +14,7 @@ /// limitations under the License. /// -import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core'; +import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'; import { PageComponent } from '@shared/components/page.component'; import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; @@ -27,7 +27,6 @@ import { RuleNodeConfigComponent } from './rule-node-config.component'; import { Router } from '@angular/router'; import { RuleChainType } from '@app/shared/models/rule-chain.models'; import { ComponentClusteringMode } from '@shared/models/component-descriptor.models'; -import { DebugRuleNodeEventBody } from '@shared/models/event.models'; @Component({ selector: 'tb-rule-node', @@ -56,8 +55,8 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O @Input() isAdd = false; - @Input() - debugEventBody: DebugRuleNodeEventBody; + @Output() + initRuleNode = new EventEmitter(); ruleNodeType = RuleNodeType; entityType = EntityType; diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html index 15695d9d87..5e20a6a8e2 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html @@ -100,8 +100,7 @@ (closeDetails)="onEditRuleNodeClosed()" (toggleDetailsEditMode)="onRevertRuleNodeEdit()" (applyDetails)="saveRuleNode()" - [theForm]="tbRuleNode.ruleNodeFormGroup" - (switchToFirstTab)="onSwitchToFirstTab()"> + [theForm]="tbRuleNode.ruleNodeFormGroup">
@@ -113,7 +112,7 @@ [ruleChainType]="ruleChainType" [isEdit]="true" [isReadOnly]="false" - [debugEventBody]="debugEventBody"> + (initRuleNode)="onRuleNodeInit()"> @@ -122,9 +121,7 @@ [active]="eventsTab.isActive" [tenantId]="ruleChain.tenantId.id" [entityId]="editingRuleNode.ruleNodeId" - [isRuleNodeDebugModeEnabled]="editingRuleNode?.debugMode" - [editingRuleNodeHasScript]="editingRuleNodeHasScript" - [rulenodeClazz]="editingRuleNode.component.clazz" + [ruleNodeTestButtonLabel]="ruleNodeTestButtonLabel" (debugEventSelected)="onDebugEventSelected($event)"> diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts index ff9d9060c8..d5ea093721 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts @@ -153,8 +153,7 @@ export class RuleChainPageComponent extends PageComponent editingRuleNodeAllowCustomLabels = false; editingRuleNodeLinkLabels: {[label: string]: LinkLabel}; editingRuleNodeSourceRuleChainId: string; - debugEventBody: DebugRuleNodeEventBody; - editingRuleNodeHasScript: boolean = false; + ruleNodeTestButtonLabel: string; @ViewChild('tbRuleNode') ruleNodeComponent: RuleNodeDetailsComponent; @ViewChild('tbRuleNodeLink') ruleNodeLinkComponent: RuleNodeLinkComponent; @@ -1112,7 +1111,6 @@ export class RuleChainPageComponent extends PageComponent this.isEditingRuleNode = true; this.editingRuleNodeIndex = this.ruleChainModel.nodes.indexOf(node); this.editingRuleNode = deepClone(node, ['component']); - this.editingRuleNodeHasScript = this.editingRuleNode.configuration.hasOwnProperty('scriptLang'); setTimeout(() => { this.ruleNodeComponent.ruleNodeFormGroup.markAsPristine(); }, 0); @@ -1261,7 +1259,6 @@ export class RuleChainPageComponent extends PageComponent onEditRuleNodeClosed() { this.editingRuleNode = null; this.isEditingRuleNode = false; - this.debugEventBody = null; } onEditRuleNodeLinkClosed() { @@ -1282,13 +1279,25 @@ export class RuleChainPageComponent extends PageComponent } onDebugEventSelected(debugEventBody: DebugRuleNodeEventBody) { - if (debugEventBody) { - this.debugEventBody = debugEventBody; - } + if (this.ruleNodeComponent.ruleNodeConfigComponent.useDefinedDirective() && + this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.getSupportTestFunction() && + this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.testScript$) { + this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.testScript$(debugEventBody) + .subscribe((value) => { + if (value) { + this.selectedRuleNodeTabIndex = 0; + } + }) + } } - onSwitchToFirstTab() { - this.selectedRuleNodeTabIndex = 0; + onRuleNodeInit() { + if (this.ruleNodeComponent.ruleNodeConfigComponent.useDefinedDirective() && + this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.getSupportTestFunction()) { + this.ruleNodeTestButtonLabel = this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.getTestButtonLabel(); + } else { + this.ruleNodeTestButtonLabel = ''; + } } saveRuleNode() { diff --git a/ui-ngx/src/app/shared/models/rule-node.models.ts b/ui-ngx/src/app/shared/models/rule-node.models.ts index 84ea8ddd3e..dd427aaf09 100644 --- a/ui-ngx/src/app/shared/models/rule-node.models.ts +++ b/ui-ngx/src/app/shared/models/rule-node.models.ts @@ -27,6 +27,7 @@ import { AppState } from '@core/core.state'; import { AbstractControl, UntypedFormGroup } from '@angular/forms'; import { RuleChainType } from '@shared/models/rule-chain.models'; import { DebugRuleNodeEventBody } from '@shared/models/event.models'; +import { TranslateService } from '@ngx-translate/core'; export interface RuleNodeConfiguration { [key: string]: any; @@ -76,7 +77,9 @@ export interface IRuleNodeConfigurationComponent { configuration: RuleNodeConfiguration; configurationChanged: Observable; validate(); - testScript? (debugEventBody: DebugRuleNodeEventBody); + getSupportTestFunction(): boolean; + getTestButtonLabel? (): string; + testScript$? (debugEventBody?: DebugRuleNodeEventBody): Observable; [key: string]: any; } @@ -112,7 +115,8 @@ export abstract class RuleNodeConfigurationComponent extends PageComponent imple configurationChangedEmiter = new EventEmitter(); configurationChanged = this.configurationChangedEmiter.asObservable(); - protected constructor(@Inject(Store) protected store: Store) { + protected constructor(@Inject(Store) protected store: Store, + @Inject(TranslateService) protected translate: TranslateService) { super(store); } @@ -130,6 +134,14 @@ export abstract class RuleNodeConfigurationComponent extends PageComponent imple this.onValidate(); } + getSupportTestFunction(): boolean { + return false; + } + + getTestButtonLabel(): string { + return this.translate.instant('rulenode.test-script-function'); + } + protected setupConfiguration(configuration: RuleNodeConfiguration) { this.onConfigurationSet(this.prepareInputConfig(configuration)); this.updateValidators(false); @@ -429,22 +441,13 @@ export const messageTypeNames = new Map( export const ruleChainNodeClazz = 'org.thingsboard.rule.engine.flow.TbRuleChainInputNode'; export const outputNodeClazz = 'org.thingsboard.rule.engine.flow.TbRuleChainOutputNode'; -export enum RuleNodeClazz { - TbJsFilterNode = 'org.thingsboard.rule.engine.filter.TbJsFilterNode', - TbLogNode = 'org.thingsboard.rule.engine.action.TbLogNode', - TbJsSwitchNode = 'org.thingsboard.rule.engine.filter.TbJsSwitchNode', - TbClearAlarmNode = 'org.thingsboard.rule.engine.action.TbClearAlarmNode', - TbCreateAlarmNode = 'org.thingsboard.rule.engine.action.TbCreateAlarmNode', - TbTransformMsgNode = 'org.thingsboard.rule.engine.transform.TbTransformMsgNode', - TbMsgGeneratorNode = 'org.thingsboard.rule.engine.debug.TbMsgGeneratorNode' -} const ruleNodeClazzHelpLinkMap = { 'org.thingsboard.rule.engine.filter.TbCheckRelationNode': 'ruleNodeCheckRelation', 'org.thingsboard.rule.engine.filter.TbCheckMessageNode': 'ruleNodeCheckExistenceFields', 'org.thingsboard.rule.engine.geo.TbGpsGeofencingFilterNode': 'ruleNodeGpsGeofencingFilter', - [RuleNodeClazz.TbJsFilterNode]: 'ruleNodeJsFilter', - [RuleNodeClazz.TbJsSwitchNode]: 'ruleNodeJsSwitch', + 'org.thingsboard.rule.engine.filter.TbJsFilterNode': 'ruleNodeJsFilter', + 'org.thingsboard.rule.engine.filter.TbJsSwitchNode': 'ruleNodeJsSwitch', 'org.thingsboard.rule.engine.filter.TbAssetTypeSwitchNode': 'ruleNodeAssetProfileSwitch', 'org.thingsboard.rule.engine.filter.TbDeviceTypeSwitchNode': 'ruleNodeDeviceProfileSwitch', 'org.thingsboard.rule.engine.filter.TbCheckAlarmStatusNode': 'ruleNodeCheckAlarmStatus', @@ -463,18 +466,18 @@ const ruleNodeClazzHelpLinkMap = { 'org.thingsboard.rule.engine.metadata.TbGetTenantDetailsNode': 'ruleNodeTenantDetails', 'org.thingsboard.rule.engine.metadata.CalculateDeltaNode': 'ruleNodeCalculateDelta', 'org.thingsboard.rule.engine.transform.TbChangeOriginatorNode': 'ruleNodeChangeOriginator', - [RuleNodeClazz.TbTransformMsgNode]: 'ruleNodeTransformMsg', + 'org.thingsboard.rule.engine.transform.TbTransformMsgNode': 'ruleNodeTransformMsg', 'org.thingsboard.rule.engine.mail.TbMsgToEmailNode': 'ruleNodeMsgToEmail', 'org.thingsboard.rule.engine.action.TbAssignToCustomerNode': 'ruleNodeAssignToCustomer', 'org.thingsboard.rule.engine.action.TbUnassignFromCustomerNode': 'ruleNodeUnassignFromCustomer', - [RuleNodeClazz.TbClearAlarmNode]: 'ruleNodeClearAlarm', - [RuleNodeClazz.TbCreateAlarmNode]: 'ruleNodeCreateAlarm', + 'org.thingsboard.rule.engine.action.TbClearAlarmNode': 'ruleNodeClearAlarm', + 'org.thingsboard.rule.engine.action.TbCreateAlarmNode': 'ruleNodeCreateAlarm', 'org.thingsboard.rule.engine.action.TbCreateRelationNode': 'ruleNodeCreateRelation', 'org.thingsboard.rule.engine.action.TbDeleteRelationNode': 'ruleNodeDeleteRelation', 'org.thingsboard.rule.engine.delay.TbMsgDelayNode': 'ruleNodeMsgDelay', - [RuleNodeClazz.TbMsgGeneratorNode]: 'ruleNodeMsgGenerator', + 'org.thingsboard.rule.engine.debug.TbMsgGeneratorNode': 'ruleNodeMsgGenerator', 'org.thingsboard.rule.engine.geo.TbGpsGeofencingActionNode': 'ruleNodeGpsGeofencingEvents', - [RuleNodeClazz.TbLogNode]: 'ruleNodeLog', + 'org.thingsboard.rule.engine.action.TbLogNode': 'ruleNodeLog', 'org.thingsboard.rule.engine.rpc.TbSendRPCReplyNode': 'ruleNodeRpcCallReply', 'org.thingsboard.rule.engine.rpc.TbSendRPCRequestNode': 'ruleNodeRpcCallRequest', 'org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode': 'ruleNodeSaveAttributes', @@ -511,12 +514,3 @@ export function getRuleNodeHelpLink(component: RuleNodeComponentDescriptor): str return 'ruleEngine'; } -export const ruleNodeClazzFunctionNameTranslations = { - [RuleNodeClazz.TbJsFilterNode]: 'rulenode.function-name.filter', - [RuleNodeClazz.TbLogNode]: 'rulenode.function-name.to-string', - [RuleNodeClazz.TbJsSwitchNode]: 'rulenode.function-name.switch', - [RuleNodeClazz.TbClearAlarmNode]: 'rulenode.function-name.details', - [RuleNodeClazz.TbCreateAlarmNode]: 'rulenode.function-name.details', - [RuleNodeClazz.TbTransformMsgNode]: 'rulenode.function-name.transform', - [RuleNodeClazz.TbMsgGeneratorNode]: 'rulenode.function-name.generate' -} diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index d3b374f1ec..ef94480aa8 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -3467,15 +3467,7 @@ "test": "Test", "help": "Help", "reset-debug-mode": "Reset debug mode in all nodes", - "test-function": "Test '{{function}}' function with this message", - "function-name": { - "filter": "Filter", - "to-string": "ToString", - "details": "Details", - "generate": "Generate", - "switch": "Switch", - "transform": "Transform" - } + "test-with-this-message": "{{test}} with this message" }, "timezone": { "timezone": "Timezone", From ac31c06f1ebe46129f83abb3549349c6e6f5269b Mon Sep 17 00:00:00 2001 From: rusikv Date: Tue, 11 Jul 2023 15:36:07 +0300 Subject: [PATCH 3/5] Removed unused service, generic event type used --- .../modules/home/components/event/event-table-config.ts | 6 ++---- .../home/components/event/event-table.component.ts | 9 +++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts index 7382633e33..565dbf14bf 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts @@ -21,7 +21,7 @@ import { EntityTableColumn, EntityTableConfig } from '@home/models/entity/entities-table-config.models'; -import { DebugEventType, DebugRuleNodeEventBody, Event, EventType, FilterEventBody } from '@shared/models/event.models'; +import { DebugEventType, Event, EventBody, EventType, FilterEventBody } from '@shared/models/event.models'; import { TimePageLink } from '@shared/models/page/page-link'; import { TranslateService } from '@ngx-translate/core'; import { DatePipe } from '@angular/common'; @@ -49,7 +49,6 @@ import { EventFilterPanelData, FilterEntityColumn } from '@home/components/event/event-filter-panel.component'; -import { NodeScriptTestService } from '@core/services/script/node-script-test.service'; export class EventTableConfig extends EntityTableConfig { @@ -87,9 +86,8 @@ export class EventTableConfig extends EntityTableConfig { private overlay: Overlay, private viewContainerRef: ViewContainerRef, private cd: ChangeDetectorRef, - private nodeScriptTestService: NodeScriptTestService, public testButtonLabel?: string, - private debugEventSelected?: EventEmitter) { + private debugEventSelected?: EventEmitter) { super(); this.loadDataOnInit = false; this.tableTitle = ''; diff --git a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts index 80394de979..8a1d71eaf4 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts @@ -32,10 +32,9 @@ import { EntitiesTableComponent } from '@home/components/entity/entities-table.c import { EventTableConfig } from './event-table-config'; import { EventService } from '@core/http/event.service'; import { DialogService } from '@core/services/dialog.service'; -import { DebugEventType, DebugRuleNodeEventBody, EventType } from '@shared/models/event.models'; +import { DebugEventType, EventBody, EventType } from '@shared/models/event.models'; import { Overlay } from '@angular/cdk/overlay'; import { Subscription } from 'rxjs'; -import { NodeScriptTestService } from '@core/services/script/node-script-test.service'; import { isNotEmptyStr } from '@core/utils'; @Component({ @@ -109,7 +108,7 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { } @Output() - debugEventSelected = new EventEmitter(null); + debugEventSelected = new EventEmitter(); @ViewChild(EntitiesTableComponent, {static: true}) entitiesTable: EntitiesTableComponent; @@ -124,8 +123,7 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { private dialog: MatDialog, private overlay: Overlay, private viewContainerRef: ViewContainerRef, - private cd: ChangeDetectorRef, - private nodeScriptTestService: NodeScriptTestService) { + private cd: ChangeDetectorRef) { } ngOnInit() { @@ -144,7 +142,6 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { this.overlay, this.viewContainerRef, this.cd, - this.nodeScriptTestService, this.ruleNodeTestButtonLabel, this.debugEventSelected ); From a630392d6689b931a2e8fd365383f9b4c22fdee6 Mon Sep 17 00:00:00 2001 From: rusikv Date: Wed, 12 Jul 2023 17:21:45 +0300 Subject: [PATCH 4/5] Renamed ruleNodeTestButtonLabel to functionTestButtonLabel to be able to use for convertors debug --- .../components/event/event-table.component.ts | 16 ++++++++-------- .../rulechain/rulechain-page.component.html | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts index 8a1d71eaf4..b354e49388 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table.component.ts @@ -88,21 +88,21 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { } } - private ruleNodeTestButtonLabelValue: string; + private functionTestButtonLabelValue: string; - get ruleNodeTestButtonLabel(): string { - return this.ruleNodeTestButtonLabelValue; + get functionTestButtonLabel(): string { + return this.functionTestButtonLabelValue; } @Input() - set ruleNodeTestButtonLabel(value: string) { + set functionTestButtonLabel(value: string) { if (isNotEmptyStr(value)) { - this.ruleNodeTestButtonLabelValue = value; + this.functionTestButtonLabelValue = value; } else { - this.ruleNodeTestButtonLabelValue = ''; + this.functionTestButtonLabelValue = ''; } if (this.eventTableConfig) { - this.eventTableConfig.testButtonLabel = this.ruleNodeTestButtonLabel; + this.eventTableConfig.testButtonLabel = this.functionTestButtonLabel; this.eventTableConfig.updateCellAction(); } } @@ -142,7 +142,7 @@ export class EventTableComponent implements OnInit, AfterViewInit, OnDestroy { this.overlay, this.viewContainerRef, this.cd, - this.ruleNodeTestButtonLabel, + this.functionTestButtonLabel, this.debugEventSelected ); } diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html index 5e20a6a8e2..ba91b45f16 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html @@ -121,7 +121,7 @@ [active]="eventsTab.isActive" [tenantId]="ruleChain.tenantId.id" [entityId]="editingRuleNode.ruleNodeId" - [ruleNodeTestButtonLabel]="ruleNodeTestButtonLabel" + [functionTestButtonLabel]="ruleNodeTestButtonLabel" (debugEventSelected)="onDebugEventSelected($event)"> From e557a2d2c4cc779135ff4946d79845d9762c633b Mon Sep 17 00:00:00 2001 From: rusikv Date: Fri, 14 Jul 2023 18:59:20 +0300 Subject: [PATCH 5/5] Refactoring --- .../components/details-panel.component.ts | 5 ++- .../components/event/event-table-config.ts | 2 +- .../rulechain/rule-node-config.component.ts | 16 ++++++++++ .../rule-node-details.component.html | 3 +- .../rulechain/rule-node-details.component.ts | 3 ++ .../rulechain/rulechain-page.component.html | 3 +- .../rulechain/rulechain-page.component.ts | 31 +++++++++---------- .../src/app/shared/models/rule-node.models.ts | 20 +++++------- 8 files changed, 48 insertions(+), 35 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/details-panel.component.ts b/ui-ngx/src/app/modules/home/components/details-panel.component.ts index 66facf08d4..b2f8a059f3 100644 --- a/ui-ngx/src/app/modules/home/components/details-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/details-panel.component.ts @@ -15,6 +15,7 @@ /// import { + ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, @@ -55,9 +56,7 @@ export class DetailsPanelComponent extends PageComponent implements OnDestroy { } this.theFormValue = value; if (this.theFormValue !== null) { - this.formSubscription = this.theFormValue.valueChanges.subscribe(() => { - this.cd.detectChanges() - }); + this.formSubscription = this.theFormValue.valueChanges.subscribe(() => this.cd.detectChanges()); } } } diff --git a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts index 565dbf14bf..4021d018ce 100644 --- a/ui-ngx/src/app/modules/home/components/event/event-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/event/event-table-config.ts @@ -359,7 +359,7 @@ export class EventTableConfig extends EntityTableConfig { case DebugEventType.DEBUG_RULE_NODE: if (this.testButtonLabel) { this.cellActionDescriptors.push({ - name: this.translate.instant('rulenode.test-with-this-message', {test: this.testButtonLabel}), + name: this.translate.instant('rulenode.test-with-this-message', {test: this.translate.instant(this.testButtonLabel)}), icon: 'bug_report', isEnabled: (entity) => entity.body.type === 'IN', onAction: ($event, entity) => { diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts index 6c19507301..3f3071221e 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-config.component.ts @@ -87,6 +87,9 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On @Output() initRuleNode = new EventEmitter(); + @Output() + changeScript = new EventEmitter(); + nodeDefinitionValue: RuleNodeDefinition; @Input() @@ -110,6 +113,8 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On changeSubscription: Subscription; + changeScriptSubscription: Subscription; + definedConfigComponent: IRuleNodeConfigurationComponent; private definedConfigComponentRef: ComponentRef; @@ -140,6 +145,14 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On if (this.definedConfigComponentRef) { this.definedConfigComponentRef.destroy(); } + if (this.changeSubscription) { + this.changeSubscription.unsubscribe(); + this.changeSubscription = null; + } + if (this.changeScriptSubscription) { + this.changeScriptSubscription.unsubscribe(); + this.changeScriptSubscription = null; + } } ngAfterViewInit(): void { @@ -212,6 +225,9 @@ export class RuleNodeConfigComponent implements ControlValueAccessor, OnInit, On this.changeSubscription = this.definedConfigComponent.configurationChanged.subscribe((configuration) => { this.updateModel(configuration); }); + if (this.definedConfigComponent?.changeScript) { + this.changeScriptSubscription = this.definedConfigComponent.changeScript.subscribe(() => this.changeScript.emit()); + } } } diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html index 2f925ded60..34aa8167e3 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.html @@ -51,7 +51,8 @@ [ruleChainId]="ruleChainId" [ruleChainType]="ruleChainType" [nodeDefinition]="ruleNode.component.configurationDescriptor.nodeDefinition" - (initRuleNode)="initRuleNode.emit($event)"> + (initRuleNode)="initRuleNode.emit($event)" + (changeScript)="changeScript.emit($event)">
diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts index 7b0f426c35..f1d6001c88 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rule-node-details.component.ts @@ -58,6 +58,9 @@ export class RuleNodeDetailsComponent extends PageComponent implements OnInit, O @Output() initRuleNode = new EventEmitter(); + @Output() + changeScript = new EventEmitter(); + ruleNodeType = RuleNodeType; entityType = EntityType; diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html index ba91b45f16..5f5594cc1a 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.html @@ -112,7 +112,8 @@ [ruleChainType]="ruleChainType" [isEdit]="true" [isReadOnly]="false" - (initRuleNode)="onRuleNodeInit()"> + (initRuleNode)="onRuleNodeInit()" + (changeScript)="switchToFirstTab()"> diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts index d5ea093721..6b78d94ac7 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-page.component.ts @@ -1279,25 +1279,24 @@ export class RuleChainPageComponent extends PageComponent } onDebugEventSelected(debugEventBody: DebugRuleNodeEventBody) { - if (this.ruleNodeComponent.ruleNodeConfigComponent.useDefinedDirective() && - this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.getSupportTestFunction() && - this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.testScript$) { - this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.testScript$(debugEventBody) - .subscribe((value) => { - if (value) { - this.selectedRuleNodeTabIndex = 0; - } - }) - } + const ruleNodeConfigComponent = this.ruleNodeComponent.ruleNodeConfigComponent; + const ruleNodeConfigDefinedComponent = ruleNodeConfigComponent.definedConfigComponent; + if (ruleNodeConfigComponent.useDefinedDirective() && ruleNodeConfigDefinedComponent.hasScript && ruleNodeConfigDefinedComponent.testScript) { + ruleNodeConfigDefinedComponent.testScript(debugEventBody); + } } onRuleNodeInit() { - if (this.ruleNodeComponent.ruleNodeConfigComponent.useDefinedDirective() && - this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.getSupportTestFunction()) { - this.ruleNodeTestButtonLabel = this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent.getTestButtonLabel(); - } else { - this.ruleNodeTestButtonLabel = ''; - } + const ruleNodeConfigDefinedComponent = this.ruleNodeComponent.ruleNodeConfigComponent.definedConfigComponent; + if (this.ruleNodeComponent.ruleNodeConfigComponent.useDefinedDirective() && ruleNodeConfigDefinedComponent.hasScript) { + this.ruleNodeTestButtonLabel = ruleNodeConfigDefinedComponent.testScriptLabel; + } else { + this.ruleNodeTestButtonLabel = ''; + } + } + + switchToFirstTab() { + this.selectedRuleNodeTabIndex = 0; } saveRuleNode() { diff --git a/ui-ngx/src/app/shared/models/rule-node.models.ts b/ui-ngx/src/app/shared/models/rule-node.models.ts index dd427aaf09..2a7dedabec 100644 --- a/ui-ngx/src/app/shared/models/rule-node.models.ts +++ b/ui-ngx/src/app/shared/models/rule-node.models.ts @@ -73,13 +73,14 @@ export interface RuleNodeConfigurationDescriptor { export interface IRuleNodeConfigurationComponent { ruleNodeId: string; ruleChainId: string; + hasScript: boolean; + testScriptLabel?: string; + changeScript?: EventEmitter; ruleChainType: RuleChainType; configuration: RuleNodeConfiguration; configurationChanged: Observable; validate(); - getSupportTestFunction(): boolean; - getTestButtonLabel? (): string; - testScript$? (debugEventBody?: DebugRuleNodeEventBody): Observable; + testScript? (debugEventBody?: DebugRuleNodeEventBody); [key: string]: any; } @@ -92,6 +93,8 @@ export abstract class RuleNodeConfigurationComponent extends PageComponent imple ruleChainId: string; + hasScript: boolean = false; + ruleChainType: RuleChainType; configurationValue: RuleNodeConfiguration; @@ -115,8 +118,7 @@ export abstract class RuleNodeConfigurationComponent extends PageComponent imple configurationChangedEmiter = new EventEmitter(); configurationChanged = this.configurationChangedEmiter.asObservable(); - protected constructor(@Inject(Store) protected store: Store, - @Inject(TranslateService) protected translate: TranslateService) { + protected constructor(@Inject(Store) protected store: Store) { super(store); } @@ -134,14 +136,6 @@ export abstract class RuleNodeConfigurationComponent extends PageComponent imple this.onValidate(); } - getSupportTestFunction(): boolean { - return false; - } - - getTestButtonLabel(): string { - return this.translate.instant('rulenode.test-script-function'); - } - protected setupConfiguration(configuration: RuleNodeConfiguration) { this.onConfigurationSet(this.prepareInputConfig(configuration)); this.updateValidators(false);