added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / components / stats / test-definition-executions-bar-chart / test-definition-executions-bar-chart.component.ts
diff --git a/otf-frontend/client/src/app/layout/components/stats/test-definition-executions-bar-chart/test-definition-executions-bar-chart.component.ts b/otf-frontend/client/src/app/layout/components/stats/test-definition-executions-bar-chart/test-definition-executions-bar-chart.component.ts
new file mode 100644 (file)
index 0000000..866de65
--- /dev/null
@@ -0,0 +1,198 @@
+/*  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, ViewChild, ElementRef, Input, OnDestroy } from '@angular/core';\r
+import { StatsService } from '../stats.service';\r
+import * as am4core from "@amcharts/amcharts4/core";\r
+import * as am4charts from "@amcharts/amcharts4/charts";\r
+import { e } from '@angular/core/src/render3';\r
+import * as moment from 'moment';\r
+import { Router } from '@angular/router';\r
+import { Subscription } from 'rxjs';\r
+\r
+@Component({\r
+  selector: 'app-test-definition-executions-bar-chart',\r
+  templateUrl: './test-definition-executions-bar-chart.component.pug',\r
+  styleUrls: ['./test-definition-executions-bar-chart.component.scss']\r
+})\r
+export class TestDefinitionExecutionsBarChartComponent implements OnInit, OnDestroy {\r
+\r
+  private toDestroy: Array<Subscription> = [];\r
+\r
+  @ViewChild('chart') chartElement: ElementRef;\r
+  @Input() height: string;\r
+\r
+  public chart: am4charts.XYChart;\r
+  public testInstanceData;\r
+  public loadingIndicator;\r
+\r
+  constructor(private stats: StatsService, private router: Router) {}\r
+\r
+\r
+  ngOnInit() {\r
+    this.renderChart();\r
+\r
+    this.toDestroy.push(this.stats.onDefaultDataCallStarted().subscribe(res => {\r
+      this.showLoadingIndicator();\r
+    }));\r
+\r
+    this.toDestroy.push(this.stats.onTIExecutionChangeStarted().subscribe(res => {\r
+      this.showLoadingIndicator();\r
+    }));\r
+\r
+    this.toDestroy.push(this.stats.onDefaultDataCallFinished().subscribe(res => {\r
+      this.setChartData();\r
+    }));\r
+\r
+    this.toDestroy.push(this.stats.onTIExecutionChangeFinished().subscribe(res => {\r
+      this.setChartData()\r
+    }));\r
+\r
+  }\r
+\r
+  ngOnDestroy() {\r
+    this.toDestroy.forEach(e => e.unsubscribe());\r
+    this.chart.dispose();\r
+  }\r
+\r
+  showLoadingIndicator() {\r
+    if(!this.loadingIndicator){\r
+      this.loadingIndicator = this.chart.tooltipContainer.createChild(am4core.Container);\r
+      this.loadingIndicator.background.fill = am4core.color("#fff");\r
+      this.loadingIndicator.background.fillOpacity = 0.8;\r
+      this.loadingIndicator.width = am4core.percent(100);\r
+      this.loadingIndicator.height = am4core.percent(100);\r
+\r
+      let indicatorLabel = this.loadingIndicator.createChild(am4core.Label);\r
+      indicatorLabel.text = "Loading..";\r
+      indicatorLabel.align = "center";\r
+      indicatorLabel.valign = "middle";\r
+      indicatorLabel.fontSize = 18;\r
+      indicatorLabel.fontWeight= "bold";\r
+      indicatorLabel.dy = 50;\r
+\r
+      let loadingImage = this.loadingIndicator.createChild(am4core.Image);\r
+      //loadingImage.href = "https://img.devrant.com/devrant/rant/r_647810_4FeCH.gif";\r
+      loadingImage.href = "/assets/images/equalizer.gif";\r
+      //loadingImage.dataSource = "/loading-pies.svg"\r
+      loadingImage.align = "center";\r
+      loadingImage.valign = "middle";\r
+      loadingImage.horizontalCenter = "middle";\r
+      loadingImage.verticalCenter = "middle";\r
+      loadingImage.scale = 3.0;\r
+    }else{\r
+      this.loadingIndicator.show();\r
+    }\r
+\r
+  }\r
+\r
+  hideLoadingIndicator() {\r
+    this.loadingIndicator.hide();\r
+  }\r
+\r
+  setChartData() {\r
+\r
+    let data = [];\r
+    this.stats.executionList.forEach((execution, i) => {\r
+      let index = data.findIndex(e => e.id === execution.historicTestDefinition._id);\r
+      let executionTime = moment(execution.endTime).diff(moment(execution.startTime), 'seconds');\r
+      if(index == -1){\r
+        data.push({\r
+          id: execution.historicTestDefinition._id,\r
+          name: execution.historicTestDefinition.testName,\r
+          testResult: execution.testResult,\r
+          executionTime: executionTime,\r
+          count: 1,\r
+          average: executionTime\r
+        })\r
+      }else{\r
+        data[index].count += 1;\r
+        data[index].executionTime += executionTime;\r
+        data[index].average = (data[index].executionTime / data[index].count);\r
+      }\r
+    });\r
+    data.sort((a, b) => b.count - a.count);\r
+    this.chart.data = data;\r
+    \r
+\r
+    // Displays the average time for each bar. \r
+    // If there is no time recorded for the Test Instance, display No Time Recorded.\r
+    let series = this.chart.series.values[0] as am4charts.ColumnSeries;\r
+    \r
+    series.columns.template.adapter.add("tooltipText", (text, target) => {\r
+      if (target.dataItem) {\r
+        if (this.chart.data[target.dataItem.index].average > 0) {\r
+          return this.chart.data[target.dataItem.index].count.toString() + " Executions \n Avg Time: " + this.chart.data[target.dataItem.index].average.toFixed(2).toString() + " seconds";\r
+        } else\r
+          return this.chart.data[target.dataItem.index].count.toString() + " Executions \n No Time Recorded";\r
+      }\r
+    });\r
+    series.columns.template.adapter.add("fill", (fill, target) => this.chart.colors.getIndex(target.dataItem.index));\r
+    \r
+    \r
+    series.columns.template.events.on("doublehit", (click) => {\r
+      this.router.navigate(['/test-definitions', click.target.dataItem.dataContext['id']]);\r
+    });\r
+    this.chart.appear();\r
+    this.hideLoadingIndicator();\r
+    \r
+  }\r
+\r
+  renderChart() {\r
+    this.chart = am4core.create(this.chartElement.nativeElement, am4charts.XYChart);\r
+    this.chart.cursor = new am4charts.XYCursor();\r
+    this.showLoadingIndicator();\r
+\r
+    this.chart.responsive.enabled = true;\r
+\r
+    // Create axes\r
+    var categoryAxis = this.chart.yAxes.push(new am4charts.CategoryAxis());\r
+    categoryAxis.dataFields.category = "name";\r
+    categoryAxis.numberFormatter.numberFormat = "#";\r
+    categoryAxis.renderer.inversed = true;\r
+    categoryAxis.renderer.minGridDistance = 5;\r
+    categoryAxis.title.fontSize = "10px";\r
+\r
+    var valueAxis = this.chart.xAxes.push(new am4charts.ValueAxis());\r
+    valueAxis.renderer.minWidth = 10;\r
+\r
+    // Create series\r
+    var series = this.chart.series.push(new am4charts.ColumnSeries());\r
+    series.dataFields.valueX = "count";\r
+    series.dataFields.categoryY = "name";\r
+    series.columns.template.tooltipText = " ";\r
+\r
+    let label = categoryAxis.renderer.labels.template;\r
+    label.truncate = true;\r
+    label.maxWidth = 130;\r
+    label.fontSize = 13;\r
+\r
+    //Scrollbar on the right. \r
+    let scrollBarY = new am4charts.XYChartScrollbar();\r
+    //scrollBarY.series.push(series);\r
+    this.chart.scrollbarY = scrollBarY;\r
+    this.chart.scrollbarY.contentHeight = 100;\r
+    this.chart.scrollbarY.minWidth = 20;\r
+    this.chart.scrollbarY.thumb.minWidth = 20;\r
+\r
+    //set initial Scrollbar Zoom to the Top 6 Instances. \r
+    this.chart.events.on("appeared", () => {\r
+      \r
+      categoryAxis.zoomToIndexes(0, 6, false, true);\r
+    });\r
+   }\r
+\r
+}\r