add create another one menu item to instantiationStatus page
[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: "Recreate",
63       dataTestId: "context-menu-recreate",
64       className: "fa-clone",
65       click: (item: ServiceInfoModel) => this.instantiationStatusComponentService.recreate(item),
66       enabled: (item: ServiceInfoModel) =>  this.instantiationStatusComponentService.isRecreateEnabled(item),
67       visible: () =>  this.instantiationStatusComponentService.isRecreateVisible(),
68     },
69     {
70       name: "Audit info",
71       dataTestId: "context-menu-audit-info",
72       className: "fa-info-circle",
73       click: (item: ServiceInfoModel) => this.auditInfo(item),
74       enabled: (item: ServiceInfoModel) =>  this.isAuditInfoEnabled(item),
75       visible: () =>  true,
76     },
77     {
78       name: "Delete",
79       dataTestId: "context-menu-remove",
80       className: "fa-trash-o",
81       click: (item: ServiceInfoModel) => this.deleteItem(item),
82       enabled: (item: ServiceInfoModel) =>  this.isDeleteEnabled(item),
83       visible: () =>  true,
84     },
85     {
86       name: "Hide request",
87       dataTestId: "context-menu-hide",
88       className: "fa-eye-slash",
89       tooltip: "Hide this service from this table",
90       click: (item: ServiceInfoModel) => this.hideItem(item),
91       enabled: (item: ServiceInfoModel) =>  this.isHideEnabled(item),
92       visible: () =>  true,
93     }
94   ];
95
96   flags: any;
97   filterText: string;
98   constructor(private _serviceInfoService: ServiceInfoService,
99               private _instantiationStatusComponentService : InstantiationStatusComponentService,
100               private _contextMenuService: ContextMenuService,
101               private _configurationService : ConfigurationService,
102               private _scrollToService: ScrollToService,
103               private _logService : LogService,
104               private route: ActivatedRoute,
105               private _store: NgRedux<AppState>) {
106     this.instantiationStatusComponentService = _instantiationStatusComponentService;
107     this.configurationService = this._configurationService;
108     this.configurationService.getConfiguration("refreshTimeInstantiationDashboard").subscribe(response => {
109       this.TIMER_TIME_IN_SECONDS = _.isNumber(response) ? response : 0;
110       this.activateInterval();
111       this.refreshData();
112     });
113   }
114
115   ngOnInit() {
116     let filterTextParam =  this.route.snapshot.queryParams["filterText"];
117     this.filterText = filterTextParam ? filterTextParam : "" ;
118   }
119
120   activateInterval() {
121     if (this.TIMER_TIME_IN_SECONDS > 0) {
122       this.timer = setInterval(() => {
123         this.refreshData();
124       }, this.TIMER_TIME_IN_SECONDS * 1000);
125     }
126   }
127
128   deactivateInterval() {
129     clearInterval(this.timer);
130   }
131
132   refreshData(): void {
133     this.dataIsReady = false;
134     this._serviceInfoService.getServicesJobInfo(this.lastUpdatedDate === null)
135       .subscribe((res: ServiceInfoModel[]) => {
136         this._instantiationStatusComponentService.convertObjectToArray(res).subscribe((res) => {
137           this._logService.info('refresh instantiation status table', res);
138           this.dataIsReady = true;
139           this.lastUpdatedDate = new Date();
140           if (!_.isEqual(this.serviceInfoData, res)) {
141             this.serviceInfoData = res;
142             this.scrollToElement(this.findFirstVisibleJob());
143           }
144         });
145       })
146   }
147
148   trackByFn(index: number, item: ServiceInfoModel){
149     return _.isNil(item) ? null : item.jobId;
150   }
151
152   deleteItem(item: ServiceInfoModel): void {
153     this._serviceInfoService.deleteJob(item.jobId).subscribe(() => {
154       this.refreshData();
155     });
156   }
157
158   hideItem(item: ServiceInfoModel): void {
159     this._serviceInfoService.hideJob(item.jobId).subscribe(() => {
160       this.refreshData();
161     });
162   }
163
164   retryItem(item: ServiceInfoModel) : void {
165     if (item.isRetryEnabled) {
166       this._instantiationStatusComponentService.retry(item);
167     }
168   }
169
170   auditInfo(jobData : ServiceInfoModel): void {
171     AuditInfoModalComponent.openModal.next(jobData);
172   }
173
174   isOpenEnabled(item: ServiceInfoModel):boolean {
175     switch(item.action) {
176       case ServiceAction.DELETE:
177       return _.includes([ JobStatus.PENDING, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.FAILED], item.jobStatus);
178       case ServiceAction.UPDATE:
179         return _.includes([JobStatus.PENDING, JobStatus.PAUSE, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.COMPLETED, JobStatus.FAILED], item.jobStatus);
180       default:
181         return _.includes([JobStatus.COMPLETED, JobStatus.PAUSE, JobStatus.COMPLETED_WITH_ERRORS], item.jobStatus);
182     }
183   }
184
185   isAuditInfoEnabled(item: ServiceInfoModel): boolean {
186     if(item.action === ServiceAction.DELETE || item.action=== ServiceAction.UPDATE) {
187       return _.includes([JobStatus.FAILED, JobStatus.IN_PROGRESS, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.PAUSE, JobStatus.COMPLETED], item.jobStatus);
188     }
189     return true;// ServiceAction.INSTANTIATE
190   }
191
192   isDeleteEnabled(item: ServiceInfoModel):boolean {
193     if( item.action === ServiceAction.DELETE || item.action === ServiceAction.UPDATE){
194       return _.includes([JobStatus.PENDING], item.jobStatus);
195     }
196     return _.includes([JobStatus.PENDING, JobStatus.STOPPED], item.jobStatus);
197   }
198
199   isHideEnabled(item: ServiceInfoModel):boolean {
200     return _.includes([JobStatus.COMPLETED, JobStatus.FAILED, JobStatus.STOPPED, JobStatus.COMPLETED_WITH_ERRORS], item.jobStatus);
201   }
202
203   public onContextMenu($event: MouseEvent, item: any): void {
204     this._contextMenuService.show.next({
205       contextMenu: this.contextMenu,
206       event: $event,
207       item: item,
208     });
209     $event.preventDefault();
210     $event.stopPropagation();
211   }
212
213   getImagesSrc(imageName : string) : string {
214     return './' + imageName + '.svg';
215   }
216
217   private getHeaderHeaderClientRect(): ClientRect {
218     const element = document.querySelector("#instantiation-status thead") as HTMLElement;
219     return element.getBoundingClientRect();
220   }
221
222   findFirstVisibleJob(): HTMLElement {
223     const elements : any = document.querySelectorAll('#instantiation-status tr');
224     const headerRect = this.getHeaderHeaderClientRect();
225     if (headerRect) {
226       const topEdge = headerRect.bottom;
227       for (let i = 0; i < elements.length; i++) {
228         if (elements[i].getBoundingClientRect().top >= topEdge)
229           return elements[i];
230       }
231     }
232     return null;
233   }
234
235   scrollToElement(currentJob: HTMLElement) {
236     if (currentJob) {
237       const config: ScrollToConfigOptions = {
238         target: currentJob,
239         duration: 0,
240         offset: -1 * (this.getHeaderHeaderClientRect().height + 2),
241       };
242
243       // wait after render
244       setTimeout(() => {
245         this._scrollToService.scrollTo(config);
246       }, 0)
247     }
248   }
249
250   isInstantiationStatusFilterFlagOn() {
251     return FeatureFlagsService.getFlagState(Features.FLAG_2004_INSTANTIATION_STATUS_FILTER, this._store);
252   }
253 }