Skip to content

Commit 22145aa

Browse files
committed
avoid linear search for clustersnames, avoid nested subscribed, rely on forkJoins instead
Signed-off-by: Mathias Petermann <[email protected]>
1 parent 5bc623a commit 22145aa

File tree

5 files changed

+88
-109
lines changed

5 files changed

+88
-109
lines changed

modules/web/src/app/backup/details/automatic-backup/component.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import {Project} from '@shared/entity/project';
2828
import {GroupConfig} from '@shared/model/Config';
2929
import {MemberUtils, Permission} from '@shared/utils/member';
3030
import _ from 'lodash';
31-
import {Subject} from 'rxjs';
32-
import {filter, map, switchMap, take, takeUntil} from 'rxjs/operators';
33-
import { Cluster } from '@app/shared/entity/cluster';
31+
import {forkJoin, of, Subject} from 'rxjs';
32+
import {filter, switchMap, take, takeUntil} from 'rxjs/operators';
33+
import {Cluster} from '@app/shared/entity/cluster';
3434

3535
@Component({
3636
selector: 'km-automatic-backup-details',
@@ -77,25 +77,23 @@ export class AutomaticBackupDetailsComponent implements OnInit, OnDestroy {
7777

7878
this._projectService.selectedProject
7979
.pipe(
80-
switchMap(project => {
81-
this.selectedProject = project;
82-
return this._userService.getCurrentUserGroup(project.id);
83-
})
80+
switchMap(project =>
81+
forkJoin({
82+
userGroup: this._userService.getCurrentUserGroup(project.id).pipe(take(1)),
83+
backups: this._backupService.list(project.id, false).pipe(take(1)),
84+
clusters: this._clusterService.clusters(project.id, false).pipe(take(1)),
85+
project: of(project),
86+
})
87+
),
8488
)
8589
.pipe(takeUntil(this._unsubscribe))
86-
.subscribe(userGroup => (this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup)));
90+
.subscribe(({userGroup, backups, clusters, project}) => {
91+
this.selectedProject = project;
92+
this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup);
93+
94+
this.backup = backups.find(backup => backup.id === this._route.snapshot.params.backupID);
95+
this.cluster = clusters.find(cluster => cluster.id === this.backup.spec?.clusterId);
8796

88-
this._projectService.selectedProject
89-
.pipe(switchMap(project => this._backupService.list(project.id)))
90-
.pipe(map(backups => backups.find(backup => backup.id === this._route.snapshot.params.backupID)))
91-
.pipe(takeUntil(this._unsubscribe))
92-
.subscribe(backup => {
93-
this.backup = backup;
94-
this._clusterService.clusters(this.selectedProject.id, false)
95-
.pipe(takeUntil(this._unsubscribe))
96-
.subscribe(clusters => {
97-
this.cluster = clusters.find(cluster => cluster.id === this.backup.spec.clusterId);
98-
});
9997
this.isInitialized = true;
10098
});
10199
}

modules/web/src/app/backup/details/snapshot/component.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import {Member} from '@shared/entity/member';
2929
import {Project} from '@shared/entity/project';
3030
import {GroupConfig} from '@shared/model/Config';
3131
import {MemberUtils, Permission} from '@shared/utils/member';
32-
import {Subject} from 'rxjs';
33-
import {filter, map, switchMap, take, takeUntil} from 'rxjs/operators';
32+
import {forkJoin, of, Subject} from 'rxjs';
33+
import {filter, switchMap, take, takeUntil} from 'rxjs/operators';
3434
import {getBackupHealthStatus, HealthStatus} from '@shared/utils/health-status';
35-
import { Cluster } from '@app/shared/entity/cluster';
35+
import {Cluster} from '@app/shared/entity/cluster';
3636

3737
@Component({
3838
selector: 'km-snapshot-details',
@@ -79,26 +79,23 @@ export class SnapshotDetailsComponent implements OnInit, OnDestroy {
7979

8080
this._projectService.selectedProject
8181
.pipe(
82-
switchMap(project => {
83-
this.selectedProject = project;
84-
return this._userService.getCurrentUserGroup(project.id);
85-
})
82+
switchMap(project =>
83+
forkJoin({
84+
userGroup: this._userService.getCurrentUserGroup(project.id).pipe(take(1)),
85+
backups: this._backupService.list(project.id, true).pipe(take(1)),
86+
clusters: this._clusterService.clusters(project.id, false).pipe(take(1)),
87+
project: of(project),
88+
})
89+
)
8690
)
8791
.pipe(takeUntil(this._unsubscribe))
88-
.subscribe(userGroup => (this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup)));
92+
.subscribe(({userGroup, backups, clusters, project}) => {
93+
this.selectedProject = project;
94+
this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup);
95+
96+
this.backup = backups.find(backup => backup.id === this._route.snapshot.params.backupID);
97+
this.cluster = clusters.find(cluster => cluster.id === this.backup.spec?.clusterId);
8998

90-
this._projectService.selectedProject
91-
.pipe(switchMap(project => this._backupService.list(project.id, true)))
92-
.pipe(map(backups => backups.find(backup => backup.id === this._route.snapshot.params.backupID)))
93-
.pipe(takeUntil(this._unsubscribe))
94-
.subscribe(backup => {
95-
this.backup = backup;
96-
this._projectService.selectedProject
97-
.pipe(switchMap(project => this._clusterService.clusters(project.id, false)))
98-
.pipe(takeUntil(this._unsubscribe))
99-
.subscribe(clusters => {
100-
this.cluster = clusters.find(cluster => cluster.id === this.backup.spec.clusterId);
101-
});
10299
this.isInitialized = true;
103100
});
104101
}

modules/web/src/app/backup/list/automatic-backup/component.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
AddAutomaticBackupDialogComponent,
2222
AddAutomaticBackupDialogConfig,
2323
} from '@app/backup/list/automatic-backup/add-dialog/component';
24+
import {Cluster} from '@app/shared/entity/cluster';
2425
import {BackupService} from '@core/services/backup';
2526
import {ClusterService} from '@core/services/cluster';
2627
import {NotificationService} from '@core/services/notification';
@@ -35,7 +36,7 @@ import {GroupConfig} from '@shared/model/Config';
3536
import {HealthStatus, getBackupHealthStatus} from '@shared/utils/health-status';
3637
import {MemberUtils, Permission} from '@shared/utils/member';
3738
import _ from 'lodash';
38-
import {Subject} from 'rxjs';
39+
import {forkJoin, of, Subject} from 'rxjs';
3940
import {filter, switchMap, take, takeUntil} from 'rxjs/operators';
4041

4142
@Component({
@@ -50,7 +51,7 @@ export class AutomaticBackupListComponent implements OnInit, OnDestroy {
5051
private _currentGroupConfig: GroupConfig;
5152
private _selectedProject = {} as Project;
5253
private _backups = [];
53-
private _clusters = [];
54+
private _clusters = new Map<string, Cluster>();
5455
dataSource = new MatTableDataSource<EtcdBackupConfig>();
5556
isInitialized = true;
5657

@@ -104,28 +105,28 @@ export class AutomaticBackupListComponent implements OnInit, OnDestroy {
104105

105106
this._projectService.selectedProject
106107
.pipe(
107-
switchMap(project => {
108-
this._selectedProject = project;
109-
return this._userService.getCurrentUserGroup(project.id);
110-
})
108+
switchMap(project =>
109+
forkJoin({
110+
userGroup: this._userService.getCurrentUserGroup(project.id).pipe(take(1)),
111+
backups: this._backupService.list(project.id).pipe(take(1)),
112+
clusters: this._clusterService.clusters(project.id, false).pipe(take(1)),
113+
project: of(project),
114+
})
115+
)
111116
)
112117
.pipe(takeUntil(this._unsubscribe))
113-
.subscribe(userGroup => (this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup)));
118+
.subscribe(
119+
({userGroup, backups, clusters, project}) => {
120+
this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup);
114121

115-
this._projectService.selectedProject
116-
.pipe(switchMap(project => this._backupService.list(project.id)))
117-
.pipe(takeUntil(this._unsubscribe))
118-
.subscribe(backups => {
119-
this._backups = backups;
120-
this.dataSource.data = this._backups;
121-
});
122+
this._backups = backups;
123+
this.dataSource.data = this._backups;
122124

123-
this._projectService.selectedProject
124-
.pipe(switchMap(project => this._clusterService.clusters(project.id, false)))
125-
.pipe(takeUntil(this._unsubscribe))
126-
.subscribe(clusters => {
127-
this._clusters = clusters;
128-
});
125+
clusters.forEach(cluster => this._clusters.set(cluster.id, cluster));
126+
127+
this._selectedProject = project;
128+
}
129+
);
129130
}
130131

131132
ngOnDestroy(): void {
@@ -147,12 +148,7 @@ export class AutomaticBackupListComponent implements OnInit, OnDestroy {
147148
}
148149

149150
getClusterName(backup: EtcdBackupConfig): string {
150-
for (const cluster of this._clusters) {
151-
if (cluster.id === backup.spec.clusterId) {
152-
return cluster.name;
153-
}
154-
}
155-
return 'N/A';
151+
return this._clusters.get(backup.spec.clusterId)?.name ?? 'N/A';
156152
}
157153

158154
delete(backup: EtcdBackupConfig): void {

modules/web/src/app/backup/list/restore/component.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {Component, OnDestroy, OnInit, TrackByFunction, ViewChild} from '@angular
1616
import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
1717
import {MatPaginator} from '@angular/material/paginator';
1818
import {MatTableDataSource} from '@angular/material/table';
19+
import {Cluster} from '@app/shared/entity/cluster';
1920
import {BackupService} from '@core/services/backup';
2021
import {ClusterService} from '@core/services/cluster';
2122
import {NotificationService} from '@core/services/notification';
@@ -29,7 +30,7 @@ import {Project} from '@shared/entity/project';
2930
import {GroupConfig} from '@shared/model/Config';
3031
import {MemberUtils, Permission} from '@shared/utils/member';
3132
import _ from 'lodash';
32-
import {Subject} from 'rxjs';
33+
import {forkJoin, Subject} from 'rxjs';
3334
import {filter, switchMap, take, takeUntil, tap} from 'rxjs/operators';
3435

3536
@Component({
@@ -45,7 +46,7 @@ export class RestoreListComponent implements OnInit, OnDestroy {
4546
private _currentGroupConfig: GroupConfig;
4647
private _selectedProject = {} as Project;
4748
private _restores = [];
48-
private _clusters = [];
49+
private _clusters = new Map<string, Cluster>();
4950
dataSource = new MatTableDataSource<EtcdRestore>();
5051
isInitialized = true;
5152

@@ -94,24 +95,23 @@ export class RestoreListComponent implements OnInit, OnDestroy {
9495
.subscribe(_ => this._onChange.next());
9596

9697
this._onChange
97-
.pipe(switchMap(_ => this._userService.getCurrentUserGroup(this._selectedProject.id).pipe(take(1))))
98-
.pipe(takeUntil(this._unsubscribe))
99-
.subscribe(userGroup => (this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup)));
100-
101-
this._onChange
102-
.pipe(switchMap(_ => this._backupService.restoreList(this._selectedProject.id)))
98+
.pipe(
99+
switchMap(_ =>
100+
forkJoin({
101+
userGroup: this._userService.getCurrentUserGroup(this._selectedProject.id).pipe(take(1)),
102+
restores: this._backupService.restoreList(this._selectedProject.id).pipe(take(1)),
103+
clusters: this._clusterService.clusters(this._selectedProject.id, false).pipe(take(1)),
104+
})
105+
)
106+
)
103107
.pipe(takeUntil(this._unsubscribe))
104-
.subscribe(restores => {
108+
.subscribe(({userGroup, restores, clusters}) => {
109+
this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup);
105110
this._restores = restores;
106111
this.dataSource.data = this._restores;
112+
clusters.forEach(cluster => this._clusters.set(cluster.id, cluster));
107113
});
108114

109-
this._onChange
110-
.pipe(switchMap(_ => this._clusterService.clusters(this._selectedProject.id, false)))
111-
.pipe(takeUntil(this._unsubscribe))
112-
.subscribe(clusters => {
113-
this._clusters = clusters;
114-
});
115115
}
116116

117117
ngOnDestroy(): void {
@@ -120,12 +120,7 @@ export class RestoreListComponent implements OnInit, OnDestroy {
120120
}
121121

122122
getClusterName(restore: EtcdRestore): string {
123-
for (const cluster of this._clusters) {
124-
if (cluster.id === restore.spec.clusterId) {
125-
return cluster.name;
126-
}
127-
}
128-
return 'N/A';
123+
return this._clusters.get(restore.spec.clusterId)?.name ?? 'N/A';
129124
}
130125

131126
delete(restore: EtcdRestore): void {

modules/web/src/app/backup/list/snapshot/component.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
DeleteSnapshotDialogComponent,
2323
DeleteSnapshotDialogConfig,
2424
} from '@app/backup/list/snapshot/delete-dialog/component';
25+
import {Cluster} from '@app/shared/entity/cluster';
2526
import {BackupService} from '@core/services/backup';
2627
import {ClusterService} from '@core/services/cluster';
2728
import {ProjectService} from '@core/services/project';
@@ -33,7 +34,7 @@ import {Project} from '@shared/entity/project';
3334
import {GroupConfig} from '@shared/model/Config';
3435
import {HealthStatus, getBackupHealthStatus} from '@shared/utils/health-status';
3536
import {MemberUtils, Permission} from '@shared/utils/member';
36-
import {Observable, Subject} from 'rxjs';
37+
import {forkJoin, of, Subject} from 'rxjs';
3738
import {filter, switchMap, take, takeUntil} from 'rxjs/operators';
3839

3940
@Component({
@@ -48,7 +49,7 @@ export class SnapshotListComponent implements OnInit, OnDestroy {
4849
private _currentGroupConfig: GroupConfig;
4950
private _selectedProject = {} as Project;
5051
private _backups = [];
51-
private _clusters = [];
52+
private _clusters = new Map<string, Cluster>();
5253
dataSource = new MatTableDataSource<EtcdBackupConfig>();
5354
isInitialized = true;
5455

@@ -101,27 +102,24 @@ export class SnapshotListComponent implements OnInit, OnDestroy {
101102

102103
this._projectService.selectedProject
103104
.pipe(
104-
switchMap(project => {
105-
this._selectedProject = project;
106-
return this._userService.getCurrentUserGroup(project.id);
107-
})
105+
switchMap(project =>
106+
forkJoin({
107+
userGroup: this._userService.getCurrentUserGroup(project.id).pipe(take(1)),
108+
backups: this._backupService.list(project.id, true).pipe(take(1)),
109+
clusters: this._clusterService.clusters(project.id, false).pipe(take(1)),
110+
project: of(project),
111+
})
112+
)
108113
)
109114
.pipe(takeUntil(this._unsubscribe))
110-
.subscribe(userGroup => (this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup)));
115+
.subscribe(({userGroup, backups, clusters, project}) => {
116+
this._selectedProject = project;
117+
this._currentGroupConfig = this._userService.getCurrentUserGroupConfig(userGroup);
111118

112-
this._projectService.selectedProject
113-
.pipe(switchMap(project => this._backupService.list(project.id, true)))
114-
.pipe(takeUntil(this._unsubscribe))
115-
.subscribe(backups => {
116119
this._backups = backups;
117120
this.dataSource.data = this._backups;
118-
});
119121

120-
this._projectService.selectedProject
121-
.pipe(switchMap(project => this._clusterService.clusters(project.id, false)))
122-
.pipe(takeUntil(this._unsubscribe))
123-
.subscribe(clusters => {
124-
this._clusters = clusters;
122+
clusters.forEach(cluster => this._clusters.set(cluster.id, cluster));
125123
});
126124
}
127125

@@ -140,12 +138,7 @@ export class SnapshotListComponent implements OnInit, OnDestroy {
140138
}
141139

142140
getClusterName(backup: EtcdBackupConfig): string {
143-
for (const cluster of this._clusters) {
144-
if (cluster.id === backup.spec.clusterId) {
145-
return cluster.name;
146-
}
147-
}
148-
return 'N/A';
141+
return this._clusters.get(backup.spec.clusterId)?.name ?? 'N/A';
149142
}
150143

151144
delete(backup: EtcdBackupConfig): void {

0 commit comments

Comments
 (0)