added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / test-instances-catalog / test-instances-catalog.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, OnInit, ViewChild, ViewContainerRef, AfterViewInit, OnDestroy, ViewChildren, ElementRef, QueryList } from '@angular/core';\r
18 import { MatDialog, MatPaginator, MatSnackBar, MatTableDataSource, MatListItem } from '@angular/material';\r
19 import { ActivatedRoute, Router } from '@angular/router';\r
20 import { ListService } from '../../shared/services/list.service';\r
21 import { AlertModalComponent } from '../../shared/modules/alert-modal/alert-modal.component';\r
22 import { TestInstanceService } from '../../shared/services/test-instance.service';\r
23 import { routerTransition } from 'app/router.animations';\r
24 import { SchedulingService } from '../../shared/services/scheduling.service';\r
25 import { TestInstanceModalComponent } from '../../shared/modules/test-instance-modal/test-instance-modal.component';\r
26 import { AlertSnackbarComponent } from '../../shared/modules/alert-snackbar/alert-snackbar.component';\r
27 import { ScheduleTestModalComponent } from '../../shared/modules/schedule-test-modal/schedule-test-modal.component';\r
28 import { animate, state, style, transition, trigger } from '@angular/animations';\r
29 import { TestDefinitionService } from '../../shared/services/test-definition.service';\r
30 import { Observable, Subscription } from 'rxjs';\r
31 import { ExecuteService } from 'app/shared/services/execute.service';\r
32 import { GroupService } from 'app/shared/services/group.service';\r
33 import { GridOptions } from "ag-grid-community";\r
34 \r
35 \r
36 @Component({\r
37     selector: 'app-test-instances-catalog',\r
38     templateUrl: './test-instances-catalog.component.pug',\r
39     styleUrls: ['./test-instances-catalog.component.scss'],\r
40     animations: [routerTransition(),\r
41     trigger('detailExpand', [\r
42         state('collapsed', style({ height: '0px', minHeight: '0', display: 'none' })),\r
43         state('expanded', style({ height: '*' })),\r
44         transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),\r
45     ])\r
46     ]\r
47 })\r
48 export class TestInstancesCatalogComponent implements OnInit, AfterViewInit, OnDestroy {\r
49 \r
50 \r
51     public resultsLength;\r
52     public loading = false;\r
53 \r
54 \r
55     public columnDefs = [\r
56         {headerName: '', field: 'disabled', cellRenderer: this.disabledIndicator, width: 100, hide: false, checkboxSelection:true, headerCheckboxSelection: true, headerCheckboxSelectionFilteredOnly: true},\r
57         {headerName: 'Name', field: 'testInstanceName', sortable: true, filter: true, resizable: true, width: 300},\r
58         {headerName: 'Description', field: 'testInstanceDescription', sortable: true, filter: true, resizable: true, width: 100},\r
59         {headerName: 'Id', field: '_id', sortable: true, filter: true, resizable: true, width: 200, editable: true},\r
60         {headerName: 'Test Definition', field: 'testDefinitionId.testName', sortable: true, filter: true, resizable: true}\r
61         \r
62       ];\r
63 \r
64     public selectedRows;\r
65     public hasSelectedRows;\r
66     public selectedSingleRow;\r
67     private gridApi;\r
68     private gridColumnApi;\r
69     public selectedUnlockedRows;\r
70     public rowData;\r
71 \r
72     public gridOptions: GridOptions;\r
73 \r
74     \r
75     public params;\r
76 \r
77     private subscriptions: Subscription[] = [];\r
78 \r
79     @ViewChild(MatPaginator) paginator: MatPaginator;\r
80 \r
81     constructor(\r
82 \r
83         private router: Router,\r
84         private viewRef: ViewContainerRef,\r
85         private testInstance: TestInstanceService,\r
86         private modal: MatDialog,\r
87         private schedulingService: SchedulingService,\r
88         private snack: MatSnackBar,\r
89         private route: ActivatedRoute,\r
90         private testDefinitionService: TestDefinitionService,\r
91         private _execute: ExecuteService,\r
92         private _groups: GroupService\r
93     ) {  }\r
94 \r
95     ngOnInit() {\r
96         \r
97         this.setComponentData(this._groups.getGroup());\r
98 \r
99         this.subscriptions.push(this._groups.groupChange().subscribe(group => {\r
100             this.setComponentData(group);\r
101             \r
102         }));\r
103 \r
104         // this.subscriptions.push(this._groups.groupChange().subscribe( group => {\r
105         //     if(!group["_id"]){\r
106         //         this.setComponentData(this._groups.getGroup());\r
107         //     }\r
108         //     this.setComponentData(group);\r
109         // }));\r
110 \r
111 \r
112         this.route.queryParams.subscribe( params => {\r
113            \r
114             this.params = params;\r
115 \r
116 \r
117         });\r
118 \r
119 \r
120     }\r
121 \r
122     setComponentData(group) {\r
123 \r
124         if(!group){\r
125             return;\r
126         }\r
127         this.loading = true;\r
128         let params = {\r
129             groupId: group['_id'],\r
130             $limit: -1,\r
131             $populate: ['testDefinitionId'],\r
132             $sort: {\r
133                 createdAt: -1\r
134             },\r
135             $select: ['testInstanceName', 'testInstanceDescription', 'testDefinitionId.testName', 'disabled']\r
136         }\r
137 \r
138         if (this.route.snapshot.params['filter']) {\r
139             params['_id'] = this.route.snapshot.params['filter'];\r
140         }\r
141 \r
142         this.testInstance.find(params).subscribe((list) => {\r
143             this.resultsLength = list['length'];\r
144             this.loading = false;\r
145             this.rowData = list;\r
146         },\r
147         err => {\r
148             console.log(err);\r
149         });\r
150         \r
151     }\r
152 \r
153     ngAfterViewInit() {\r
154    \r
155     }\r
156 \r
157     ngOnDestroy() {\r
158         this.subscriptions.forEach(e => e.unsubscribe());\r
159     }\r
160 \r
161 \r
162     schedule() {\r
163         this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName, testDefinitionId}) => ({_id, testInstanceName, testDefinitionId}));\r
164         console.log("The new element is: "+JSON.stringify(this.selectedRows[0]._id));\r
165         \r
166         console.log("Here is the selected Row: "+JSON.stringify(this.gridApi.getSelectedRows()[0]));\r
167         const dialogRef = this.modal.open(ScheduleTestModalComponent, {\r
168             width: '90%',\r
169             data: {\r
170                 id: this.selectedRows[0]._id\r
171             }\r
172         });\r
173 \r
174         dialogRef.afterClosed().subscribe(result => {\r
175             /*if(result != ''){\r
176               this.test_instance_selected = result;\r
177               this.strategy_selected = true;\r
178             }else{\r
179               this.strategy_selected = false;\r
180             }*/\r
181         });\r
182     }\r
183 \r
184     executeMultipleTestInstance(){\r
185         for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
186             this.executeTestInstance(i);\r
187         }\r
188     }\r
189 \r
190     executeTestInstance(ti) {\r
191         \r
192         this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName, testDefinitionId}) => ({_id, testInstanceName, testDefinitionId}));\r
193 \r
194 \r
195         if (this.selectedRows[ti].testDefinitionId) {\r
196             const executer = this.modal.open(AlertModalComponent, {\r
197                 width: '250px',\r
198                 data: {\r
199                     type: 'confirmation',\r
200                     message: 'Are you sure you want to run ' + this.selectedRows[ti].testInstanceName + '?'\r
201                 }\r
202             });\r
203 \r
204             executer.afterClosed().subscribe(result => {\r
205                 if (result) {\r
206                     this._execute.create({\r
207                         _id: this.selectedRows[ti]._id,\r
208                         async: true\r
209                     }).subscribe((result) => {\r
210                         console.log(result);\r
211                         if (result) {\r
212                             this.snack.openFromComponent(AlertSnackbarComponent, {\r
213                                 duration: 1500,\r
214                                 data: {\r
215                                     message: 'Test Instance Executed'\r
216                                 }\r
217                             });\r
218                         }\r
219                     },\r
220                         (error) => {\r
221                             console.log(error);\r
222                             this.modal.open(AlertModalComponent, {\r
223                                 width: '450px',\r
224                                 data: {\r
225                                     type: 'Alert',\r
226                                     message: 'Failed to execute Test Instance!\n' + JSON.stringify(error)\r
227                                 }\r
228                             });\r
229                         })\r
230                 }\r
231             });\r
232         }\r
233 \r
234     }\r
235 \r
236     createTestInstance() {\r
237         const create = this.modal.open(TestInstanceModalComponent, {\r
238             width: '90%',\r
239             data: null,\r
240             disableClose: true\r
241         });\r
242 \r
243         create.afterClosed().subscribe(result => {\r
244           this.ngOnInit();\r
245         });\r
246     }\r
247 \r
248 \r
249     editTestInstance() {\r
250         this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName}) => ({_id, testInstanceName}));\r
251 \r
252         const edit = this.modal.open(TestInstanceModalComponent, {\r
253             data: {\r
254                 ti: this.selectedRows[0]._id,\r
255                 isEdit: true\r
256             },\r
257             width: '90%',\r
258             disableClose: true\r
259         });\r
260 \r
261         edit.afterClosed().subscribe(result => {\r
262 \r
263         });\r
264     }\r
265 \r
266     cloneTestInstance() {\r
267         this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName}) => ({_id, testInstanceName}));\r
268         this.testInstance.get(this.selectedRows[0]._id).subscribe(\r
269             result => {\r
270                 var temp = Object.assign({}, result);\r
271                 delete result['_id'];\r
272                 delete result['createdAt'];\r
273                 delete result['updatedAt'];\r
274                 if (this.selectedRows[0].testInstanceName) {\r
275                     result['testInstanceName'] = this.selectedRows[0].testInstanceName + '_Clone';\r
276                 } else {\r
277                     result['testInstanceName'] = result['testInstanceName'] + '_Clone';\r
278                 }\r
279                 this.testInstance.create(result).subscribe(\r
280                     resp => {\r
281                         //this.editTestInstance(resp);\r
282                         this.editTestInstance();\r
283                     },\r
284                     err => {\r
285                         if (err) {\r
286                       \r
287                             result['_id'] = temp['_id'];\r
288                            \r
289                             //this.cloneTestInstance(result);\r
290                         }\r
291                     }\r
292                 );\r
293             }\r
294         )\r
295     }\r
296 \r
297     deleteMultipleTestInstance(){\r
298         for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
299             this.deleteTestInstance(i);\r
300         }\r
301     }\r
302 \r
303     deleteTestInstance(ti) {\r
304         this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName}) => ({_id, testInstanceName}));\r
305 \r
306 \r
307         const deleter = this.modal.open(AlertModalComponent, {\r
308             width: '250px',\r
309             data: {\r
310                 type: 'confirmation',\r
311                 message: 'Are you sure you want to delete ' + this.selectedRows[ti].testInstanceName + ' ? Executions of this instance will no longer display everything.'\r
312             }\r
313         });\r
314 \r
315         deleter.afterClosed().subscribe(result => {\r
316             if (result) {\r
317                 this.testInstance.delete(this.selectedRows[ti]._id).subscribe(response => {\r
318                     this.snack.openFromComponent(AlertSnackbarComponent, {\r
319                         duration: 1500,\r
320                         data: {\r
321                             message: 'Test Instance Deleted'\r
322                         }\r
323                     });\r
324 \r
325                 });\r
326                 this.setComponentData(this._groups.getGroup());\r
327             }\r
328         });\r
329         \r
330     }\r
331 \r
332 \r
333     disabledIndicator(params){\r
334         if (params.value){\r
335           return `<mat-icon class="mat-icon mat-icon-no-color" role="img" >\r
336            locked</mat-icon>`;   \r
337     }\r
338 \r
339  \r
340     }\r
341 \r
342     setParams(element) {\r
343    \r
344         if (JSON.stringify(element) == JSON.stringify({testInstanceId: this.params.testInstanceId})){\r
345             element = {};\r
346         }\r
347 \r
348         \r
349         this.router.navigate([], {\r
350            //queryParams: {testInstanceId: element.testInstanceId, page: this.paginator.pageIndex, instancePerPage: this.paginator.pageSize }\r
351             queryParams: {testInstanceId: element._id}\r
352         })\r
353            \r
354     }\r
355 \r
356     onGridReady(params){\r
357         this.gridApi = params.api;\r
358         console.log(params.columnApi.autoSizeColumns)\r
359         this.gridColumnApi = params.columnApi;\r
360     \r
361         //auto size the column widths\r
362         this.gridColumnApi.autoSizeColumns(['name']);\r
363 \r
364       }\r
365 \r
366       selectActiveInstance($event){\r
367         if(this.params.testInstanceId)\r
368         {\r
369             this.gridApi.forEachNode( (node, index) => {\r
370 \r
371                 if(node.data._id ==this.params.testInstanceId)\r
372                 {\r
373                     // Pre selects the row that was last selected when on the page\r
374                     node.setSelected(true, true);\r
375                     //Vertically scrolls to that row so it is visible\r
376                     this.gridApi.ensureIndexVisible(index, "middle");\r
377                     \r
378                 }\r
379               \r
380             });\r
381         }\r
382         \r
383       }\r
384 \r
385 \r
386     onRowSelected(event){\r
387 \r
388         this.selectedRows = this.gridApi.getSelectedRows().map(({ _id, disabled, testInstanceName  }) => ({ _id, disabled, testInstanceName}));\r
389 \r
390 \r
391         \r
392         if(event.api.getSelectedNodes().length > 0){\r
393           this.hasSelectedRows = true;\r
394           \r
395             //Checks for all Unlocked rows\r
396             for (let i = 0; i < event.api.getSelectedNodes().length; i++ )\r
397             {\r
398                 if(!this.selectedRows[i].disabled)\r
399                 {\r
400                     this.selectedUnlockedRows = true;\r
401                 }\r
402                 else{\r
403                     this.selectedUnlockedRows = false;\r
404                     break;\r
405                 }\r
406             }\r
407         }\r
408         else{\r
409           this.hasSelectedRows = false;\r
410           this.selectedUnlockedRows = false;\r
411           \r
412           this.setParams({_id: null});\r
413         }\r
414 \r
415         \r
416         //One Row was selected\r
417         if((event.api.getSelectedNodes().length == 1)){\r
418           this.selectedSingleRow = true;\r
419          \r
420           this.setParams({_id: this.selectedRows[0]._id});\r
421          \r
422         }else{\r
423           this.selectedSingleRow = false;\r
424           this.setParams({_id: null});\r
425         }\r
426        \r
427     }\r
428 \r
429 \r
430 \r
431       \r
432 \r
433 \r
434 \r
435     \r
436 }\r