forked from ceph/ceph
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rgw-bucket-list.component.ts
207 lines (195 loc) · 7.23 KB
/
rgw-bucket-list.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { Component, NgZone, OnInit, TemplateRef, ViewChild } from '@angular/core';
import _ from 'lodash';
import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs';
import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
import { TableStatus } from '~/app/shared/classes/table-status';
import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
import { TableComponent } from '~/app/shared/datatable/table/table.component';
import { Icons } from '~/app/shared/enum/icons.enum';
import { CdTableAction } from '~/app/shared/models/cd-table-action';
import { CdTableColumn } from '~/app/shared/models/cd-table-column';
import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
import { Permission } from '~/app/shared/models/permissions';
import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
import { DimlessPipe } from '~/app/shared/pipes/dimless.pipe';
import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
import { ModalService } from '~/app/shared/services/modal.service';
import { URLBuilderService } from '~/app/shared/services/url-builder.service';
const BASE_URL = 'rgw/bucket';
@Component({
selector: 'cd-rgw-bucket-list',
templateUrl: './rgw-bucket-list.component.html',
styleUrls: ['./rgw-bucket-list.component.scss'],
providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
})
export class RgwBucketListComponent extends ListWithDetails implements OnInit {
@ViewChild(TableComponent, { static: true })
table: TableComponent;
@ViewChild('bucketSizeTpl', { static: true })
bucketSizeTpl: TemplateRef<any>;
@ViewChild('bucketObjectTpl', { static: true })
bucketObjectTpl: TemplateRef<any>;
permission: Permission;
tableActions: CdTableAction[];
columns: CdTableColumn[] = [];
buckets: object[] = [];
selection: CdTableSelection = new CdTableSelection();
tableStatus = new TableStatus();
staleTimeout: number;
constructor(
private authStorageService: AuthStorageService,
private dimlessBinaryPipe: DimlessBinaryPipe,
private dimlessPipe: DimlessPipe,
private rgwBucketService: RgwBucketService,
private modalService: ModalService,
private urlBuilder: URLBuilderService,
public actionLabels: ActionLabelsI18n,
private ngZone: NgZone
) {
super();
}
ngOnInit() {
this.permission = this.authStorageService.getPermissions().rgw;
this.columns = [
{
name: $localize`Name`,
prop: 'bid',
flexGrow: 2
},
{
name: $localize`Owner`,
prop: 'owner',
flexGrow: 2.5
},
{
name: $localize`Used Capacity`,
prop: 'bucket_size',
flexGrow: 0.6,
pipe: this.dimlessBinaryPipe
},
{
name: $localize`Capacity Limit %`,
prop: 'size_usage',
cellTemplate: this.bucketSizeTpl,
flexGrow: 0.8
},
{
name: $localize`Objects`,
prop: 'num_objects',
flexGrow: 0.6,
pipe: this.dimlessPipe
},
{
name: $localize`Object Limit %`,
prop: 'object_usage',
cellTemplate: this.bucketObjectTpl,
flexGrow: 0.8
}
];
const getBucketUri = () =>
this.selection.first() && `${encodeURIComponent(this.selection.first().bid)}`;
const addAction: CdTableAction = {
permission: 'create',
icon: Icons.add,
routerLink: () => this.urlBuilder.getCreate(),
name: this.actionLabels.CREATE,
canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
};
const editAction: CdTableAction = {
permission: 'update',
icon: Icons.edit,
routerLink: () => this.urlBuilder.getEdit(getBucketUri()),
name: this.actionLabels.EDIT
};
const deleteAction: CdTableAction = {
permission: 'delete',
icon: Icons.destroy,
click: () => this.deleteAction(),
disable: () => !this.selection.hasSelection,
name: this.actionLabels.DELETE,
canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
};
this.tableActions = [addAction, editAction, deleteAction];
this.timeConditionReached();
}
transformBucketData() {
_.forEach(this.buckets, (bucketKey) => {
const usageList = bucketKey['usage'];
const maxBucketSize = bucketKey['bucket_quota']['max_size'];
const maxBucketObjects = bucketKey['bucket_quota']['max_objects'];
let totalBucketSize = 0;
let numOfObjects = 0;
_.forEach(usageList, (usageKey) => {
totalBucketSize = totalBucketSize + usageKey.size_actual;
numOfObjects = numOfObjects + usageKey.num_objects;
});
bucketKey['bucket_size'] = totalBucketSize;
bucketKey['num_objects'] = numOfObjects;
bucketKey['size_usage'] = maxBucketSize > 0 ? totalBucketSize / maxBucketSize : undefined;
bucketKey['object_usage'] =
maxBucketObjects > 0 ? numOfObjects / maxBucketObjects : undefined;
});
}
timeConditionReached() {
clearTimeout(this.staleTimeout);
this.ngZone.runOutsideAngular(() => {
this.staleTimeout = window.setTimeout(() => {
this.ngZone.run(() => {
this.tableStatus = new TableStatus(
'warning',
$localize`The bucket list data might be stale. If needed, you can manually reload it.`
);
});
}, 10000);
});
}
getBucketList(context: CdTableFetchDataContext) {
this.tableStatus = new TableStatus();
this.timeConditionReached();
this.rgwBucketService.list().subscribe(
(resp: object[]) => {
this.buckets = resp;
this.transformBucketData();
},
() => {
context.error();
}
);
}
updateSelection(selection: CdTableSelection) {
this.selection = selection;
}
deleteAction() {
this.modalService.show(CriticalConfirmationModalComponent, {
itemDescription: this.selection.hasSingleSelection ? $localize`bucket` : $localize`buckets`,
itemNames: this.selection.selected.map((bucket: any) => bucket['bid']),
submitActionObservable: () => {
return new Observable((observer: Subscriber<any>) => {
// Delete all selected data table rows.
observableForkJoin(
this.selection.selected.map((bucket: any) => {
return this.rgwBucketService.delete(bucket.bid);
})
).subscribe({
error: (error) => {
// Forward the error to the observer.
observer.error(error);
// Reload the data table content because some deletions might
// have been executed successfully in the meanwhile.
this.table.refreshBtn();
},
complete: () => {
// Notify the observer that we are done.
observer.complete();
// Reload the data table content.
this.table.refreshBtn();
}
});
});
}
});
}
}