blob: f3f651960349ec27809126eb05bb1df3451a5750 [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {Component, ViewChild} from '@angular/core';
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03002import {ServiceInfoService} from '../shared/server/serviceInfo/serviceInfo.service';
3import {ServiceInfoModel} from '../shared/server/serviceInfo/serviceInfo.model';
4import {InstantiationStatusComponentService} from './instantiationStatus.component.service';
5import {ContextMenuComponent, ContextMenuService} from 'ngx-contextmenu';
Ittay Stern6f900cc2018-08-29 17:01:32 +03006import {AuditInfoModalComponent} from "../shared/components/auditInfoModal/auditInfoModal.component";
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03007import * as _ from 'lodash';
8import {ScrollToConfigOptions, ScrollToService} from '@nicky-lenaers/ngx-scroll-to';
Ittay Stern6f900cc2018-08-29 17:01:32 +03009import {ConfigurationService} from "../shared/services/configuration.service";
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030010import {LogService} from '../shared/utils/log/log.service';
Ittay Stern6f900cc2018-08-29 17:01:32 +030011import {AppState} from "../shared/store/reducers";
12import {NgRedux} from '@angular-redux/store';
13import {JobStatus, ServiceAction} from "../shared/models/serviceInstanceActions";
14import {DrawingBoardModes} from "../drawingBoard/service-planning/drawing-board.modes";
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030015
Ittay Stern6f900cc2018-08-29 17:01:32 +030016export interface MenuAction{
17 name: string;
18 dataTestId: string;
19 className: string;
20 tooltip?: string;
21 click(item: ServiceInfoModel): void;
22 enabled (item?: ServiceInfoModel): boolean;
23 visible (item?: ServiceInfoModel): boolean;
24}
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030025
26@Component({
27 selector : 'instantiation-status',
28 templateUrl : './instantiationStatus.component.html',
29 styleUrls : ['./instantiationStatus.component.scss']
30})
Ittay Stern6f900cc2018-08-29 17:01:32 +030031export class InstantiationStatusComponent {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030032
33 TIMER_TIME_IN_SECONDS : number = 0;
34 timer = null;
35 dataIsReady : boolean = false;
36 scroll : boolean = false;
37 lastUpdatedDate: Date = null;
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030038 instantiationStatusComponentService: InstantiationStatusComponentService;
39 configurationService : ConfigurationService;
Ittay Stern6f900cc2018-08-29 17:01:32 +030040 serviceInfoData: ServiceInfoModel[] = null;
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030041 @ViewChild(ContextMenuComponent) public contextMenu: ContextMenuComponent;
42
Ittay Stern6f900cc2018-08-29 17:01:32 +030043 public contextMenuActions: Array<MenuAction> = [
44 {
45 name: "Redeploy",
46 dataTestId: "context-menu-retry",
47 className: "fa-repeat",
48 click: (item: ServiceInfoModel) => this.retryItem(item),
49 enabled: () => true,
50 visible: (item: ServiceInfoModel) => item.isRetryEnabled,
51 },
52 {
53 name: "Open",
54 dataTestId: "context-menu-open",
55 className: "fa-external-link",
56 click: (item: ServiceInfoModel) => this.instantiationStatusComponentService.open(item),
57 enabled: (item: ServiceInfoModel) => this.isOpenEnabled(item),
58 visible: () => true,
59 },
60 {
61 name: "Audit info",
62 dataTestId: "context-menu-audit-info",
63 className: "fa-info-circle",
64 click: (item: ServiceInfoModel) => this.auditInfo(item),
65 enabled: (item: ServiceInfoModel) => this.isAuditInfoEnabled(item),
66 visible: () => true,
67 },
68 {
69 name: "Delete",
70 dataTestId: "context-menu-remove",
71 className: "fa-trash-o",
72 click: (item: ServiceInfoModel) => this.deleteItem(item),
73 enabled: (item: ServiceInfoModel) => this.isDeleteEnabled(item),
74 visible: () => true,
75 },
76 {
77 name: "Hide request",
78 dataTestId: "context-menu-hide",
79 className: "fa-eye-slash",
80 tooltip: "Hide this service from this table",
81 click: (item: ServiceInfoModel) => this.hideItem(item),
82 enabled: (item: ServiceInfoModel) => this.isHideEnabled(item),
83 visible: () => true,
84 }
85 ];
86
87 flags: any;
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030088 constructor(private _serviceInfoService: ServiceInfoService,
89 private _instantiationStatusComponentService : InstantiationStatusComponentService,
90 private _contextMenuService: ContextMenuService,
91 private _configurationService : ConfigurationService,
92 private _scrollToService: ScrollToService,
Ittay Stern6f900cc2018-08-29 17:01:32 +030093 private _logService : LogService,
94 private _store: NgRedux<AppState>) {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030095 this.instantiationStatusComponentService = _instantiationStatusComponentService;
96 this.configurationService = this._configurationService;
97 this.configurationService.getConfiguration("refreshTimeInstantiationDashboard").subscribe(response => {
98 this.TIMER_TIME_IN_SECONDS = _.isNumber(response) ? response : 0;
99 this.activateInterval();
100 this.refreshData();
101 });
102 }
103
104 activateInterval() {
105 if (this.TIMER_TIME_IN_SECONDS > 0) {
106 this.timer = setInterval(() => {
107 this.refreshData();
108 }, this.TIMER_TIME_IN_SECONDS * 1000);
109 }
110 }
111
112 deactivateInterval() {
113 clearInterval(this.timer);
114 }
115
116 refreshData(): void {
117 this.dataIsReady = false;
Ittay Stern6f900cc2018-08-29 17:01:32 +0300118 this._serviceInfoService.getServicesJobInfo(true, this.lastUpdatedDate === null)
119 .subscribe((res: ServiceInfoModel[]) => {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300120 this._instantiationStatusComponentService.convertObjectToArray(res).subscribe((res) => {
121 this._logService.info('refresh instantiation status table', res);
122 this.dataIsReady = true;
123 this.lastUpdatedDate = new Date();
124 if (!_.isEqual(this.serviceInfoData, res)) {
125 this.serviceInfoData = res;
Ittay Stern6f900cc2018-08-29 17:01:32 +0300126 this.scrollToElement(this.findFirstVisibleJob());
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300127 }
128 });
129 })
130 }
131
Ittay Stern6f900cc2018-08-29 17:01:32 +0300132 trackByFn(index: number, item: ServiceInfoModel){
133 return _.isNil(item) ? null : item.jobId;
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300134 }
135
Ittay Stern6f900cc2018-08-29 17:01:32 +0300136 deleteItem(item: ServiceInfoModel): void {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300137 this._serviceInfoService.deleteJob(item.jobId).subscribe(() => {
138 this.refreshData();
139 });
140 }
141
Ittay Stern6f900cc2018-08-29 17:01:32 +0300142 hideItem(item: ServiceInfoModel): void {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300143 this._serviceInfoService.hideJob(item.jobId).subscribe(() => {
144 this.refreshData();
145 });
146 }
Ittay Stern6f900cc2018-08-29 17:01:32 +0300147
148 retryItem(item: ServiceInfoModel) : void {
149 if (item.isRetryEnabled) {
150 this._instantiationStatusComponentService.retry(item);
151 }
152 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300153
154 auditInfo(jobData : ServiceInfoModel): void {
155 AuditInfoModalComponent.openModal.next(jobData);
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300156 }
157
Ittay Stern6f900cc2018-08-29 17:01:32 +0300158 isOpenEnabled(item: ServiceInfoModel):boolean {
159 switch(item.action) {
160 case ServiceAction.DELETE:
161 return _.includes([ JobStatus.PENDING, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.FAILED], item.jobStatus);
162 case ServiceAction.UPDATE:
163 return _.includes([JobStatus.PENDING, JobStatus.PAUSE, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.COMPLETED, JobStatus.FAILED], item.jobStatus);
164 default:
165 return _.includes([JobStatus.COMPLETED, JobStatus.PAUSE, JobStatus.COMPLETED_WITH_ERRORS], item.jobStatus);
166 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300167 }
168
Ittay Stern6f900cc2018-08-29 17:01:32 +0300169 isAuditInfoEnabled(item: ServiceInfoModel): boolean {
170 if(item.action === ServiceAction.DELETE || item.action=== ServiceAction.UPDATE) {
171 return _.includes([JobStatus.FAILED, JobStatus.IN_PROGRESS, JobStatus.COMPLETED_WITH_ERRORS, JobStatus.PAUSE, JobStatus.COMPLETED], item.jobStatus);
172 }
173 return true;// ServiceAction.INSTANTIATE
174 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300175
Ittay Stern6f900cc2018-08-29 17:01:32 +0300176 isDeleteEnabled(item: ServiceInfoModel):boolean {
177 if( item.action === ServiceAction.DELETE || item.action === ServiceAction.UPDATE){
178 return _.includes([JobStatus.PENDING], item.jobStatus);
179 }
180 return _.includes([JobStatus.PENDING, JobStatus.STOPPED], item.jobStatus);
181 }
182
183 isHideEnabled(item: ServiceInfoModel):boolean {
184 return _.includes([JobStatus.COMPLETED, JobStatus.FAILED, JobStatus.STOPPED, JobStatus.COMPLETED_WITH_ERRORS], item.jobStatus);
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300185 }
186
187 public onContextMenu($event: MouseEvent, item: any): void {
188 this._contextMenuService.show.next({
189 contextMenu: this.contextMenu,
190 event: $event,
191 item: item,
192 });
193 $event.preventDefault();
194 $event.stopPropagation();
195 }
196
197 getImagesSrc(imageName : string) : string {
198 return './' + imageName + '.svg';
199 }
200
Ittay Stern6f900cc2018-08-29 17:01:32 +0300201 private getHeaderHeaderClientRect(): ClientRect {
202 const element = document.querySelector("#instantiation-status thead") as HTMLElement;
203 return element.getBoundingClientRect();
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300204 }
Ittay Stern6f900cc2018-08-29 17:01:32 +0300205
206 findFirstVisibleJob(): HTMLElement {
207 const elements : any = document.querySelectorAll('#instantiation-status tr');
208 const headerRect = this.getHeaderHeaderClientRect();
209 if (headerRect) {
210 const topEdge = headerRect.bottom;
211 for (let i = 0; i < elements.length; i++) {
212 if (elements[i].getBoundingClientRect().top >= topEdge)
213 return elements[i];
214 }
215 }
216 return null;
217 }
218
219 scrollToElement(currentJob: HTMLElement) {
220 if (currentJob) {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300221 const config: ScrollToConfigOptions = {
Ittay Stern6f900cc2018-08-29 17:01:32 +0300222 target: currentJob,
223 duration: 0,
224 offset: -1 * (this.getHeaderHeaderClientRect().height + 2),
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300225 };
Ittay Stern6f900cc2018-08-29 17:01:32 +0300226
227 // wait after render
228 setTimeout(() => {
229 this._scrollToService.scrollTo(config);
230 }, 0)
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +0300231 }
232 }
233}