Browse Source

Merge pull request #1 from volodymyr-babak/import-updates

Import updates
pull/3811/head
Artem Babak 6 years ago
committed by GitHub
parent
commit
473f0c33cf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 249
      ui-ngx/src/app/core/http/entity.service.ts
  2. 57
      ui-ngx/src/app/modules/home/components/import-export/import-dialog-csv.component.ts
  3. 6
      ui-ngx/src/app/modules/home/components/import-export/import-export.models.ts
  4. 18
      ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts
  5. 7
      ui-ngx/src/app/modules/home/pages/customer/customer.component.ts
  6. 8
      ui-ngx/src/app/modules/home/pages/edge/edge.component.ts
  7. 3
      ui-ngx/src/app/shared/models/entity.models.ts
  8. 6
      ui-ngx/src/assets/locale/locale.constant-en_US.json

249
ui-ngx/src/app/core/http/entity.service.ts

@ -14,7 +14,7 @@
/// limitations under the License.
///
import { Inject, Injectable } from '@angular/core';
import { Injectable } from '@angular/core';
import { EMPTY, forkJoin, Observable, of, throwError } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { PageLink } from '@shared/models/page/page-link';
@ -44,9 +44,15 @@ import { AliasInfo, StateParams, SubscriptionInfo } from '@core/api/widget-api.m
import { Datasource, DatasourceType, KeyInfo } from '@app/shared/models/widget.models';
import { UtilsService } from '@core/services/utils.service';
import { AliasFilterType, EntityAlias, EntityAliasFilter, EntityAliasFilterResult } from '@shared/models/alias.models';
import { entityFields, EntityInfo, ImportEntitiesResultInfo, ImportEntityData } from '@shared/models/entity.models';
import {
EdgeImportEntityData,
entityFields,
EntityInfo,
ImportEntitiesResultInfo,
ImportEntityData
} from '@shared/models/entity.models';
import { EntityRelationService } from '@core/http/entity-relation.service';
import { deepClone, generateSecret, guid, isDefined, isDefinedAndNotNull } from '@core/utils';
import { deepClone, isDefined, isDefinedAndNotNull } from '@core/utils';
import { Asset } from '@shared/models/asset.models';
import { Device, DeviceCredentialsType } from '@shared/models/device.models';
import { AttributeService } from '@core/http/attribute.service';
@ -68,10 +74,9 @@ import {
StringOperation
} from '@shared/models/query/query.models';
import { alarmFields } from '@shared/models/alarm.models';
import { EdgeService } from "@core/http/edge.service";
import { EdgeService } from '@core/http/edge.service';
import { Edge } from '@shared/models/edge.models';
import { WINDOW } from "@core/services/window.service";
import { RuleChainType } from "@shared/models/rule-chain.models";
import { RuleChainType } from '@shared/models/rule-chain.models';
@Injectable({
providedIn: 'root'
@ -92,8 +97,7 @@ export class EntityService {
private dashboardService: DashboardService,
private entityRelationService: EntityRelationService,
private attributeService: AttributeService,
private utils: UtilsService,
@Inject(WINDOW) protected window: Window
private utils: UtilsService
) { }
private getEntityObservable(entityType: EntityType, entityId: string,
@ -861,6 +865,51 @@ export class EntityService {
public saveEntityParameters(entityType: EntityType, entityData: ImportEntityData, update: boolean,
config?: RequestConfig): Observable<ImportEntitiesResultInfo> {
const saveEntityObservable: Observable<BaseData<EntityId>> = this.getSaveEntityObservable(entityType, entityData, config);
return saveEntityObservable.pipe(
mergeMap((entity) => {
return this.saveEntityData(entity.id, entityData, config).pipe(
map(() => {
return { create: { entity: 1 } } as ImportEntitiesResultInfo;
}),
catchError(err => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
}),
catchError(err => {
if (update) {
let findEntityObservable: Observable<BaseData<EntityId>>;
switch (entityType) {
case EntityType.DEVICE:
findEntityObservable = this.deviceService.findByName(entityData.name, config);
break;
case EntityType.ASSET:
findEntityObservable = this.assetService.findByName(entityData.name, config);
break;
case EntityType.EDGE:
findEntityObservable = this.edgeService.findByName(entityData.name, config);
break;
}
return findEntityObservable.pipe(
mergeMap((entity) => {
const updateEntityTasks: Observable<any>[] = this.getUpdateEntityTasks(entityType, entityData, entity, config);
return forkJoin(updateEntityTasks).pipe(
map(() => {
return { update: { entity: 1 } } as ImportEntitiesResultInfo;
}),
catchError(updateError => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
}),
catchError(findErr => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
} else {
return of({ error: { entity: 1 } } as ImportEntitiesResultInfo);
}
})
);
}
private getSaveEntityObservable(entityType: EntityType, entityData: ImportEntityData,
config?: RequestConfig): Observable<BaseData<EntityId>> {
let saveEntityObservable: Observable<BaseData<EntityId>>;
switch (entityType) {
case EntityType.DEVICE:
@ -892,127 +941,95 @@ export class EntityService {
saveEntityObservable = this.assetService.saveAsset(asset, config);
break;
case EntityType.EDGE:
const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
const edge: Edge = {
name: entityData.name,
type: entityData.type,
label: entityData.label,
name: edgeEntityData.name,
type: edgeEntityData.type,
label: edgeEntityData.label,
additionalInfo: {
description: entityData.description
description: edgeEntityData.description
},
edgeLicenseKey: entityData.edgeLicenseKey,
cloudEndpoint: entityData.cloudEndpoint,
routingKey: entityData.routingKey,
secret: entityData.secret
edgeLicenseKey: edgeEntityData.edgeLicenseKey,
cloudEndpoint: edgeEntityData.cloudEndpoint,
routingKey: edgeEntityData.routingKey,
secret: edgeEntityData.secret
};
if (edge.cloudEndpoint === '') {
edge.cloudEndpoint = this.window.location.origin;
}
if (edge.routingKey === '') {
edge.routingKey = guid();
}
if (edge.secret === '') {
edge.secret = generateSecret(20);
}
saveEntityObservable = this.edgeService.saveEdge(edge, config);
break;
}
return saveEntityObservable.pipe(
mergeMap((entity) => {
return this.saveEntityData(entity.id, entityData, config).pipe(
map(() => {
return { create: { entity: 1 } } as ImportEntitiesResultInfo;
}),
catchError(err => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
}),
catchError(err => {
if (update) {
let findEntityObservable: Observable<BaseData<EntityId>>;
switch (entityType) {
return saveEntityObservable;
}
private getUpdateEntityTasks(entityType: EntityType, entityData: ImportEntityData | EdgeImportEntityData,
entity: BaseData<EntityId>, config?: RequestConfig): Observable<any>[] {
const tasks: Observable<any>[] = [];
let result;
let additionalInfo;
switch (entityType) {
case EntityType.EDGE:
result = entity as Edge;
additionalInfo = result.additionalInfo || {};
const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
if (result.label !== edgeEntityData.label ||
result.type !== edgeEntityData.type ||
result.cloudEndpoint !== edgeEntityData.cloudEndpoint ||
result.edgeLicenseKey !== edgeEntityData.edgeLicenseKey ||
result.routingKey !== edgeEntityData.routingKey ||
result.secret !== edgeEntityData.secret ||
additionalInfo.description !== edgeEntityData.description) {
result.type = edgeEntityData.type;
if (edgeEntityData.label !== '') {
result.label = edgeEntityData.label;
}
if (edgeEntityData.description !== '') {
result.additionalInfo = additionalInfo;
result.additionalInfo.description = edgeEntityData.description;
}
if (edgeEntityData.cloudEndpoint !== '') {
result.cloudEndpoint = edgeEntityData.cloudEndpoint;
}
if (edgeEntityData.edgeLicenseKey !== '') {
result.edgeLicenseKey = edgeEntityData.edgeLicenseKey;
}
if (edgeEntityData.routingKey !== '') {
result.routingKey = edgeEntityData.routingKey;
}
if (edgeEntityData.cloudEndpoint !== '') {
result.secret = edgeEntityData.secret;
}
tasks.push(this.edgeService.saveEdge(result, config));
}
tasks.push(this.saveEntityData(entity.id, edgeEntityData, config));
break;
case EntityType.ASSET:
case EntityType.DEVICE:
result = entity as (Device | Asset);
additionalInfo = result.additionalInfo || {};
if (result.label !== entityData.label ||
result.type !== entityData.type ||
additionalInfo.description !== entityData.description ||
(result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
result.label = entityData.label;
result.type = entityData.type;
result.additionalInfo = additionalInfo;
result.additionalInfo.description = entityData.description;
if (result.id.entityType === EntityType.DEVICE) {
result.additionalInfo.gateway = entityData.gateway;
}
switch (result.id.entityType) {
case EntityType.DEVICE:
findEntityObservable = this.deviceService.findByName(entityData.name, config);
tasks.push(this.deviceService.saveDevice(result, config));
break;
case EntityType.ASSET:
findEntityObservable = this.assetService.findByName(entityData.name, config);
break;
case EntityType.EDGE:
findEntityObservable = this.edgeService.findByName(entityData.name, config);
tasks.push(this.assetService.saveAsset(result, config));
break;
}
return findEntityObservable.pipe(
mergeMap((entity) => {
const tasks: Observable<any>[] = [];
if (entity.id.entityType === EntityType.EDGE) {
const result: Edge = entity as Edge;
const additionalInfo = result.additionalInfo || {};
if (result.label !== entityData.label ||
result.type !== entityData.type ||
result.cloudEndpoint !== entityData.cloudEndpoint ||
result.edgeLicenseKey !== entityData.edgeLicenseKey ||
result.routingKey !== entityData.routingKey ||
result.secret !== entityData.secret ||
additionalInfo.description !== entityData.description) {
result.type = entityData.type;
if (entityData.label !== '') {
result.label = entityData.label;
}
if (entityData.description !== '') {
result.additionalInfo = additionalInfo;
result.additionalInfo.description = entityData.description;
}
if (entityData.cloudEndpoint !== '') {
result.cloudEndpoint = entityData.cloudEndpoint;
}
if (entityData.edgeLicenseKey !== '') {
result.edgeLicenseKey = entityData.edgeLicenseKey;
}
if (entityData.routingKey !== '') {
result.routingKey = entityData.routingKey;
}
if (entityData.cloudEndpoint !== '') {
result.secret = entityData.secret;
}
tasks.push(this.edgeService.saveEdge(result, config));
}
} else {
const result: Device | Asset = entity as (Device | Asset);
const additionalInfo = result.additionalInfo || {};
if (result.label !== entityData.label ||
result.type !== entityData.type ||
additionalInfo.description !== entityData.description ||
(result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
result.label = entityData.label;
result.type = entityData.type;
result.additionalInfo = additionalInfo;
result.additionalInfo.description = entityData.description;
if (result.id.entityType === EntityType.DEVICE) {
result.additionalInfo.gateway = entityData.gateway;
}
switch (result.id.entityType) {
case EntityType.DEVICE:
tasks.push(this.deviceService.saveDevice(result, config));
break;
case EntityType.ASSET:
tasks.push(this.assetService.saveAsset(result, config));
break;
}
}
}
tasks.push(this.saveEntityData(entity.id, entityData, config));
return forkJoin(tasks).pipe(
map(() => {
return { update: { entity: 1 } } as ImportEntitiesResultInfo;
}),
catchError(updateError => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
}),
catchError(findErr => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
} else {
return of({ error: { entity: 1 } } as ImportEntitiesResultInfo);
}
})
);
tasks.push(this.saveEntityData(entity.id, entityData, config));
break;
}
return tasks;
}
public saveEntityData(entityId: EntityId, entityData: ImportEntityData, config?: RequestConfig): Observable<any> {

57
ui-ngx/src/app/modules/home/components/import-export/import-dialog-csv.component.ts

@ -32,8 +32,9 @@ import {
CsvToJsonResult,
ImportEntityColumnType
} from '@home/components/import-export/import-export.models';
import { ImportEntitiesResultInfo, ImportEntityData } from '@app/shared/models/entity.models';
import { EdgeImportEntityData, ImportEntitiesResultInfo, ImportEntityData } from '@app/shared/models/entity.models';
import { ImportExportService } from '@home/components/import-export/import-export.service';
import { generateSecret, guid } from '@core/utils';
export interface ImportDialogCsvData {
entityType: EntityType;
@ -186,29 +187,14 @@ export class ImportDialogCsvComponent extends DialogComponent<ImportDialogCsvCom
return columnsParam;
}
private addEntities() {
const importData = this.parseData;
const parameterColumns: CsvColumnParam[] = this.columnTypesFormGroup.get('columnsParam').value;
const entitiesData: ImportEntityData[] = [];
let sentDataLength = 0;
for (let row = 0; row < importData.rows.length; row++) {
const entityData: ImportEntityData = {
name: '',
type: '',
description: '',
gateway: null,
label: '',
accessToken: '',
attributes: {
server: [],
shared: []
},
timeseries: [],
edgeLicenseKey: '',
cloudEndpoint: '',
routingKey: '',
secret: ''
};
const entityData: ImportEntityData = this.constructDraftImportEntityData();
const i = row;
for (let j = 0; j < parameterColumns.length; j++) {
switch (parameterColumns[j].type) {
@ -249,16 +235,16 @@ export class ImportDialogCsvComponent extends DialogComponent<ImportDialogCsvCom
entityData.description = importData.rows[i][j];
break;
case ImportEntityColumnType.edgeLicenseKey:
entityData.edgeLicenseKey = importData.rows[i][j];
(entityData as EdgeImportEntityData).edgeLicenseKey = importData.rows[i][j];
break;
case ImportEntityColumnType.cloudEndpoint:
entityData.cloudEndpoint = importData.rows[i][j];
(entityData as EdgeImportEntityData).cloudEndpoint = importData.rows[i][j];
break;
case ImportEntityColumnType.routingKey:
entityData.routingKey = importData.rows[i][j];
(entityData as EdgeImportEntityData).routingKey = importData.rows[i][j];
break;
case ImportEntityColumnType.secret:
entityData.secret = importData.rows[i][j];
(entityData as EdgeImportEntityData).secret = importData.rows[i][j];
break;
}
}
@ -281,4 +267,31 @@ export class ImportDialogCsvComponent extends DialogComponent<ImportDialogCsvCom
);
}
private constructDraftImportEntityData(): ImportEntityData {
const entityData: ImportEntityData = {
name: '',
type: '',
description: '',
gateway: null,
label: '',
accessToken: '',
attributes: {
server: [],
shared: []
},
timeseries: []
};
if (this.entityType === EntityType.EDGE) {
const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
edgeEntityData.edgeLicenseKey = '';
edgeEntityData.cloudEndpoint = window.location.origin;
edgeEntityData.routingKey = guid();
edgeEntityData.secret = generateSecret(20);
return edgeEntityData;
} else {
return entityData;
}
}
}

6
ui-ngx/src/app/modules/home/components/import-export/import-export.models.ts

@ -72,9 +72,9 @@ export const importEntityColumnTypeTranslations = new Map<ImportEntityColumnType
[ImportEntityColumnType.accessToken, 'import.column-type.access-token'],
[ImportEntityColumnType.isGateway, 'import.column-type.isgateway'],
[ImportEntityColumnType.description, 'import.column-type.description'],
[ImportEntityColumnType.edgeLicenseKey, 'import.column-type.edgeLicenseKey'],
[ImportEntityColumnType.cloudEndpoint, 'import.column-type.cloudEndpoint'],
[ImportEntityColumnType.routingKey, 'import.column-type.routingKey'],
[ImportEntityColumnType.edgeLicenseKey, 'import.column-type.edge-license-key'],
[ImportEntityColumnType.cloudEndpoint, 'import.column-type.cloud-endpoint'],
[ImportEntityColumnType.routingKey, 'import.column-type.routing-key'],
[ImportEntityColumnType.secret, 'import.column-type.secret']
]
);

18
ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts

@ -52,12 +52,7 @@ import { NULL_UUID } from '@shared/models/id/has-uuid';
import { WidgetsBundle } from '@shared/models/widgets-bundle.model';
import { ImportEntitiesResultInfo, ImportEntityData } from '@shared/models/entity.models';
import { RequestConfig } from '@core/http/http-utils';
import {
RuleChain,
RuleChainImport,
RuleChainMetaData,
RuleChainType
} from '@shared/models/rule-chain.models';
import { RuleChain, RuleChainImport, RuleChainMetaData, RuleChainType } from '@shared/models/rule-chain.models';
import { RuleChainService } from '@core/http/rule-chain.service';
import * as JSZip from 'jszip';
import { FiltersInfo } from '@shared/models/query/query.models';
@ -349,8 +344,9 @@ export class ImportExportService {
let statisticalInfo: ImportEntitiesResultInfo = {};
const importEntitiesObservables: Observable<ImportEntitiesResultInfo>[] = [];
for (let i = 0; i < partSize; i++) {
const importEntityPromise =
this.entityService.saveEntityParameters(entityType, entitiesData[i], updateData, config).pipe(
let saveEntityPromise: Observable<ImportEntitiesResultInfo>;
saveEntityPromise = this.entityService.saveEntityParameters(entityType, entitiesData[i], updateData, config);
const importEntityPromise = saveEntityPromise.pipe(
tap((res) => {
if (importEntityCompleted) {
importEntityCompleted();
@ -412,7 +408,7 @@ export class ImportExportService {
throw new Error('Invalid rule chain file');
} else if (ruleChainImport.ruleChain.type !== expectedRuleChainType) {
this.store.dispatch(new ActionNotificationShow(
{message: this.translate.instant('rulechain.invalid-rulechain-type-error', { expectedRuleChainType: expectedRuleChainType }),
{message: this.translate.instant('rulechain.invalid-rulechain-type-error', {expectedRuleChainType}),
type: 'error'}));
throw new Error('Invalid rule chain type');
} else {
@ -602,7 +598,7 @@ export class ImportExportService {
private editMissingAliases(widgets: Array<Widget>, isSingleWidget: boolean,
customTitle: string, missingEntityAliases: EntityAliases): Observable<EntityAliases> {
let allowedEntityTypes: Array<EntityType | AliasEntityType> =
const allowedEntityTypes: Array<EntityType | AliasEntityType> =
this.entityService.prepareAllowedEntityTypesList(null, true);
return this.dialog.open<EntityAliasesDialogComponent, EntityAliasesDialogData,
@ -615,7 +611,7 @@ export class ImportExportService {
customTitle,
isSingleWidget,
disableAdd: true,
allowedEntityTypes: allowedEntityTypes
allowedEntityTypes
}
}).afterClosed().pipe(
map((updatedEntityAliases) => {

7
ui-ngx/src/app/modules/home/pages/customer/customer.component.ts

@ -24,6 +24,7 @@ import { TranslateService } from '@ngx-translate/core';
import { ContactBasedComponent } from '../../components/entity/contact-based.component';
import { EntityTableConfig } from '@home/models/entity/entities-table-config.models';
import { getCurrentAuthState } from '@core/auth/auth.selectors';
import { AuthState } from '@core/auth/auth.models';
@Component({
selector: 'tb-customer',
@ -33,6 +34,8 @@ export class CustomerComponent extends ContactBasedComponent<Customer> {
isPublic = false;
authState: AuthState = getCurrentAuthState(this.store);
constructor(protected store: Store<AppState>,
protected translate: TranslateService,
@Inject('entity') protected entityValue: Customer,
@ -80,8 +83,6 @@ export class CustomerComponent extends ContactBasedComponent<Customer> {
}
edgesSupportEnabled() {
const authState = getCurrentAuthState(this.store);
return authState.edgesSupportEnabled;
return this.authState.edgesSupportEnabled;
}
}

8
ui-ngx/src/app/modules/home/pages/edge/edge.component.ts

@ -26,7 +26,6 @@ import { NULL_UUID } from '@shared/models/id/has-uuid';
import { ActionNotificationShow } from '@core/notification/notification.actions';
import { generateSecret, guid } from '@core/utils';
import { EntityTableConfig } from '@home/models/entity/entities-table-config.models';
import { WINDOW } from '@core/services/window.service';
@Component({
selector: 'tb-edge',
@ -43,15 +42,14 @@ export class EdgeComponent extends EntityComponent<EdgeInfo> {
protected translate: TranslateService,
@Inject('entity') protected entityValue: EdgeInfo,
@Inject('entitiesTableConfig') protected entitiesTableConfigValue: EntityTableConfig<EdgeInfo>,
public fb: FormBuilder,
@Inject(WINDOW) protected window: Window) {
public fb: FormBuilder) {
super(store, fb, entityValue, entitiesTableConfigValue);
}
ngOnInit() {
this.edgeScope = this.entitiesTableConfig.componentsData.edgeScope;
this.entityForm.patchValue({
cloudEndpoint: this.window.location.origin
cloudEndpoint: window.location.origin
});
super.ngOnInit();
}
@ -94,7 +92,7 @@ export class EdgeComponent extends EntityComponent<EdgeInfo> {
name: entity.name,
type: entity.type,
label: entity.label,
cloudEndpoint: entity.cloudEndpoint ? entity.cloudEndpoint : this.window.location.origin,
cloudEndpoint: entity.cloudEndpoint ? entity.cloudEndpoint : window.location.origin,
edgeLicenseKey: entity.edgeLicenseKey,
routingKey: entity.routingKey,
secret: entity.secret,

3
ui-ngx/src/app/shared/models/entity.models.ts

@ -43,6 +43,9 @@ export interface ImportEntityData {
shared: AttributeData[]
};
timeseries: AttributeData[];
}
export interface EdgeImportEntityData extends ImportEntityData {
secret: string;
routingKey: string;
cloudEndpoint: string;

6
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -1981,9 +1981,9 @@
"access-token": "Access token",
"isgateway": "Is Gateway",
"description": "Description",
"edgeLicenseKey": "License Key",
"cloudEndpoint": "Cloud Endpoint",
"routingKey": "Edge key",
"edge-license-key": "License Key",
"cloud-endpoint": "Cloud Endpoint",
"routing-key": "Edge key",
"secret": "Edge secret"
},
"stepper-text":{

Loading…
Cancel
Save