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