1 import { HttpParams } from '@angular/common/http';
14 } from '@angular/core';
16 import _ from 'lodash';
17 import { Observable, Subscription } from 'rxjs';
18 import { take } from 'rxjs/operators';
20 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
21 import { DaemonService } from '~/app/shared/api/daemon.service';
22 import { HostService } from '~/app/shared/api/host.service';
23 import { OrchestratorService } from '~/app/shared/api/orchestrator.service';
24 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
25 import { TableComponent } from '~/app/shared/datatable/table/table.component';
26 import { Icons } from '~/app/shared/enum/icons.enum';
27 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
28 import { CdTableAction } from '~/app/shared/models/cd-table-action';
29 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
30 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
31 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
32 import { Daemon } from '~/app/shared/models/daemon.interface';
33 import { Permissions } from '~/app/shared/models/permissions';
34 import { CephServiceSpec } from '~/app/shared/models/service.interface';
35 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
36 import { RelativeDatePipe } from '~/app/shared/pipes/relative-date.pipe';
37 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
38 import { NotificationService } from '~/app/shared/services/notification.service';
41 selector: 'cd-service-daemon-list',
42 templateUrl: './service-daemon-list.component.html',
43 styleUrls: ['./service-daemon-list.component.scss']
45 export class ServiceDaemonListComponent implements OnInit, OnChanges, AfterViewInit, OnDestroy {
46 @ViewChild('statusTpl', { static: true })
47 statusTpl: TemplateRef<any>;
49 @ViewChild('listTpl', { static: true })
50 listTpl: TemplateRef<any>;
52 @ViewChild('cpuTpl', { static: true })
53 cpuTpl: TemplateRef<any>;
55 @ViewChildren('daemonsTable')
56 daemonsTableTpls: QueryList<TemplateRef<TableComponent>>;
65 hiddenColumns: string[] = [];
72 warningThreshold = 0.8;
78 daemons: Daemon[] = [];
79 services: Array<CephServiceSpec> = [];
80 columns: CdTableColumn[] = [];
81 serviceColumns: CdTableColumn[] = [];
82 tableActions: CdTableAction[];
83 selection = new CdTableSelection();
84 permissions: Permissions;
86 hasOrchestrator = false;
89 private daemonsTable: TableComponent;
90 private daemonsTableTplsSub: Subscription;
91 private serviceSub: Subscription;
94 private hostService: HostService,
95 private cephServiceService: CephServiceService,
96 private orchService: OrchestratorService,
97 private relativeDatePipe: RelativeDatePipe,
98 private dimlessBinary: DimlessBinaryPipe,
99 public actionLabels: ActionLabelsI18n,
100 private authStorageService: AuthStorageService,
101 private daemonService: DaemonService,
102 private notificationService: NotificationService,
103 private cdRef: ChangeDetectorRef
107 this.permissions = this.authStorageService.getPermissions();
108 this.tableActions = [
110 permission: 'update',
112 click: () => this.daemonAction('start'),
113 name: this.actionLabels.START,
114 disable: () => this.actionDisabled('start')
117 permission: 'update',
119 click: () => this.daemonAction('stop'),
120 name: this.actionLabels.STOP,
121 disable: () => this.actionDisabled('stop')
124 permission: 'update',
126 click: () => this.daemonAction('restart'),
127 name: this.actionLabels.RESTART,
128 disable: () => this.actionDisabled('restart')
131 permission: 'update',
133 click: () => this.daemonAction('redeploy'),
134 name: this.actionLabels.REDEPLOY,
135 disable: () => this.actionDisabled('redeploy')
140 name: $localize`Hostname`,
146 name: $localize`Daemon name`,
152 name: $localize`Version`,
158 name: $localize`Status`,
162 cellTemplate: this.statusTpl
165 name: $localize`Last Refreshed`,
166 prop: 'last_refresh',
167 pipe: this.relativeDatePipe,
171 name: $localize`CPU Usage`,
172 prop: 'cpu_percentage',
174 cellTemplate: this.cpuTpl
177 name: $localize`Memory Usage`,
178 prop: 'memory_usage',
180 pipe: this.dimlessBinary,
181 cellClass: 'text-right'
184 name: $localize`Daemon Events`,
187 cellTemplate: this.listTpl
191 this.serviceColumns = [
193 name: $localize`Service Name`,
194 prop: 'service_name',
199 name: $localize`Service Type`,
200 prop: 'service_type',
205 name: $localize`Service Events`,
208 cellTemplate: this.listTpl
212 this.orchService.status().subscribe((data: { available: boolean }) => {
213 this.hasOrchestrator = data.available;
214 this.showDocPanel = !data.available;
217 this.columns = this.columns.filter((col: any) => {
218 return !this.hiddenColumns.includes(col.prop);
222 this.cdRef.detectChanges();
227 if (!_.isUndefined(this.daemonsTable)) {
228 this.daemonsTable.reloadData();
233 this.daemonsTableTplsSub = this.daemonsTableTpls.changes.subscribe(
234 (tableRefs: QueryList<TableComponent>) => {
235 this.daemonsTable = tableRefs.first;
241 if (this.daemonsTableTplsSub) {
242 this.daemonsTableTplsSub.unsubscribe();
244 if (this.serviceSub) {
245 this.serviceSub.unsubscribe();
249 getStatusClass(row: Daemon): string {
252 '-1': 'badge-danger',
253 '0': 'badge-warning',
261 getDaemons(context: CdTableFetchDataContext) {
262 let observable: Observable<Daemon[]>;
264 observable = this.hostService.getDaemons(this.hostname);
265 } else if (this.serviceName) {
266 observable = this.cephServiceService.getDaemons(this.serviceName);
271 observable.subscribe(
272 (daemons: Daemon[]) => {
273 this.daemons = daemons;
274 this.sortDaemonEvents();
284 this.daemons.forEach((daemon: any) => {
285 daemon.events?.sort((event1: any, event2: any) => {
286 return new Date(event2.created).getTime() - new Date(event1.created).getTime();
290 getServices(context: CdTableFetchDataContext) {
291 this.serviceSub = this.cephServiceService
292 .list(new HttpParams({ fromObject: { limit: -1, offset: 0 } }), this.serviceName)
293 .observable.subscribe(
294 (services: CephServiceSpec[]) => {
295 this.services = services;
304 trackByFn(_index: any, item: any) {
308 updateSelection(selection: CdTableSelection) {
309 this.selection = selection;
312 daemonAction(actionType: string) {
314 .action(this.selection.first()?.daemon_name, actionType)
318 this.notificationService.show(
319 NotificationType.success,
320 `Daemon ${actionType} scheduled`,
325 this.notificationService.show(
326 NotificationType.error,
327 'Daemon action failed',
334 actionDisabled(actionType: string) {
335 if (this.selection?.hasSelection) {
336 const daemon = this.selection.selected[0];
337 if (daemon.daemon_type === 'mon' || daemon.daemon_type === 'mgr') {
338 return true; // don't allow actions on mon and mgr, dashboard requires them.
340 switch (actionType) {
342 if (daemon.status_desc === 'running') {
347 if (daemon.status_desc === 'stopped') {
354 return true; // if no selection then disable everything