2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 import { Component, OnInit, ViewChildren, QueryList } from '@angular/core';
21 import { BaseChartDirective } from 'ng2-charts/ng2-charts';
22 import { StatsService } from '../services/stats/stats.service';
23 import { MatSlideToggleChange } from "@angular/material/slide-toggle";
24 import { HttpClient } from '@angular/common/http';
25 import { HttpHeaders } from "@angular/common/http";
26 import { Observable } from "rxjs";
27 import { HttpErrorResponse } from "@angular/common/http";
28 import { map } from 'rxjs/operators';
31 selector: 'app-stats',
32 templateUrl: './stats.component.html',
33 styleUrls: ['./stats.component.scss']
35 export class StatsComponent implements OnInit {
37 @ViewChildren(BaseChartDirective) charts: QueryList<BaseChartDirective>;
44 public latencyChartColors: Array<any> = [
46 backgroundColor: 'rgba(197, 239, 247, 0.2)',
47 borderColor: 'lightblue',
48 pointBackgroundColor: 'lightblue',
49 pointBorderColor: '#fff',
50 pointHoverBackgroundColor: '#fff',
51 pointHoverBorderColor: 'rgba(148,159,177,0.8)'
54 public latencyChartOptions = {
55 scaleShowVerticalLines: true,
62 animationDuration: 1 // duration of animations when hovering an item
64 responsiveAnimationDuration: 500,
68 // the data minimum used for determining the ticks is Math.min(dataMin, suggestedMin)
70 // the data maximum used for determining the ticks is Math.max(dataMax, suggestedMax)
81 labelString: 'time (last 10 seconds)'
86 public latencyChartLabels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
87 public latencyChartType = 'line';
88 public latencyChartLegend = true;
89 public latencyChartData = [
90 { data: [65, 59, 80, 81, 56, 55, 40, 20, 12, 34], label: 'Latency' },
93 public loadChartColors: Array<any> = [
96 backgroundColor: 'rgba(200, 247, 197, 0.2)',
97 borderColor: 'lightgreen',
98 pointBackgroundColor: 'lightgreen',
99 pointBorderColor: '#fff',
100 pointHoverBackgroundColor: '#fff',
101 pointHoverBorderColor: 'rgba(0,200,0,0.5)'
104 public loadChartOptions = {
105 scaleShowVerticalLines: false,
112 animationDuration: 1 // duration of animations when hovering an item
114 responsiveAnimationDuration: 500,
118 // the data minimum used for determining the ticks is Math.min(dataMin, suggestedMin)
120 // the data maximum used for determining the ticks is Math.max(dataMax, suggestedMax)
121 // suggestedMax: 1000
125 labelString: '# of requests'
131 labelString: 'time (last 10 seconds)'
136 public loadChartLabels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
137 public loadChartType = 'line';
138 public loadChartLegend = true;
139 public loadChartData = [
140 { data: [28, 48, 40, 19, 86, 77, 90, 20, 12, 34], label: 'Load' }
143 public cpuChartColors: Array<any> = [
146 backgroundColor: 'rgba(241, 169, 160, 0.2)',
147 borderColor: 'brown',
148 pointBackgroundColor: 'brown',
149 pointBorderColor: '#fff',
150 pointHoverBackgroundColor: '#fff',
151 pointHoverBorderColor: 'rgba(0,200,0,0.5)'
154 public cpuChartOptions = {
155 scaleShowVerticalLines: false,
162 animationDuration: 1 // duration of animations when hovering an item
164 responsiveAnimationDuration: 500,
168 // the data minimum used for determining the ticks is Math.min(dataMin, suggestedMin)
170 // the data maximum used for determining the ticks is Math.max(dataMax, suggestedMax)
171 // suggestedMax: 1000
175 labelString: '# of requests'
181 labelString: 'time (last 10 seconds)'
186 public cpuChartLabels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
187 public cpuChartType = 'line';
188 public cpuChartLegend = true;
189 public cpuChartData = [
190 { data: [15, 29, 30, 31, 53, 52, 41, 70, 32, 14], label: 'RICLoad' }
201 // this.latencyChartData = [{data: [Math.random() * 100, Math.random() * 100, Math.random() * 100,
202 // Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100,
203 // Math.random() * 100, Math.random() * 100], label: 'Latency'}];
204 this.charts.forEach((child) => {
205 if (child.datasets[0].label === 'Latency') {
206 this.latencyChartLabels.shift();
207 child.datasets[0].data.shift();
209 const latencyData = this.service.getLatencyMetrics();
210 child.datasets[0].data.push(latencyData);
211 this.latencyChartLabels.push('' + this.x++);
213 // once new data is computed and datasets are updated, tell our baseChart the datasets changed
216 currentValue: child.datasets,
219 isFirstChange: () => true
228 this.startLoadTimer();
231 this.pauseLoadTimer();
235 loopLoadData(metricsv: any) {
236 this.charts.forEach((child) => {
237 if (child.datasets[0].label === 'Load') {
238 this.loadChartLabels.shift();
239 child.datasets[0].data.shift();
241 //const loadData = this.service.getLoad();
242 //child.datasets[0].data.push(this.service.load);
243 child.datasets[0].data.push(metricsv['load']);
244 this.loadChartLabels.push('' + this.x++);
246 if (child.datasets[0].label === 'Latency') {
247 this.latencyChartLabels.shift();
248 child.datasets[0].data.shift();
250 //const loadData = this.service.getLoad();
251 //child.datasets[0].data.push(this.service.load);
252 child.datasets[0].data.push(metricsv['latency']);
253 this.latencyChartLabels.push('' + this.x++);
255 if (child.datasets[0].label === 'RICLoad') {
256 this.latencyChartLabels.shift();
257 child.datasets[0].data.shift();
259 //const loadData = this.service.getLoad();
260 //child.datasets[0].data.push(this.service.load);
261 child.datasets[0].data.push(metricsv['ricload']);
262 this.latencyChartLabels.push('' + this.x++);
264 // once new data is computed and datasets are updated, tell our baseChart the datasets changed
267 currentValue: child.datasets,
270 isFirstChange: () => true
277 // this.cpuChartData = [{data: [Math.random() * 100, Math.random() * 100, Math.random() * 100,
278 // Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100,
279 // Math.random() * 100, Math.random() * 100], label: 'CPU'}];
280 const cpuData = this.service.getLatencyMetrics();
281 this.newDataPoint([cpuData], this.z++);
284 newDataPoint(dataArr = [100], label) {
286 this.cpuChartData.forEach((dataset, index) => {
287 this.cpuChartData[index] = Object.assign({}, this.cpuChartData[index], {
288 data: [...this.cpuChartData[index].data, dataArr[index]]
292 this.cpuChartLabels = [...this.cpuChartLabels, label];
293 console.log(this.cpuChartLabels);
294 console.log(this.cpuChartData);
297 public sliderLoadMax = Number(this.service.loadMax) || 0;
299 public sliderDelayMax = Number(this.service.delayMax) || 0;
301 formatLabel(value: number | null) {
307 return Math.round(value / 1000);
313 constructor(private service: StatsService, private httpClient: HttpClient) {
314 this.sliderLoadMax = Number(this.service.loadMax) || 0;
316 this.sliderDelayMax = Number(this.service.delayMax) || 0;
317 console.log('this.sliderLoadMax: ' + this.sliderLoadMax);
318 console.log('this.sliderDelayMax: ' + this.sliderDelayMax);
321 this.fetchLoad().subscribe(loadv => {
322 console.log('loadv: ' + loadv);
323 this.checked = loadv;
325 this.fetchDelay().subscribe(delayv => {
326 console.log('delayv: ' + delayv);
329 this.fetchMetrics().subscribe(metricsv => {
330 console.log('metricsv.load: ' + metricsv['load']);
336 this.interval = setInterval(() => {
337 if (this.timeLeft > 0) {
339 this.fetchMetrics().subscribe(metricsv => {
340 console.log('metricsv.load: ' + metricsv['latency']);
341 console.log('metricsv.load: ' + metricsv['load']);
342 console.log('metricsv.load: ' + metricsv['ricload']);
343 this.loopLoadData(metricsv);
353 clearInterval(this.interval);
357 return this.httpClient.get<any[]>(this.service.hostURL + this.service.metricsPath, this.service.httpOptions).pipe(map(res => {
359 console.log(res['load']);
365 return this.httpClient.get<any[]>(this.service.hostURL + this.service.delayPath, this.service.httpOptions).pipe(map(res => {
367 console.log(res['delay']);
368 const delayv = res['delay'];
376 console.log(this.delay);
377 this.service.putDelay(this.delay);
381 return this.httpClient.get<any[]>(this.service.hostURL + this.service.loadPath, this.service.httpOptions).pipe(map(res => {
383 console.log(res['load']);
384 const loadv = res['load'];
393 console.log(this.load);
394 this.service.putLoad(this.load);