-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add app builder dashboard migration button plugin (#55)
- Loading branch information
Showing
9 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,5 +100,8 @@ | |
} | ||
} | ||
} | ||
}, | ||
"cli": { | ||
"analytics": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/app/app-builder-dashboard-migration/app-builder-dashboard-migration-button.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { ActionBarItem, ExtensionFactory } from '@c8y/ngx-components'; | ||
import { AppBuilderDashboardMigrationButtonComponent } from './app-builder-dashboard-migration-button/app-builder-dashboard-migration-button.component'; | ||
import { Observable, of } from 'rxjs'; | ||
import { distinctUntilChanged, switchMap } from 'rxjs/operators'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AppBuilderDashboardMigrationButtonService | ||
implements ExtensionFactory<ActionBarItem> | ||
{ | ||
get( | ||
activatedRoute?: ActivatedRoute | ||
): Observable<ActionBarItem | ActionBarItem[]> { | ||
return (activatedRoute?.url || of([])).pipe( | ||
switchMap(async (url) => { | ||
if (url?.length !== 1 || url?.[0].path !== 'reports') { | ||
return false; | ||
} | ||
|
||
return true; | ||
}), | ||
distinctUntilChanged(), | ||
switchMap(async (show) => { | ||
if (!show) { | ||
return []; | ||
} | ||
|
||
return { | ||
component: AppBuilderDashboardMigrationButtonComponent, | ||
placement: 'right' as const, | ||
}; | ||
}) | ||
); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-builder-dashboard-migration-button/app-builder-dashboard-migration-button.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<button | ||
type="button" | ||
class="btn btn-link" | ||
(click)="migrateDashboards()" | ||
[disabled]="!dashboards?.length" | ||
> | ||
<i c8yIcon="process"></i> | ||
Migrate application builder dashboards | ||
</button> |
34 changes: 34 additions & 0 deletions
34
...pp-builder-dashboard-migration-button/app-builder-dashboard-migration-button.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { AlertService, IconDirective } from '@c8y/ngx-components'; | ||
import { AppBuilderDashboardMigrationService } from '../app-builder-dashboard-migration.service'; | ||
import { IManagedObject } from '@c8y/client'; | ||
|
||
@Component({ | ||
selector: 'li[app-app-builder-dashboard-migration-button]', | ||
templateUrl: './app-builder-dashboard-migration-button.component.html', | ||
standalone: true, | ||
imports: [IconDirective], | ||
}) | ||
export class AppBuilderDashboardMigrationButtonComponent implements OnInit { | ||
dashboards: IManagedObject[] = []; | ||
constructor( | ||
private alert: AlertService, | ||
private migrationService: AppBuilderDashboardMigrationService | ||
) {} | ||
|
||
async ngOnInit(): Promise<void> { | ||
this.dashboards = | ||
await this.migrationService.getNotMigratedAppBuilderDashboards(); | ||
} | ||
|
||
async migrateDashboards() { | ||
this.alert.info('Migrating dashboards...'); | ||
for (const dashboard of this.dashboards) { | ||
await this.migrationService.migrateDashboard(dashboard); | ||
} | ||
this.alert.success( | ||
'Dashboards migrated successfully, please reload the page.' | ||
); | ||
this.dashboards = []; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/app/app-builder-dashboard-migration/app-builder-dashboard-migration.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { hookActionBar } from '@c8y/ngx-components'; | ||
import { AppBuilderDashboardMigrationButtonService } from './app-builder-dashboard-migration-button.service'; | ||
|
||
@NgModule({ | ||
providers: [hookActionBar(AppBuilderDashboardMigrationButtonService)], | ||
}) | ||
export class AppBuilderDashboardMigrationModule {} |
53 changes: 53 additions & 0 deletions
53
src/app/app-builder-dashboard-migration/app-builder-dashboard-migration.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { IManagedObject, InventoryService } from '@c8y/client'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AppBuilderDashboardMigrationService { | ||
readonly reportPrefix = 'c8y_Dashboard!name!report_'; | ||
readonly identifier = 'migratedAppBuilderDashboardsToReport'; | ||
|
||
constructor(private inventory: InventoryService) {} | ||
|
||
async getNotMigratedAppBuilderDashboards() { | ||
const { data: dashboards } = await this.inventory.list({ | ||
fragmentType: 'c8y_Dashboard', | ||
pageSize: 2000, | ||
}); | ||
|
||
const noReportDashboards = dashboards.filter( | ||
(dashboard) => | ||
!Object.keys(dashboard).some((key) => key.startsWith(this.reportPrefix)) | ||
); | ||
|
||
const dashboardsWithMarker = noReportDashboards.filter((dashboard) => | ||
Object.keys(dashboard).some( | ||
(key) => | ||
key.startsWith('global!presales!') || | ||
key === 'c8y_Dashboard!name!app-builder-db' | ||
) | ||
); | ||
return dashboardsWithMarker; | ||
} | ||
|
||
async migrateDashboard(dashboard: IManagedObject) { | ||
const { id, c8y_Dashboard: dashboardDetails } = dashboard; | ||
const { name, icon, priority } = dashboardDetails; | ||
|
||
const { data: reportObject } = await this.inventory.create({ | ||
name: name || 'Unnamed app builder dashboard', | ||
icon, | ||
priority, | ||
description: null, | ||
c8y_Report: {}, | ||
[this.identifier]: {}, | ||
}); | ||
|
||
await this.inventory.update({ | ||
id, | ||
[`${this.reportPrefix}${reportObject.id}`]: {}, | ||
[this.identifier]: {}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters