Browse Source

Merge pull request #16732 from thingsboard/rc

Merge rc into master
pull/16744/head
Vladyslav Prykhodko 3 weeks ago
committed by GitHub
parent
commit
1b7dee3e0b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 19
      ui-ngx/src/app/modules/home/dialogs/home-dialogs.service.ts
  2. 5
      ui-ngx/src/app/modules/home/pages/dashboard/dashboard-group-config.factory.ts
  3. 4
      ui-ngx/src/app/modules/home/pages/dashboard/dashboard.module.ts
  4. 5
      ui-ngx/src/app/modules/home/pages/dashboard/dashboards-table-config.resolver.ts
  5. 50
      ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.html
  6. 85
      ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.ts
  7. 102
      ui-ngx/src/app/shared/import-export/import-export.service.ts

19
ui-ngx/src/app/modules/home/dialogs/home-dialogs.service.ts

@ -36,11 +36,6 @@ import {
EntityGroupWizardDialogData,
EntityGroupWizardDialogResult
} from '@home/components/wizard/entity-group-wizard-dialog.component';
import { DashboardInfo } from '@shared/models/dashboard.models';
import {
DashboardInfoDialogData,
ImportDashboardFileDialogComponent
} from '@home/pages/dashboard/import-dashboard-file-dialog.component';
@Injectable()
export class HomeDialogsService {
@ -141,20 +136,6 @@ export class HomeDialogsService {
}).afterClosed();
}
public importDashboardFile($event: Event, dashboard: DashboardInfo): Observable<boolean> {
if ($event) {
$event.stopPropagation();
}
return this.dialog.open<ImportDashboardFileDialogComponent, DashboardInfoDialogData,
boolean>(ImportDashboardFileDialogComponent, {
disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: {
dashboard
}
}).afterClosed();
}
public selectOwner($event: Event, selectOwnerTitle: string, confirmSelectTitle: string,
placeholderText: string, notFoundText: string, requiredText: string,
onOwnerSelected?: (targetOwnerId: EntityId) => Observable<boolean>,

5
ui-ngx/src/app/modules/home/pages/dashboard/dashboard-group-config.factory.ts

@ -254,7 +254,10 @@ export class DashboardGroupConfigFactory implements EntityGroupStateConfigFactor
}
importDashboardFile($event: Event, dashboard: DashboardInfo, config: GroupEntityTableConfig<DashboardInfo>) {
this.homeDialogs.importDashboardFile($event, dashboard).subscribe(
if ($event) {
$event.stopPropagation();
}
this.importExport.importDashboardVersion(dashboard, this.editMissingAliases.bind(this)).subscribe(
(res) => {
if (res) {
config.updateData();

4
ui-ngx/src/app/modules/home/pages/dashboard/dashboard.module.ts

@ -12,14 +12,12 @@ import { DASHBOARD_GROUP_CONFIG_FACTORY } from '@home/models/group/group-entitie
import { DashboardGroupConfigFactory } from '@home/pages/dashboard/dashboard-group-config.factory';
import { DashboardRoutingModule } from '@home/pages/dashboard/dashboard-routing.module';
import { DashboardTableHeaderComponent } from '@home/pages/dashboard/dashboard-table-header.component';
import { ImportDashboardFileDialogComponent } from "@home/pages/dashboard/import-dashboard-file-dialog.component";
@NgModule({
declarations: [
DashboardFormComponent,
DashboardTableHeaderComponent,
PublicDashboardLinkDialogComponent,
ImportDashboardFileDialogComponent
PublicDashboardLinkDialogComponent
],
imports: [
CommonModule,

5
ui-ngx/src/app/modules/home/pages/dashboard/dashboards-table-config.resolver.ts

@ -286,7 +286,10 @@ export class DashboardsTableConfigResolver {
}
importDashboardFile($event: Event, dashboard: DashboardInfo, config: EntityTableConfig<DashboardInfo>) {
this.homeDialogs.importDashboardFile($event, dashboard).subscribe(
if ($event) {
$event.stopPropagation();
}
this.importExport.importDashboardVersion(dashboard, this.editMissingAliases.bind(this)).subscribe(
(res) => {
if (res) {
config.updateData();

50
ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.html

@ -1,50 +0,0 @@
<!--
SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
-->
<form [formGroup]="uploadFileFormGroup" (ngSubmit)="save()" style="width: 800px;">
<mat-toolbar color="tb-primary" class="tb-bottom-border">
<h2>{{ 'dashboard.update-new-version' | translate }}</h2>
<span class="flex-1"></span>
<button mat-icon-button
(click)="cancel()"
type="button">
<mat-icon class="material-icons">close</mat-icon>
</button>
</mat-toolbar>
@if (isLoading$ | async) {
<mat-progress-bar color="warn" mode="indeterminate">
</mat-progress-bar>
} @else {
<div style="height: 4px;"></div>
}
<div mat-dialog-content>
<tb-file-input [contentConvertFunction]="loadDataFromJsonContent"
[existingFileName]="currentFileName"
(fileNameChanged)="currentFileName = $event"
label="{{'dashboard.upload-file-to-update' | translate}}"
formControlName="file"
dropLabel="{{ 'import.drop-json-file-or' | translate }}"
accept=".json,application/json"
allowedExtensions="json">
</tb-file-input>
</div>
<div mat-dialog-actions class="flex flex-row items-center justify-end">
<button mat-button color="primary"
type="button"
[disabled]="(isLoading$ | async)"
(click)="cancel()" cdkFocusInitial>
{{ 'action.cancel' | translate }}
</button>
<button mat-raised-button color="primary"
type="submit"
[disabled]="(isLoading$ | async) || uploadFileFormGroup.invalid
|| !uploadFileFormGroup.dirty">
{{ 'action.save' | translate }}
</button>
</div>
</form>

85
ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.ts

@ -1,85 +0,0 @@
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { FormBuilder, FormGroup } from '@angular/forms';
import { DashboardService } from '@core/http/dashboard.service';
import { DashboardInfo } from '@app/shared/models/dashboard.models';
import { ActionNotificationShow } from '@core/notification/notification.actions';
import { DialogComponent } from '@shared/components/dialog.component';
import { Router } from '@angular/router';
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
export interface DashboardInfoDialogData {
dashboard: DashboardInfo;
}
@Component({
selector: 'tb-import-dashboard-file-dialog',
templateUrl: './import-dashboard-file-dialog.component.html',
styleUrls: [],
standalone: false
})
export class ImportDashboardFileDialogComponent extends DialogComponent<ImportDashboardFileDialogComponent> implements OnInit {
private dashboard: DashboardInfo;
currentFileName: string = '';
uploadFileFormGroup: FormGroup;
constructor(protected store: Store<AppState>,
protected router: Router,
@Inject(MAT_DIALOG_DATA) public data: DashboardInfoDialogData,
private dashboardService: DashboardService,
protected dialogRef: MatDialogRef<ImportDashboardFileDialogComponent>,
private fb: FormBuilder) {
super(store, router, dialogRef);
this.dashboard = data.dashboard;
}
ngOnInit(): void {
this.uploadFileFormGroup = this.fb.group({
file: [null]
});
}
cancel(): void {
this.dialogRef.close();
}
save() {
const fileControl = this.uploadFileFormGroup.get('file');
if (!fileControl || !fileControl.value) {
return;
}
const dashboard: DashboardInfo = {
...this.dashboard,
configuration: {
...fileControl.value,
description: this.dashboard.configuration.description
}
};
this.dashboardService.saveDashboard(dashboard).subscribe({
next: () => this.dialogRef.close(true),
error: (err: HttpErrorResponse) => {
if (err.status === HttpStatusCode.Conflict) {
this.dialogRef.close(true);
}
}
});
}
loadDataFromJsonContent(content: string): any {
try {
const importData = JSON.parse(content);
return importData ? importData['configuration'] : importData;
} catch (err) {
this.store.dispatch(new ActionNotificationShow({message: err.message, type: 'error'}));
return null;
}
}
}

102
ui-ngx/src/app/shared/import-export/import-export.service.ts

@ -7,9 +7,9 @@ import { TranslateService } from '@ngx-translate/core';
import { select, Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { ActionNotificationShow } from '@core/notification/notification.actions';
import { BreakpointId, Dashboard, DashboardLayoutId } from '@shared/models/dashboard.models';
import { BreakpointId, Dashboard, DashboardInfo, DashboardLayoutId } from '@shared/models/dashboard.models';
import { DatePipe } from '@angular/common';
import { deepClone, guid, isDate, isDefined, isNotEmptyStr, isNumber, isObject, isString, isUndefined, unwrapModule } from '@core/utils';
import { deepClone, guid, isDate, isDefined, isNotEmptyStr, isObject, isString, isUndefined, unwrapModule } from '@core/utils';
import {
AliasesInfo,
@ -22,7 +22,8 @@ import {
import { MatDialog } from '@angular/material/dialog';
import { ImportDialogComponent, ImportDialogData } from '@shared/import-export/import-dialog.component';
import { forkJoin, Observable, of, Subject } from 'rxjs';
import { catchError, map, mergeMap, switchMap, take, tap } from 'rxjs/operators';
import { catchError, defaultIfEmpty, map, mergeMap, switchMap, take, tap } from 'rxjs/operators';
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
import { DashboardUtilsService } from '@core/services/dashboard-utils.service';
import { EntityService } from '@core/http/entity.service';
import { ExportRow, Widget, WidgetActionsMap, WidgetActionType, WidgetSize, WidgetTypeDetails } from '@shared/models/widget.models';
@ -266,48 +267,33 @@ export class ImportExportService {
onEditMissingAliases: editMissingAliasesFunction, entityGroupId?: string): Observable<Dashboard> {
return this.openImportDialog('dashboard.import', 'dashboard.dashboard-file').pipe(
mergeMap((dashboard: Dashboard) => {
if (!this.validateImportedDashboard(dashboard)) {
this.store.dispatch(new ActionNotificationShow(
{message: this.translate.instant('dashboard.invalid-dashboard-file-error'),
type: 'error'}));
throw new Error('Invalid dashboard file');
} else {
dashboard = this.prepareImport(dashboard);
dashboard = this.dashboardUtils.validateAndUpdateDashboard(dashboard);
dashboard.customerId = customerId;
let aliasIds = null;
const entityAliases = dashboard.configuration.entityAliases;
if (entityAliases) {
aliasIds = Object.keys(entityAliases);
}
if (aliasIds && aliasIds.length > 0) {
return this.processEntityAliases(entityAliases, aliasIds).pipe(
mergeMap((missingEntityAliases) => {
if (Object.keys(missingEntityAliases).length > 0) {
return onEditMissingAliases(this.dashboardUtils.getWidgetsArray(dashboard),
false, 'dashboard.dashboard-import-missing-aliases-title',
missingEntityAliases).pipe(
mergeMap((updatedEntityAliases) => {
for (const aliasId of Object.keys(updatedEntityAliases)) {
entityAliases[aliasId] = updatedEntityAliases[aliasId];
}
return this.saveImportedDashboard(dashboard, entityGroupId);
})
);
} else {
return this.saveImportedDashboard(dashboard, entityGroupId);
}
}
));
} else {
return this.saveImportedDashboard(dashboard, entityGroupId);
}
}
dashboard = this.prepareImportedDashboard(dashboard);
dashboard.customerId = customerId;
return this.resolveMissingAliases(dashboard, onEditMissingAliases);
}),
mergeMap((dashboard) => this.saveImportedDashboard(dashboard, entityGroupId)),
catchError(() => of(null))
);
}
public importDashboardVersion(dashboard: DashboardInfo,
onEditMissingAliases: editMissingAliasesFunction): Observable<boolean> {
return this.openImportDialog('dashboard.update-new-version', 'dashboard.upload-file-to-update').pipe(
mergeMap((imported: Dashboard) =>
this.resolveMissingAliases(this.prepareImportedDashboard(imported), onEditMissingAliases)),
mergeMap((imported) => this.dashboardService.saveDashboard({
...dashboard,
configuration: {...imported.configuration, description: dashboard.configuration?.description},
resources: imported.resources
}).pipe(
map(() => true),
defaultIfEmpty(true),
catchError((err) => of(err instanceof HttpErrorResponse && err.status === HttpStatusCode.Conflict))
)),
catchError(() => of(false))
);
}
public exportWidget(dashboard: Dashboard, sourceState: string, sourceLayout: DashboardLayoutId, widget: Widget,
widgetTitle: string, breakpoint: BreakpointId) {
const widgetItem = this.itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget, breakpoint);
@ -1315,10 +1301,40 @@ export class ImportExportService {
}
private validateImportedDashboard(dashboard: Dashboard): boolean {
if (isUndefined(dashboard.title) || isUndefined(dashboard.configuration)) {
return false;
return isObject(dashboard) && !Array.isArray(dashboard) && isDefined(dashboard.title)
&& isObject(dashboard.configuration) && !Array.isArray(dashboard.configuration);
}
private prepareImportedDashboard(dashboard: Dashboard): Dashboard {
if (!this.validateImportedDashboard(dashboard)) {
this.store.dispatch(new ActionNotificationShow(
{message: this.translate.instant('dashboard.invalid-dashboard-file-error'),
type: 'error'}));
throw new Error('Invalid dashboard file');
}
return true;
return this.dashboardUtils.validateAndUpdateDashboard(this.prepareImport(dashboard));
}
private resolveMissingAliases(dashboard: Dashboard, onEditMissingAliases: editMissingAliasesFunction): Observable<Dashboard> {
const entityAliases = dashboard.configuration.entityAliases;
const aliasIds = entityAliases ? Object.keys(entityAliases) : [];
if (!aliasIds.length) {
return of(dashboard);
}
return this.processEntityAliases(entityAliases, aliasIds).pipe(
mergeMap((missingEntityAliases) => {
if (!Object.keys(missingEntityAliases).length) {
return of(dashboard);
}
return onEditMissingAliases(this.dashboardUtils.getWidgetsArray(dashboard),
false, 'dashboard.dashboard-import-missing-aliases-title', missingEntityAliases).pipe(
map((updatedEntityAliases) => {
Object.assign(entityAliases, updatedEntityAliases);
return dashboard;
})
);
})
);
}
private validateImportedWidget(widgetItem: WidgetItem): boolean {

Loading…
Cancel
Save