]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
6683d276c6a46d1780b5b5880f60496780c1da8e
[ceph.git] /
1 import {
2   AfterViewInit,
3   Component,
4   Input,
5   OnChanges,
6   OnDestroy,
7   OnInit,
8   QueryList,
9   TemplateRef,
10   ViewChildren
11 } from '@angular/core';
12 import { I18n } from '@ngx-translate/i18n-polyfill';
13
14 import * as _ from 'lodash';
15
16 import { Observable, Subscription } from 'rxjs';
17 import { CephServiceService } from '../../../../shared/api/ceph-service.service';
18 import { HostService } from '../../../../shared/api/host.service';
19 import { OrchestratorService } from '../../../../shared/api/orchestrator.service';
20 import { TableComponent } from '../../../../shared/datatable/table/table.component';
21 import { CellTemplate } from '../../../../shared/enum/cell-template.enum';
22 import { CdTableColumn } from '../../../../shared/models/cd-table-column';
23 import { CdTableFetchDataContext } from '../../../../shared/models/cd-table-fetch-data-context';
24 import { Daemon } from '../../../../shared/models/daemon.interface';
25
26 @Component({
27   selector: 'cd-service-daemon-list',
28   templateUrl: './service-daemon-list.component.html',
29   styleUrls: ['./service-daemon-list.component.scss']
30 })
31 export class ServiceDaemonListComponent implements OnInit, OnChanges, AfterViewInit, OnDestroy {
32   @ViewChildren('daemonsTable')
33   daemonsTableTpls: QueryList<TemplateRef<TableComponent>>;
34
35   @Input()
36   serviceName?: string;
37
38   @Input()
39   hostname?: string;
40
41   daemons: Daemon[] = [];
42   columns: CdTableColumn[] = [];
43
44   hasOrchestrator = false;
45
46   private daemonsTable: TableComponent;
47   private daemonsTableTplsSub: Subscription;
48
49   constructor(
50     private i18n: I18n,
51     private hostService: HostService,
52     private cephServiceService: CephServiceService,
53     private orchService: OrchestratorService
54   ) {}
55
56   ngOnInit() {
57     this.columns = [
58       {
59         name: this.i18n('Hostname'),
60         prop: 'hostname',
61         flexGrow: 1,
62         filterable: true
63       },
64       {
65         name: this.i18n('Daemon type'),
66         prop: 'daemon_type',
67         flexGrow: 1,
68         filterable: true
69       },
70       {
71         name: this.i18n('Daemon ID'),
72         prop: 'daemon_id',
73         flexGrow: 1,
74         filterable: true
75       },
76       {
77         name: this.i18n('Container ID'),
78         prop: 'container_id',
79         flexGrow: 3,
80         filterable: true,
81         cellTransformation: CellTemplate.truncate,
82         customTemplateConfig: {
83           length: 12
84         }
85       },
86       {
87         name: this.i18n('Container Image name'),
88         prop: 'container_image_name',
89         flexGrow: 3,
90         filterable: true
91       },
92       {
93         name: this.i18n('Container Image ID'),
94         prop: 'container_image_id',
95         flexGrow: 3,
96         filterable: true,
97         cellTransformation: CellTemplate.truncate,
98         customTemplateConfig: {
99           length: 12
100         }
101       },
102       {
103         name: this.i18n('Version'),
104         prop: 'version',
105         flexGrow: 1,
106         filterable: true
107       },
108       {
109         name: this.i18n('Status'),
110         prop: 'status',
111         flexGrow: 1,
112         filterable: true
113       },
114       {
115         name: this.i18n('Status Description'),
116         prop: 'status_desc',
117         flexGrow: 1,
118         filterable: true
119       },
120       {
121         name: this.i18n('Last Refreshed'),
122         prop: 'last_refresh',
123         flexGrow: 2
124       }
125     ];
126
127     this.orchService.status().subscribe((data: { available: boolean }) => {
128       this.hasOrchestrator = data.available;
129     });
130   }
131
132   ngOnChanges() {
133     if (!_.isUndefined(this.daemonsTable)) {
134       this.daemonsTable.reloadData();
135     }
136   }
137
138   ngAfterViewInit() {
139     this.daemonsTableTplsSub = this.daemonsTableTpls.changes.subscribe(
140       (tableRefs: QueryList<TableComponent>) => {
141         this.daemonsTable = tableRefs.first;
142       }
143     );
144   }
145
146   ngOnDestroy() {
147     if (this.daemonsTableTplsSub) {
148       this.daemonsTableTplsSub.unsubscribe();
149     }
150   }
151
152   getDaemons(context: CdTableFetchDataContext) {
153     let observable: Observable<Daemon[]>;
154     if (this.hostname) {
155       observable = this.hostService.getDaemons(this.hostname);
156     } else if (this.serviceName) {
157       observable = this.cephServiceService.getDaemons(this.serviceName);
158     } else {
159       this.daemons = [];
160       return;
161     }
162     observable.subscribe(
163       (daemons: Daemon[]) => {
164         this.daemons = daemons;
165       },
166       () => {
167         this.daemons = [];
168         context.error();
169       }
170     );
171   }
172 }