added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / test-definition-expanded-details / test-definition-expanded-details.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, Input, ViewChild, HostListener, AfterContentInit, AfterViewInit, ElementRef, OnDestroy } from '@angular/core';\r
18 import { TestDefinitionService } from 'app/shared/services/test-definition.service';\r
19 import { TestInstanceService } from 'app/shared/services/test-instance.service';\r
20 import { MatTableDataSource, MatPaginator, MatDialog, MatSnackBar } from '@angular/material';\r
21 import Modeler from 'bpmn-js';\r
22 import { timeInterval } from 'rxjs/operators';\r
23 import { Observable } from 'rxjs';\r
24 import { TestExecutionService } from 'app/shared/services/test-execution.service';\r
25 import { AlertModalComponent } from 'app/shared/modules/alert-modal/alert-modal.component';\r
26 import { AlertSnackbarComponent } from 'app/shared/modules/alert-snackbar/alert-snackbar.component';\r
27 import { SchedulingService } from 'app/shared/services/scheduling.service';\r
28 import { FileTransferService } from 'app/shared/services/file-transfer.service';\r
29 import { Buffer } from 'buffer';\r
30 import { ViewWorkflowModalComponent } from 'app/shared/modules/view-workflow-modal/view-workflow-modal.component';\r
31 import { ExecuteService } from 'app/shared/services/execute.service';\r
32 import { BpmnFactoryService } from 'app/shared/factories/bpmn-factory.service';\r
33 \r
34 @Component({\r
35   selector: 'app-test-definition-expanded-details',\r
36   templateUrl: './test-definition-expanded-details.component.pug',\r
37   styleUrls: ['./test-definition-expanded-details.component.scss']\r
38 })\r
39 export class TestDefinitionExpandedDetailsComponent implements OnInit, OnDestroy {\r
40 \r
41   @Input() public testDefinitionId;\r
42 \r
43   @Input() public events: Observable<void>;\r
44   \r
45   @ViewChild(MatPaginator) instancePaginator: MatPaginator;\r
46   @ViewChild('canvas') canvas: ElementRef;\r
47 \r
48   public data = null;\r
49   public dataLength = 0;\r
50   public displayedColumns;\r
51   public foundStatuses = ['name'];\r
52   public statusList = ['COMPLETED', 'SUCCESS', 'UNKNOWN', 'FAILURE', 'STOPPED', 'UNAUTHORIZED', 'FAILED'];\r
53   public testInstanceList = null;\r
54   public testExecutionList = [];\r
55   public viewer;\r
56   public eventSub;\r
57   public bpmnXml;\r
58 \r
59   constructor(\r
60     private bpmnFactory: BpmnFactoryService, \r
61     private fileTransfer: FileTransferService, \r
62     private dialog: MatDialog, \r
63     private testDefinition: TestDefinitionService, \r
64     private testInstance: TestInstanceService, \r
65     private testExecution: TestExecutionService, \r
66     private execute: ExecuteService, \r
67     private modal: MatDialog, \r
68     private snack: MatSnackBar\r
69   ) { }\r
70 \r
71   async ngOnInit() {\r
72     \r
73     await this.testDefinition.get(this.testDefinitionId).subscribe(\r
74       result => {\r
75         result['createdAt'] = new Date(result['createdAt']).toLocaleString();\r
76         result['updatedAt'] = new Date(result['updatedAt']).toLocaleString();\r
77         this.data = result;\r
78         if(this.data.bpmnInstances){\r
79           this.bpmnFactory.setup({\r
80             mode: 'viewer',\r
81             options: {\r
82               container: this.canvas.nativeElement\r
83             },\r
84             fileId: this.data.bpmnInstances[0].bpmnFileId\r
85           }).then(res => {\r
86             this.viewer = res;\r
87           });\r
88           // this.loadDiagram();\r
89         }\r
90       }\r
91     );\r
92 \r
93     this.testInstanceList = new MatTableDataSource();\r
94     this.testInstance.find({\r
95       $limit: -1,\r
96       $sort: {\r
97         createdAt: -1\r
98       },\r
99       testDefinitionId: this.testDefinitionId\r
100     }).subscribe(\r
101       result => {\r
102         this.testInstanceList.data = result;\r
103         this.testInstanceList.paginator = this.instancePaginator;\r
104 \r
105         this.testInstanceList.data.forEach(elem => {\r
106           this.setExecutions(elem._id);\r
107         });\r
108 \r
109         this.displayedColumns = ['name', 'COMPLETED', 'SUCCESS', 'UNKNOWN', 'FAILURE', 'STOPPED', 'UNAUTHORIZED', 'FAILED', 'options'];\r
110 \r
111       }\r
112     )\r
113 \r
114     //If parent emeits, diagram will reload\r
115     if(this.events != undefined && this.events){\r
116       this.events.subscribe(() => {\r
117         setTimeout(() => {\r
118           this.loadDiagram();\r
119         }, 500)\r
120       });\r
121     }\r
122   }\r
123 \r
124   enlargeBpmn(){\r
125     this.dialog.open(ViewWorkflowModalComponent, {\r
126       data: {\r
127         xml: this.viewer.getBpmnXml()\r
128       },\r
129       width: '100%',\r
130       height: '100%'\r
131     })\r
132   }\r
133 \r
134   ngOnDestroy() {\r
135     delete this.events;\r
136   }\r
137 \r
138   async setExecutions(instanceId) {\r
139     // ['$limit=-1', '$sort[startTime]=-1', 'testInstanceId=' + instanceId]\r
140     this.testExecution.find({\r
141       $limit: -1,\r
142       $sort: {\r
143         startTime: -1\r
144       },\r
145       'historicTestInstance._id': instanceId\r
146     }).subscribe(\r
147       result => {\r
148         for(let i = 0; i < result['length']; i++){\r
149           result[i].startTime = new Date(result[i].startTime).toLocaleString();\r
150           this.testExecutionList.push(result[i]);\r
151           for(let j = 0; j < this.testInstanceList.data.length; j++){\r
152             if(this.testInstanceList.data[j]._id == instanceId){\r
153               if(!this.testInstanceList.data[j][result[i]['testResult']]){\r
154                 this.testInstanceList.data[j][result[i]['testResult']] = 1;\r
155               }else{\r
156                 this.testInstanceList.data[j][result[i]['testResult']] += 1;\r
157               }\r
158             }\r
159           }\r
160         }\r
161 \r
162         //this.setDisplayColumns();\r
163         // for(let i = 0; i < result[i]; i++){\r
164         //   this.testInstanceList[instanceId] = result;\r
165         // }\r
166         \r
167       }\r
168     );\r
169   }\r
170 \r
171   loadDiagram(){\r
172     if(this.viewer && this.data && this.data.bpmnInstances){\r
173       this.viewer.renderDiagram();\r
174     }\r
175   }\r
176 \r
177   executeTestInstance(element){\r
178     (element);\r
179     if(element.testDefinitionId){\r
180       const executer = this.modal.open(AlertModalComponent, {\r
181         width: '250px',\r
182         data: {\r
183           type: 'confirmation',\r
184           message: 'Are you sure you want to run ' + element.testInstanceName + '?'\r
185         }\r
186       });\r
187 \r
188       executer.afterClosed().subscribe(result => {\r
189         if(result){\r
190           this.execute.create({\r
191             _id : element._id,\r
192             async: true\r
193           }).subscribe((result) => {\r
194             this.snack.openFromComponent(AlertSnackbarComponent, {\r
195               duration: 1500,\r
196               data: {\r
197                 message: 'Test Instance Executed'\r
198               }\r
199             });\r
200           },\r
201           (error) => {\r
202             this.modal.open(AlertModalComponent, {\r
203               width: '450px',\r
204               data: {\r
205                 type: 'Alert',\r
206                 message: 'Failed to execute Test Instance!\n' + JSON.stringify(error)\r
207               }\r
208             });\r
209           })\r
210         }\r
211       });\r
212     }\r
213     \r
214   }\r
215 \r
216 }\r