From d8de53c186c9818d02495077e2f7ebbc352f733d Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Wed, 13 Jan 2021 18:43:49 +0200 Subject: [PATCH] Code review fixes --- ui-ngx/src/app/core/http/entity.service.ts | 4 +- .../src/app/core/http/rule-chain.service.ts | 8 +- .../app/core/services/item-buffer.service.ts | 2 +- .../import-export/import-export.service.ts | 5 +- .../rule-chain-autocomplete.component.ts | 4 +- .../add-entities-to-edge-dialog.component.ts | 4 +- .../home/pages/edge/edge-routing.module.ts | 6 +- .../modules/home/pages/edge/edge.component.ts | 28 ++----- .../pages/edge/edges-table-config.resolver.ts | 79 +++++++++---------- .../rulechain/rule-node-details.component.ts | 6 +- .../rulechain/rulechain-page.component.ts | 13 ++- .../rulechain/rulechain-routing.module.ts | 7 +- .../rulechains-table-config.resolver.ts | 14 ++-- .../pages/rulechain/rulenode.component.ts | 4 +- .../widgets-bundles-table-config.resolver.ts | 16 ++-- .../widgets-bundle-select.component.ts | 14 ++-- .../app/shared/models/rule-chain.models.ts | 4 +- .../src/app/shared/models/rule-node.models.ts | 3 +- 18 files changed, 96 insertions(+), 125 deletions(-) diff --git a/ui-ngx/src/app/core/http/entity.service.ts b/ui-ngx/src/app/core/http/entity.service.ts index c9832b4cc3..fa2dd79c53 100644 --- a/ui-ngx/src/app/core/http/entity.service.ts +++ b/ui-ngx/src/app/core/http/entity.service.ts @@ -69,7 +69,7 @@ import { } from '@shared/models/query/query.models'; import { alarmFields } from '@shared/models/alarm.models'; import { EdgeService } from "@core/http/edge.service"; -import { ruleChainType } from "@shared/models/rule-chain.models"; +import { RuleChainType } from "@shared/models/rule-chain.models"; @Injectable({ providedIn: 'root' @@ -324,7 +324,7 @@ export class EntityService { entitiesObservable = this.ruleChainService.getRuleChains(pageLink, subType, config); } else { // safe fallback to default core type - entitiesObservable = this.ruleChainService.getRuleChains(pageLink, ruleChainType.core, config); + entitiesObservable = this.ruleChainService.getRuleChains(pageLink, RuleChainType.core, config); } break; case EntityType.DASHBOARD: diff --git a/ui-ngx/src/app/core/http/rule-chain.service.ts b/ui-ngx/src/app/core/http/rule-chain.service.ts index 7a04d388d2..d67eec5468 100644 --- a/ui-ngx/src/app/core/http/rule-chain.service.ts +++ b/ui-ngx/src/app/core/http/rule-chain.service.ts @@ -151,12 +151,8 @@ export class RuleChainService { return this.ruleNodeConfigFactories[directive]; } - public getRuleNodeComponentByClazz(clazz: string): RuleNodeComponentDescriptor { - let mergedRuleNodeComponents: RuleNodeComponentDescriptor[] = []; - this.ruleNodeComponentsMap.forEach((value: Array, key: RuleChainType) => { - mergedRuleNodeComponents = mergedRuleNodeComponents.concat(value); - }); - const found = mergedRuleNodeComponents.filter((component) => component.clazz === clazz); + public getRuleNodeComponentByClazz(ruleChainType: RuleChainType = RuleChainType.core, clazz: string): RuleNodeComponentDescriptor { + const found = this.ruleNodeComponentsMap.get(ruleChainType).filter((component) => component.clazz === clazz); if (found && found.length) { return found[0]; } else { diff --git a/ui-ngx/src/app/core/services/item-buffer.service.ts b/ui-ngx/src/app/core/services/item-buffer.service.ts index 76c3545f6a..24f00dcb6d 100644 --- a/ui-ngx/src/app/core/services/item-buffer.service.ts +++ b/ui-ngx/src/app/core/services/item-buffer.service.ts @@ -331,7 +331,7 @@ export class ItemBufferService { const deltaX = x - ruleNodes.originX; const deltaY = y - ruleNodes.originY; for (const node of ruleNodes.nodes) { - const component = this.ruleChainService.getRuleNodeComponentByClazz(node.componentClazz); + const component = this.ruleChainService.getRuleNodeComponentByClazz(node.ruleChainType, node.componentClazz); if (component) { let icon = ruleNodeTypeDescriptors.get(component.type).icon; let iconUrl: string = null; diff --git a/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts b/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts index c55027d10c..dc71e02d72 100644 --- a/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts +++ b/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts @@ -56,8 +56,7 @@ import { RuleChain, RuleChainImport, RuleChainMetaData, - RuleChainType, - ruleChainType + RuleChainType } from '@shared/models/rule-chain.models'; import { RuleChainService } from '@core/http/rule-chain.service'; import * as JSZip from 'jszip'; @@ -470,7 +469,7 @@ export class ImportExportService { return false; } if (isUndefined(ruleChainImport.ruleChain.type)) { - ruleChainImport.ruleChain.type = ruleChainType.core; + ruleChainImport.ruleChain.type = RuleChainType.core; } return true; } diff --git a/ui-ngx/src/app/modules/home/components/rule-chain/rule-chain-autocomplete.component.ts b/ui-ngx/src/app/modules/home/components/rule-chain/rule-chain-autocomplete.component.ts index 946ed76a80..cdc8935d74 100644 --- a/ui-ngx/src/app/modules/home/components/rule-chain/rule-chain-autocomplete.component.ts +++ b/ui-ngx/src/app/modules/home/components/rule-chain/rule-chain-autocomplete.component.ts @@ -29,7 +29,7 @@ import { EntityService } from '@core/http/entity.service'; import { TruncatePipe } from '@shared/pipe/truncate.pipe'; import { RuleChainService } from '@core/http/rule-chain.service'; import { MatAutocompleteTrigger } from '@angular/material/autocomplete'; -import { ruleChainType } from '@app/shared/models/rule-chain.models'; +import { RuleChainType } from '@app/shared/models/rule-chain.models'; @Component({ selector: 'tb-rule-chain-autocomplete', @@ -190,7 +190,7 @@ export class RuleChainAutocompleteComponent implements ControlValueAccessor, OnI this.searchText = searchText; // voba: at the moment device profiles are not supported by edge, so 'core' hardcoded return this.entityService.getEntitiesByNameFilter(EntityType.RULE_CHAIN, searchText, - 50, ruleChainType.core, {ignoreLoading: true}); + 50, RuleChainType.core, {ignoreLoading: true}); } clear() { diff --git a/ui-ngx/src/app/modules/home/dialogs/add-entities-to-edge-dialog.component.ts b/ui-ngx/src/app/modules/home/dialogs/add-entities-to-edge-dialog.component.ts index 2bcc6be99c..2ce5c9ae38 100644 --- a/ui-ngx/src/app/modules/home/dialogs/add-entities-to-edge-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/dialogs/add-entities-to-edge-dialog.component.ts @@ -30,7 +30,7 @@ import { DashboardService } from '@core/http/dashboard.service'; import { DialogComponent } from '@shared/components/dialog.component'; import { Router } from '@angular/router'; import { RuleChainService } from '@core/http/rule-chain.service'; -import { ruleChainType } from '@shared/models/rule-chain.models'; +import { RuleChainType } from '@shared/models/rule-chain.models'; export interface AddEntitiesToEdgeDialogData { edgeId: string; @@ -85,7 +85,7 @@ export class AddEntitiesToEdgeDialogComponent extends case EntityType.RULE_CHAIN: this.assignToEdgeTitle = 'rulechain.assign-rulechain-to-edge-title'; this.assignToEdgeText = 'rulechain.assign-rulechain-to-edge-text'; - this.subType = ruleChainType.edge; + this.subType = RuleChainType.edge; break; case EntityType.ASSET: this.assignToEdgeTitle = 'asset.assign-asset-to-edge-title'; diff --git a/ui-ngx/src/app/modules/home/pages/edge/edge-routing.module.ts b/ui-ngx/src/app/modules/home/pages/edge/edge-routing.module.ts index bfa48160b4..2dc4a46a17 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edge-routing.module.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edge-routing.module.ts @@ -29,7 +29,7 @@ import { dashboardBreadcumbLabelFunction, DashboardResolver } from '@home/pages/ import { BreadCrumbConfig } from '@shared/components/breadcrumb'; import { RuleChainPageComponent } from '@home/pages/rulechain/rulechain-page.component'; import { ConfirmOnExitGuard } from '@core/guards/confirm-on-exit.guard'; -import { ruleChainType } from '@shared/models/rule-chain.models'; +import { RuleChainType } from '@shared/models/rule-chain.models'; import { importRuleChainBreadcumbLabelFunction, ResolvedRuleChainMetaDataResolver, @@ -189,7 +189,7 @@ const routes: Routes = [ auth: [Authority.TENANT_ADMIN], title: 'rulechain.edge-rulechain', import: false, - ruleChainType: ruleChainType.edge + ruleChainType: RuleChainType.edge }, resolve: { ruleChain: RuleChainResolver, @@ -210,7 +210,7 @@ const routes: Routes = [ auth: [Authority.TENANT_ADMIN], title: 'rulechain.edge-rulechain', import: true, - ruleChainType: ruleChainType.edge + ruleChainType: RuleChainType.edge }, resolve: { ruleNodeComponents: RuleNodeComponentsResolver diff --git a/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts b/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts index 393fa0556b..87c745be28 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts @@ -24,7 +24,7 @@ import { EdgeInfo } from '@shared/models/edge.models'; import { TranslateService } from '@ngx-translate/core'; import { NULL_UUID } from '@shared/models/id/has-uuid'; import { ActionNotificationShow } from '@core/notification/notification.actions'; -import { guid, isUndefined } from '@core/utils'; +import { generateSecret, guid } from '@core/utils'; import { EntityTableConfig } from '@home/models/entity/entities-table-config.models'; import { WINDOW } from '@core/services/window.service'; @@ -51,7 +51,7 @@ export class EdgeComponent extends EntityComponent { ngOnInit() { this.edgeScope = this.entitiesTableConfig.componentsData.edgeScope; this.entityForm.patchValue({ - cloudEndpoint:this.window.location.origin + cloudEndpoint: this.window.location.origin }); super.ngOnInit(); } @@ -76,8 +76,8 @@ export class EdgeComponent extends EntityComponent { label: [entity ? entity.label : ''], cloudEndpoint: [null, [Validators.required]], edgeLicenseKey: ['', [Validators.required]], - routingKey: this.fb.control({ value: entity ? entity.routingKey : null, disabled: true }), - secret: this.fb.control({ value: entity ? entity.secret : null, disabled: true }), + routingKey: this.fb.control({value: entity ? entity.routingKey : null, disabled: true}), + secret: this.fb.control({value: entity ? entity.secret : null, disabled: true}), additionalInfo: this.fb.group( { description: [entity && entity.additionalInfo ? entity.additionalInfo.description : ''] @@ -107,14 +107,14 @@ export class EdgeComponent extends EntityComponent { updateFormState() { super.updateFormState(); - this.entityForm.get('routingKey').disable({ emitEvent: false }); - this.entityForm.get('secret').disable({ emitEvent: false }); + this.entityForm.get('routingKey').disable({emitEvent: false}); + this.entityForm.get('secret').disable({emitEvent: false}); } private checkIsNewEdge(entity: EdgeInfo, form: FormGroup) { if (entity && !entity.id) { - form.get('routingKey').patchValue(guid(), { emitEvent: false }); - form.get('secret').patchValue(this.generateSecret(20), { emitEvent: false }); + form.get('routingKey').patchValue(guid(), {emitEvent: false}); + form.get('secret').patchValue(generateSecret(20), {emitEvent: false}); } } @@ -129,18 +129,6 @@ export class EdgeComponent extends EntityComponent { })); } - generateSecret(length): string { - if (isUndefined(length) || length == null) { - length = 1; - } - var l = length > 10 ? 10 : length; - var str = Math.random().toString(36).substr(2, l); - if (str.length >= length) { - return str; - } - return str.concat(this.generateSecret(length - str.length)); - } - onEdgeInfoCopied(type: string) { const message = type === 'key' ? 'edge.edge-key-copied-message' : 'edge.edge-secret-copied-message'; diff --git a/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts b/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts index f175a25e9e..f52f49e63c 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts @@ -210,31 +210,31 @@ export class EdgesTableConfigResolver implements Resolve true, - onAction: ($event, entity) => this.openEdgeAssets($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.ASSET) }, { name: this.translate.instant('edge.manage-edge-devices'), icon: 'devices_other', isEnabled: (entity) => true, - onAction: ($event, entity) => this.openEdgeDevices($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.DEVICE) }, { name: this.translate.instant('edge.manage-edge-entity-views'), icon: 'view_quilt', isEnabled: (entity) => true, - onAction: ($event, entity) => this.openEdgeEntityViews($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.ENTITY_VIEW) }, { name: this.translate.instant('edge.manage-edge-dashboards'), icon: 'dashboard', isEnabled: (entity) => true, - onAction: ($event, entity) => this.openEdgeDashboards($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.DASHBOARD) }, { name: this.translate.instant('edge.manage-edge-rulechains'), icon: 'settings_ethernet', isEnabled: (entity) => true, - onAction: ($event, entity) => this.openEdgeRuleChains($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.RULE_CHAIN) } ); } @@ -260,25 +260,25 @@ export class EdgesTableConfigResolver implements Resolve true, - onAction: ($event, entity) => this.openEdgeAssets($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.ASSET) }, { name: this.translate.instant('edge.manage-edge-devices'), icon: 'devices_other', isEnabled: (entity) => true, - onAction: ($event, entity) => this.openEdgeDevices($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.DEVICE) }, { name: this.translate.instant('edge.manage-edge-entity-views'), icon: 'view_quilt', isEnabled: (entity) => true, - onAction: ($event, entity) => this.openEdgeEntityViews($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.ENTITY_VIEW) }, { name: this.translate.instant('edge.manage-edge-dashboards'), icon: 'dashboard', isEnabled: (entity) => true, - onAction: ($event, entity) => this.openEdgeDashboards($event, entity) + onAction: ($event, entity) => this.openEdgeEntitiesByType($event, entity, EntityType.DASHBOARD) } ); } @@ -392,39 +392,32 @@ export class EdgesTableConfigResolver implements Resolve) { @@ -537,19 +530,19 @@ export class EdgesTableConfigResolver implements Resolve { - const component = this.ruleChainService.getRuleNodeComponentByClazz(ruleNode.type); + const component = this.ruleChainService.getRuleNodeComponentByClazz(this.ruleChainType, ruleNode.type); const descriptor = ruleNodeTypeDescriptors.get(component.type); let icon = descriptor.icon; let iconUrl = null; @@ -1287,7 +1286,7 @@ export class RuleChainPageComponent extends PageComponent if (this.isImport) { this.isDirtyValue = false; this.isImport = false; - if (this.ruleChainType !== ruleChainType.edge) { + if (this.ruleChainType !== RuleChainType.edge) { this.router.navigateByUrl(`ruleChains/${this.ruleChain.id.id}`); } else { this.router.navigateByUrl(`edges/ruleChains/${this.ruleChain.id.id}`); @@ -1514,7 +1513,7 @@ export class AddRuleNodeLinkDialogComponent extends DialogComponent { if (ruleChainImport) { this.itembuffer.storeRuleChainImport(ruleChainImport); @@ -299,12 +299,12 @@ export class RuleChainsTableConfigResolver implements Resolve { this.config.componentsData.autoAssignToEdgeRuleChainIds = []; ruleChains.map(ruleChain => this.config.componentsData.autoAssignToEdgeRuleChainIds.push(ruleChain.id.id)); - return this.ruleChainService.getRuleChains(pageLink, ruleChainType.edge); + return this.ruleChainService.getRuleChains(pageLink, RuleChainType.edge); }) ); } diff --git a/ui-ngx/src/app/modules/home/pages/rulechain/rulenode.component.ts b/ui-ngx/src/app/modules/home/pages/rulechain/rulenode.component.ts index f61a7d40c3..362dc7b98c 100644 --- a/ui-ngx/src/app/modules/home/pages/rulechain/rulenode.component.ts +++ b/ui-ngx/src/app/modules/home/pages/rulechain/rulenode.component.ts @@ -19,7 +19,7 @@ import { Component, OnInit } from '@angular/core'; import { FcNodeComponent } from 'ngx-flowchart/dist/ngx-flowchart'; import { FcRuleNode, RuleNodeType } from '@shared/models/rule-node.models'; import { Router } from '@angular/router'; -import { ruleChainType } from '@app/shared/models/rule-chain.models'; +import { RuleChainType } from '@app/shared/models/rule-chain.models'; @Component({ // tslint:disable-next-line:component-selector @@ -49,7 +49,7 @@ export class RuleNodeComponent extends FcNodeComponent implements OnInit { $event.stopPropagation(); } if (node.targetRuleChainId) { - if (node.ruleChainType === ruleChainType.edge) { + if (node.ruleChainType === RuleChainType.edge) { this.router.navigateByUrl(`/edges/ruleChains/${node.targetRuleChainId}`); } else { this.router.navigateByUrl(`/ruleChains/${node.targetRuleChainId}`); diff --git a/ui-ngx/src/app/modules/home/pages/widget/widgets-bundles-table-config.resolver.ts b/ui-ngx/src/app/modules/home/pages/widget/widgets-bundles-table-config.resolver.ts index ebc563c2c4..c2672189a3 100644 --- a/ui-ngx/src/app/modules/home/pages/widget/widgets-bundles-table-config.resolver.ts +++ b/ui-ngx/src/app/modules/home/pages/widget/widgets-bundles-table-config.resolver.ts @@ -121,16 +121,14 @@ export class WidgetsBundlesTableConfigResolver implements Resolve this.isWidgetsBundleEditable(widgetsBundle, authUser.authority); this.config.detailsReadonly = (widgetsBundle) => !this.isWidgetsBundleEditable(widgetsBundle, authUser.authority); const authState = getCurrentAuthState(this.store); - if (!authState.edgesSupportEnabled) { - this.config.entitiesFetchFunction = pageLink => this.widgetsService.getWidgetBundles(pageLink).pipe( - map((widgetBundles) => { + this.config.entitiesFetchFunction = pageLink => this.widgetsService.getWidgetBundles(pageLink).pipe( + map((widgetBundles) => { + if (!authState.edgesSupportEnabled) { widgetBundles.data = widgetBundles.data.filter(widgetBundle => widgetBundle.alias !== 'edge_widgets'); - return widgetBundles; - }) - ); - } else { - this.config.entitiesFetchFunction = pageLink => this.widgetsService.getWidgetBundles(pageLink); - } + } + return widgetBundles; + }) + ); return this.config; } diff --git a/ui-ngx/src/app/shared/components/widgets-bundle-select.component.ts b/ui-ngx/src/app/shared/components/widgets-bundle-select.component.ts index 38baf05ef9..e987d19833 100644 --- a/ui-ngx/src/app/shared/components/widgets-bundle-select.component.ts +++ b/ui-ngx/src/app/shared/components/widgets-bundle-select.component.ts @@ -82,6 +82,13 @@ export class WidgetsBundleSelectComponent implements ControlValueAccessor, OnIni ngOnInit() { this.widgetsBundles$ = this.getWidgetsBundles().pipe( + map((widgetsBundles) => { + const authState = getCurrentAuthState(this.store); + if (!authState.edgesSupportEnabled) { + widgetsBundles = widgetsBundles.filter(widgetsBundle => widgetsBundle.alias !== 'edge_widgets'); + } + return widgetsBundles; + }), tap((widgetsBundles) => { this.widgetsBundles = widgetsBundles; if (this.selectFirstBundle) { @@ -95,13 +102,6 @@ export class WidgetsBundleSelectComponent implements ControlValueAccessor, OnIni } } }), - map((widgetsBundles) => { - const authState = getCurrentAuthState(this.store); - if (!authState.edgesSupportEnabled) { - widgetsBundles = widgetsBundles.filter(widgetsBundle => widgetsBundle.alias !== 'edge_widgets'); - } - return widgetsBundles; - }), share() ); } diff --git a/ui-ngx/src/app/shared/models/rule-chain.models.ts b/ui-ngx/src/app/shared/models/rule-chain.models.ts index 5fbd94d0ed..a2dc82be40 100644 --- a/ui-ngx/src/app/shared/models/rule-chain.models.ts +++ b/ui-ngx/src/app/shared/models/rule-chain.models.ts @@ -113,9 +113,7 @@ export const inputNodeComponent: RuleNodeComponentDescriptor = { clazz: 'tb.internal.Input' }; -export declare type RuleChainType = 'CORE' | 'EDGE'; - -export enum ruleChainType { +export enum RuleChainType { core = 'CORE', edge = 'EDGE' } 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 acf08d5438..379220546b 100644 --- a/ui-ngx/src/app/shared/models/rule-node.models.ts +++ b/ui-ngx/src/app/shared/models/rule-node.models.ts @@ -25,6 +25,7 @@ import { AfterViewInit, EventEmitter, Inject, OnInit, Directive } from '@angular import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { AbstractControl, FormGroup } from '@angular/forms'; +import { RuleChainType } from '@shared/models/rule-chain.models'; export interface RuleNodeConfiguration { [key: string]: any; @@ -313,7 +314,7 @@ export interface FcRuleNode extends FcRuleNodeType { error?: string; highlighted?: boolean; componentClazz?: string; - ruleChainType?: string; + ruleChainType?: RuleChainType; } export interface FcRuleEdge extends FcEdge {