added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / test-executions-catalog / test-executions-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, OnDestroy } from '@angular/core';\r
18 import { MatPaginator, MatDialog, MatTableDataSource, MatSnackBar } from '@angular/material';\r
19 import { Router } from '@angular/router';\r
20 import { ActivatedRoute } from '@angular/router';\r
21 import { ListService } from 'app/shared/services/list.service';\r
22 import { TestInstanceService } from 'app/shared/services/test-instance.service';\r
23 import { AlertModalComponent } from 'app/shared/modules/alert-modal/alert-modal.component';\r
24 import { TestExecutionService } from 'app/shared/services/test-execution.service';\r
25 import { routerTransition } from 'app/router.animations';\r
26 import { AlertSnackbarComponent } from 'app/shared/modules/alert-snackbar/alert-snackbar.component';\r
27 import { TestDefinitionService } from 'app/shared/services/test-definition.service';\r
28 import { GroupService } from 'app/shared/services/group.service';\r
29 import { Subscription } from 'rxjs';\r
30 \r
31 @Component({\r
32   selector: 'app-test-executions-catalog',\r
33   templateUrl: './test-executions-catalog.component.pug',\r
34   styleUrls: ['./test-executions-catalog.component.scss'],\r
35   animations: [routerTransition()]\r
36 })\r
37 export class TestExecutionsCatalogComponent implements OnInit, OnDestroy {\r
38 \r
39   private toDestroy: Array<Subscription> = [];\r
40   public dataSource;\r
41   public displayedColumns: string[] = ['testInstanceName', 'testInstanceDescription', 'result', 'totalTime', 'options'];\r
42   public resultsLength;\r
43   public loading = false;\r
44 \r
45   @ViewChild(MatPaginator) paginator: MatPaginator;\r
46 \r
47   constructor(\r
48     private list: ListService,\r
49     private testExecution: TestExecutionService,\r
50     private modal: MatDialog,\r
51     private route: ActivatedRoute,\r
52     private _groups: GroupService,\r
53     private snack: MatSnackBar\r
54   ) {\r
55   }\r
56 \r
57   ngOnInit() {\r
58     this.setComponentData(this._groups.getGroup());\r
59     this.toDestroy.push(this._groups.groupChange().subscribe(group => {\r
60       this.setComponentData(group);\r
61     }));\r
62   }\r
63 \r
64   ngOnDestroy() {\r
65     this.toDestroy.forEach(e => e.unsubscribe());\r
66   }\r
67 \r
68   setComponentData(group) {\r
69     if (!group) {\r
70       return;\r
71     }\r
72     this.loading = true;\r
73 \r
74     this.dataSource = new MatTableDataSource();\r
75     this.dataSource.paginator = this.paginator;\r
76 \r
77     //RG: Hard limit returns object, -1 returns array\r
78     const params = { $limit: 50, groupId: group._id, $populate: ['testInstanceId'], $sort: { startTime: -1 } }//['$limit=-1', '$populate[]=testInstanceId', '$sort[startTime]=-1'];\r
79     if (this.route.snapshot.params['filter']) {\r
80       params['testResult'] = this.route.snapshot.params['filter'].toUpperCase();\r
81     }\r
82     this.testExecution.find(params).subscribe((response) => {\r
83 \r
84       let list = response;\r
85       //RG: check if hard limit if so it will be object w/ prop data\r
86       if(!Array.isArray(response) && response.hasOwnProperty('data')){\r
87         list = response['data'];\r
88       }\r
89       for (let i = 0; i < list['length']; i++) {\r
90         const tsDate = new Date(list[i]['startTime']);\r
91         const teDate = new Date(list[i]['endTime']);\r
92         list[i]['totalTime'] = (teDate.getTime() - tsDate.getTime()) / 1000;\r
93       }\r
94       this.dataSource.data = list;\r
95       this.resultsLength = this.dataSource.data.length;\r
96       this.loading = false;\r
97     });\r
98 \r
99   }\r
100 \r
101   applyFilter(filterValue: string) {\r
102     this.dataSource.filter = filterValue.trim().toLowerCase();\r
103   }\r
104 \r
105   createTestInstance() {\r
106     // const create = this.modal.open(TestDefinition, {\r
107     //   width: '450px',\r
108     //   data: {\r
109     //     goal: 'create'\r
110     //   }\r
111     // });\r
112 \r
113     // create.afterClosed().subscribe(result => {\r
114     //   this.list.listMap['vth'].currentList.subscribe(x => {\r
115     //     this.dataSource = x;\r
116     //   });\r
117     // });\r
118   }\r
119 \r
120 \r
121   editTestInstance(th) {\r
122     // const edit = this.modal.open(TestHeadModalComponent, {\r
123     //   width: '450px',\r
124     //   data: {\r
125     //     goal: 'edit',\r
126     //     testHead: th\r
127     //   }\r
128     // });\r
129 \r
130     // edit.afterClosed().subscribe(result => {\r
131     //   console.log(result);\r
132     // });\r
133   }\r
134 \r
135   deleteTestInstance(te) {\r
136     const deleter = this.modal.open(AlertModalComponent, {\r
137       width: '250px',\r
138       data: {\r
139         type: 'confirmation',\r
140         message: 'Are you sure you want to delete ' + te.testExecutionName + ' ?'\r
141       }\r
142     });\r
143 \r
144     deleter.afterClosed().subscribe(result => {\r
145       if (result) {\r
146         this.testExecution.delete(te._id).subscribe(response => {\r
147           this.snack.openFromComponent(AlertSnackbarComponent, {\r
148             duration: 1500,\r
149             data: {\r
150               message: 'Test Execution Deleted'\r
151             }\r
152           });\r
153           this.list.removeElement('te', '_id', te._id + '');\r
154           this.list.listMap['te'].currentList.subscribe(x => {\r
155             this.dataSource.data = x;\r
156             this.resultsLength = x.length;\r
157           });\r
158         });\r
159       }\r
160     });\r
161   }\r
162 }\r