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
diff --git a/otf-frontend/client/src/app/layout/test-definition-expanded-details/test-definition-expanded-details.component.ts b/otf-frontend/client/src/app/layout/test-definition-expanded-details/test-definition-expanded-details.component.ts
new file mode 100644 (file)
index 0000000..4e2891d
--- /dev/null
@@ -0,0 +1,216 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+import { Component, OnInit, Input, ViewChild, HostListener, AfterContentInit, AfterViewInit, ElementRef, OnDestroy } from '@angular/core';\r
+import { TestDefinitionService } from 'app/shared/services/test-definition.service';\r
+import { TestInstanceService } from 'app/shared/services/test-instance.service';\r
+import { MatTableDataSource, MatPaginator, MatDialog, MatSnackBar } from '@angular/material';\r
+import Modeler from 'bpmn-js';\r
+import { timeInterval } from 'rxjs/operators';\r
+import { Observable } from 'rxjs';\r
+import { TestExecutionService } from 'app/shared/services/test-execution.service';\r
+import { AlertModalComponent } from 'app/shared/modules/alert-modal/alert-modal.component';\r
+import { AlertSnackbarComponent } from 'app/shared/modules/alert-snackbar/alert-snackbar.component';\r
+import { SchedulingService } from 'app/shared/services/scheduling.service';\r
+import { FileTransferService } from 'app/shared/services/file-transfer.service';\r
+import { Buffer } from 'buffer';\r
+import { ViewWorkflowModalComponent } from 'app/shared/modules/view-workflow-modal/view-workflow-modal.component';\r
+import { ExecuteService } from 'app/shared/services/execute.service';\r
+import { BpmnFactoryService } from 'app/shared/factories/bpmn-factory.service';\r
+\r
+@Component({\r
+  selector: 'app-test-definition-expanded-details',\r
+  templateUrl: './test-definition-expanded-details.component.pug',\r
+  styleUrls: ['./test-definition-expanded-details.component.scss']\r
+})\r
+export class TestDefinitionExpandedDetailsComponent implements OnInit, OnDestroy {\r
+\r
+  @Input() public testDefinitionId;\r
+\r
+  @Input() public events: Observable<void>;\r
+  \r
+  @ViewChild(MatPaginator) instancePaginator: MatPaginator;\r
+  @ViewChild('canvas') canvas: ElementRef;\r
+\r
+  public data = null;\r
+  public dataLength = 0;\r
+  public displayedColumns;\r
+  public foundStatuses = ['name'];\r
+  public statusList = ['COMPLETED', 'SUCCESS', 'UNKNOWN', 'FAILURE', 'STOPPED', 'UNAUTHORIZED', 'FAILED'];\r
+  public testInstanceList = null;\r
+  public testExecutionList = [];\r
+  public viewer;\r
+  public eventSub;\r
+  public bpmnXml;\r
+\r
+  constructor(\r
+    private bpmnFactory: BpmnFactoryService, \r
+    private fileTransfer: FileTransferService, \r
+    private dialog: MatDialog, \r
+    private testDefinition: TestDefinitionService, \r
+    private testInstance: TestInstanceService, \r
+    private testExecution: TestExecutionService, \r
+    private execute: ExecuteService, \r
+    private modal: MatDialog, \r
+    private snack: MatSnackBar\r
+  ) { }\r
+\r
+  async ngOnInit() {\r
+    \r
+    await this.testDefinition.get(this.testDefinitionId).subscribe(\r
+      result => {\r
+        result['createdAt'] = new Date(result['createdAt']).toLocaleString();\r
+        result['updatedAt'] = new Date(result['updatedAt']).toLocaleString();\r
+        this.data = result;\r
+        if(this.data.bpmnInstances){\r
+          this.bpmnFactory.setup({\r
+            mode: 'viewer',\r
+            options: {\r
+              container: this.canvas.nativeElement\r
+            },\r
+            fileId: this.data.bpmnInstances[0].bpmnFileId\r
+          }).then(res => {\r
+            this.viewer = res;\r
+          });\r
+          // this.loadDiagram();\r
+        }\r
+      }\r
+    );\r
+\r
+    this.testInstanceList = new MatTableDataSource();\r
+    this.testInstance.find({\r
+      $limit: -1,\r
+      $sort: {\r
+        createdAt: -1\r
+      },\r
+      testDefinitionId: this.testDefinitionId\r
+    }).subscribe(\r
+      result => {\r
+        this.testInstanceList.data = result;\r
+        this.testInstanceList.paginator = this.instancePaginator;\r
+\r
+        this.testInstanceList.data.forEach(elem => {\r
+          this.setExecutions(elem._id);\r
+        });\r
+\r
+        this.displayedColumns = ['name', 'COMPLETED', 'SUCCESS', 'UNKNOWN', 'FAILURE', 'STOPPED', 'UNAUTHORIZED', 'FAILED', 'options'];\r
+\r
+      }\r
+    )\r
+\r
+    //If parent emeits, diagram will reload\r
+    if(this.events != undefined && this.events){\r
+      this.events.subscribe(() => {\r
+        setTimeout(() => {\r
+          this.loadDiagram();\r
+        }, 500)\r
+      });\r
+    }\r
+  }\r
+\r
+  enlargeBpmn(){\r
+    this.dialog.open(ViewWorkflowModalComponent, {\r
+      data: {\r
+        xml: this.viewer.getBpmnXml()\r
+      },\r
+      width: '100%',\r
+      height: '100%'\r
+    })\r
+  }\r
+\r
+  ngOnDestroy() {\r
+    delete this.events;\r
+  }\r
+\r
+  async setExecutions(instanceId) {\r
+    // ['$limit=-1', '$sort[startTime]=-1', 'testInstanceId=' + instanceId]\r
+    this.testExecution.find({\r
+      $limit: -1,\r
+      $sort: {\r
+        startTime: -1\r
+      },\r
+      'historicTestInstance._id': instanceId\r
+    }).subscribe(\r
+      result => {\r
+        for(let i = 0; i < result['length']; i++){\r
+          result[i].startTime = new Date(result[i].startTime).toLocaleString();\r
+          this.testExecutionList.push(result[i]);\r
+          for(let j = 0; j < this.testInstanceList.data.length; j++){\r
+            if(this.testInstanceList.data[j]._id == instanceId){\r
+              if(!this.testInstanceList.data[j][result[i]['testResult']]){\r
+                this.testInstanceList.data[j][result[i]['testResult']] = 1;\r
+              }else{\r
+                this.testInstanceList.data[j][result[i]['testResult']] += 1;\r
+              }\r
+            }\r
+          }\r
+        }\r
+\r
+        //this.setDisplayColumns();\r
+        // for(let i = 0; i < result[i]; i++){\r
+        //   this.testInstanceList[instanceId] = result;\r
+        // }\r
+        \r
+      }\r
+    );\r
+  }\r
+\r
+  loadDiagram(){\r
+    if(this.viewer && this.data && this.data.bpmnInstances){\r
+      this.viewer.renderDiagram();\r
+    }\r
+  }\r
+\r
+  executeTestInstance(element){\r
+    (element);\r
+    if(element.testDefinitionId){\r
+      const executer = this.modal.open(AlertModalComponent, {\r
+        width: '250px',\r
+        data: {\r
+          type: 'confirmation',\r
+          message: 'Are you sure you want to run ' + element.testInstanceName + '?'\r
+        }\r
+      });\r
+\r
+      executer.afterClosed().subscribe(result => {\r
+        if(result){\r
+          this.execute.create({\r
+            _id : element._id,\r
+            async: true\r
+          }).subscribe((result) => {\r
+            this.snack.openFromComponent(AlertSnackbarComponent, {\r
+              duration: 1500,\r
+              data: {\r
+                message: 'Test Instance Executed'\r
+              }\r
+            });\r
+          },\r
+          (error) => {\r
+            this.modal.open(AlertModalComponent, {\r
+              width: '450px',\r
+              data: {\r
+                type: 'Alert',\r
+                message: 'Failed to execute Test Instance!\n' + JSON.stringify(error)\r
+              }\r
+            });\r
+          })\r
+        }\r
+      });\r
+    }\r
+    \r
+  }\r
+\r
+}\r