FE getServicesJobInfo can filter by serviceModelId
[vid.git] / vid-webpack-master / src / app / instantiationStatus / instantiationStatus.component.ts
1 import {Component, ViewChild} from '@angular/core';
2 import {ServiceInfoService} from '../shared/server/serviceInfo/serviceInfo.service';
3 import {ServiceInfoModel} from '../shared/server/serviceInfo/serviceInfo.model';
4 import {InstantiationStatusComponentService} from './instantiationStatus.component.service';
5 import {ContextMenuComponent, ContextMenuService} from 'ngx-contextmenu';
6 import {AuditInfoModalComponent} from "../shared/components/auditInfoModal/auditInfoModal.component";
7 import * as _ from 'lodash';
8 import {ScrollToConfigOptions, ScrollToService} from '@nicky-lenaers/ngx-scroll-to';
9 import {ConfigurationService} from "../shared/services/configuration.service";
10 import {LogService} from '../shared/utils/log/log.service';
11 import {AppState} from "../shared/store/reducers";
12 import {NgRedux} from '@angular-redux/store';
13 import {JobStatus, ServiceAction} from "../shared/models/serviceInstanceActions";
14
15 export interface MenuAction{
16   name: string;
17   dataTestId: string;
18   className: string;
19   tooltip?: string;
20   click(item: ServiceInfoModel): void;
21   enabled (item?: ServiceInfoModel): boolean;
22   visible (item?: ServiceInfoModel): boolean;
23 }
24
25 @Component({
26   selector : 'instantiation-status',
27   templateUrl : './instantiationStatus.component.html',
28   styleUrls : ['./instantiationStatus.component.scss']
29 })
30 export class InstantiationStatusComponent {
31
32   TIMER_TIME_IN_SECONDS : number = 0;
33   timer = null;
34   dataIsReady : boolean = false;
35   scroll : boolean = false;
36   lastUpdatedDate: Date = null;
37   instantiationStatusComponentService: InstantiationStatusComponentService;
38   configurationService : ConfigurationService;
39   serviceInfoData: ServiceInfoModel[] = null;
40   @ViewChild(ContextMenuComponent) public contextMenu: ContextMenuComponent;
41
42   public contextMenuActions: Array<MenuAction> = [
43     {
44       name: "Redeploy",
45       dataTestId: "context-menu-retry",
46       className: "fa-repeat",
47       click: (item: ServiceInfoModel) => this.retryItem(item),
48       enabled: () =>  true,
49       visible: (item: ServiceInfoModel) =>  item.isRetryEnabled,
50     },
51     {
52       name: "Open",
53       dataTestId: "context-menu-open",
54       className: "fa-external-link",
55       click: (item: ServiceInfoModel) => this.instantiationStatusComponentService.open(item),
56       enabled: (item: ServiceInfoModel) =>  this.isOpenEnabled(item),
57       visible: () =>  true,
58     },
59     {
60       name: "Audit info",
61       dataTestId: "context-menu-audit-info",
62       className: "fa-info-circle",
63       click: (item: ServiceInfoModel) => this.auditInfo(item),
64       enabled: (item: ServiceInfoModel) =>  this.isAuditInfoEnabled(item),
65       visible: () =>  true,
66     },
67     {
68       name: "Delete",
69       dataTestId: "context-menu-remove",
70       className: "fa-trash-o",
71       click: (item: ServiceInfoModel) => this.deleteItem(item),
72       enabled: (item: ServiceInfoModel) =>  this.isDeleteEnabled(item),
73       visible: () =>  true,
74     },
75     {
76       name: "Hide request",
77       dataTestId: "context-menu-hide",
78       className: "fa-eye-slash",
79       tooltip: "Hide this service from this table",
80       click: (item: ServiceInfoModel) => this.hideItem(item),
81       enabled: (item: ServiceInfoModel) =>  this.isHideEnabled(item),
82       visible: () =>  true,
83     }
84   ];
85
86   flags: any;
87   constructor(private _serviceInfoService: ServiceInfoService,
88               private _instantiationStatusComponentService : InstantiationStatusComponentService,
89               private _contextMenuService: ContextMenuService,
90               private _configurationService : ConfigurationService,
91               private _scrollToService: ScrollToService,
92               private _logService : LogService,
93               private _store: NgRedux<AppState>) {
94     this.instantiationStatusComponentService = _instantiationStatusComponentService;
95     this.configurationService = this._configurationService;
96     this.configurationService.getConfiguration("refreshTimeInstantiationDashboard").subscribe(response => {
97       this.TIMER_TIME_IN_SECONDS = _.isNumber(response) ? response : 0;
98       this.activateInterval();
99       this.refreshData();
100     });
101   }
102
103   activateInterval() {
104     if (this.TIMER_TIME_IN_SECONDS > 0) {
105       this.timer = setInterval(() => {
106         this.refreshData();
107       }, this.TIMER_TIME_IN_SECONDS * 1000);
108     }
109   }
110
111   deactivateInterval() {
112     clearInterval(this.timer);
113   }
114
115   refreshData(): void {
116     this.dataIsReady = false;
117     this._serviceInfoService.getServicesJobInfo(this.lastUpdatedDate === null)
118       .subscribe((res: ServiceInfoModel[]) => {
119         this._instantiationStatusComponentService.convertObjectToArray(res).subscribe((res) => {
120           this._logService.info('refresh instantiation status table', res);
121           this.dataIsReady = true;
122           this.lastUpdatedDate = new Date();
123           if (!_.isEqual(this.serviceInfoData, res)) {
124             this.serviceInfoData = res;
125             this.scrollToElement(this.findFirstVisibleJob());
126           }
127         });
128       })
129   }
130
131   trackByFn(index: number, item: ServiceInfoModel){
132     return _.isNil(item) ? null : item.jobId;
133   }
134
135   deleteItem(item: ServiceInfoModel): void {
136     this._serviceInfoService.deleteJob(item.jobId).subscribe(() => {
137       this.refreshData();
138     });
139   }
140
141   hideItem(item: ServiceInfoModel): void {
142     this._serviceInfoService.hideJob(item.jobId).subscribe(() => {
143       this.refreshData();
144     });
145   }
146
147   retryItem(item: ServiceInfoModel) : void {
148     if (item.isRetryEnabled) {
149       this._instantiationStatusComponentService.retry(item);
150     }
151   }
152
153   auditInfo(jobData : ServiceInfoModel): void {
154     AuditInfoModalComponent.openModal.next(jobData);
155   }
156
157   isOpenEnabled(item: ServiceInfoModel):boolean {
158     switch(item.action) {
159       case ServiceAction.DELETE:
160       return _.includes([ JobStatus.PENDING, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.FAILED], item.jobStatus);
161       case ServiceAction.UPDATE:
162         return _.includes([JobStatus.PENDING, JobStatus.PAUSE, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.COMPLETED, JobStatus.FAILED], item.jobStatus);
163       default:
164         return _.includes([JobStatus.COMPLETED, JobStatus.PAUSE, JobStatus.COMPLETED_WITH_ERRORS], item.jobStatus);
165     }
166   }
167
168   isAuditInfoEnabled(item: ServiceInfoModel): boolean {
169     if(item.action === ServiceAction.DELETE || item.action=== ServiceAction.UPDATE) {
170       return _.includes([JobStatus.FAILED, JobStatus.IN_PROGRESS, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.PAUSE, JobStatus.COMPLETED], item.jobStatus);
171     }
172     return true;// ServiceAction.INSTANTIATE
173   }
174
175   isDeleteEnabled(item: ServiceInfoModel):boolean {
176     if( item.action === ServiceAction.DELETE || item.action === ServiceAction.UPDATE){
177       return _.includes([JobStatus.PENDING], item.jobStatus);
178     }
179     return _.includes([JobStatus.PENDING, JobStatus.STOPPED], item.jobStatus);
180   }
181
182   isHideEnabled(item: ServiceInfoModel):boolean {
183     return _.includes([JobStatus.COMPLETED, JobStatus.FAILED, JobStatus.STOPPED, JobStatus.COMPLETED_WITH_ERRORS], item.jobStatus);
184   }
185
186   public onContextMenu($event: MouseEvent, item: any): void {
187     this._contextMenuService.show.next({
188       contextMenu: this.contextMenu,
189       event: $event,
190       item: item,
191     });
192     $event.preventDefault();
193     $event.stopPropagation();
194   }
195
196   getImagesSrc(imageName : string) : string {
197     return './' + imageName + '.svg';
198   }
199
200   private getHeaderHeaderClientRect(): ClientRect {
201     const element = document.querySelector("#instantiation-status thead") as HTMLElement;
202     return element.getBoundingClientRect();
203   }
204
205   findFirstVisibleJob(): HTMLElement {
206     const elements : any = document.querySelectorAll('#instantiation-status tr');
207     const headerRect = this.getHeaderHeaderClientRect();
208     if (headerRect) {
209       const topEdge = headerRect.bottom;
210       for (let i = 0; i < elements.length; i++) {
211         if (elements[i].getBoundingClientRect().top >= topEdge)
212           return elements[i];
213       }
214     }
215     return null;
216   }
217
218   scrollToElement(currentJob: HTMLElement) {
219     if (currentJob) {
220       const config: ScrollToConfigOptions = {
221         target: currentJob,
222         duration: 0,
223         offset: -1 * (this.getHeaderHeaderClientRect().height + 2),
224       };
225
226       // wait after render
227       setTimeout(() => {
228         this._scrollToService.scrollTo(config);
229       }, 0)
230     }
231   }
232 }