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