2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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 { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
21 import { Injectable } from '@angular/core';
22 import { Observable } from 'rxjs';
23 import { DashboardService } from '../dashboard/dashboard.service';
29 export class StatsService {
31 private component = 'admin';
33 baseJSONServerUrl = 'http://localhost:3000';
36 load: Observable<number>;
38 hostURL = 'http://localhost:10080';
39 jsonURL = 'http://localhost:3000';
40 metricsPath = '/a1ric/metrics';
41 delayPath = '/a1ric/delay';
42 loadPath = '/a1ric/load';
46 headers: new HttpHeaders({
47 'Content-Type': 'application/json',
48 'Access-Control-Allow-Origin': '*',
49 'Access-Control-Allow-Methods': '*',
50 'Access-Control-Allow-Credentials': 'true',
51 'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
56 private dashboardSvc: DashboardService,
57 private httpClient: HttpClient) {
63 return this.dataMetrics; // @TODO implement the service to fetch the backend data
67 this.latencyMetrics = this.getRandomValue();
68 return this.latencyMetrics;
71 getLoad(): Observable<number> {
72 // this.loadMetrics = this.getRandomValue();
73 this.httpClient.get(this.hostURL + this.loadPath).subscribe((res) => {
75 console.log('stats.service.getLoad(): ' + res['load']);
76 this.load = res['load'];
83 return Math.round((Math.random() * (20 - 0)) + 0);
86 // Gets xApp metrics Kibana url for the named application
87 getAppMetricsUrl(appName: string) {
88 const path = this.dashboardSvc.buildPath(this.component, null, 'metrics');
89 return this.httpClient.get(path, {
90 params: new HttpParams()
95 saveConfig(key: string, value: string) {
96 if (key === 'jsonURL') {
97 this.baseJSONServerUrl = value;
99 console.log('save this.baseJSONServerUrl ' + this.baseJSONServerUrl);
100 const jsonValue = '{"id": "' + key + '", "value": "' + value + '"}';
101 console.log(jsonValue);
102 this.httpClient.put(this.baseJSONServerUrl + '/config/' + key , jsonValue, this.httpOptions).subscribe((res) => {
108 console.log('load this.baseJSONServerUrl ' + this.baseJSONServerUrl);
109 const httpOptions = {
110 headers: new HttpHeaders({
111 'Content-Type': 'application/json'
114 this.httpClient.get(this.baseJSONServerUrl + '/config/', httpOptions).subscribe((res) => {
116 this.jsonURL = res[0].value;
117 this.hostURL = res[1].value;
118 this.metricsPath = res[2].value;
119 this.delayPath = res[3].value;
120 this.loadPath = res[4].value;
121 this.delayMax = res[5].value;
122 this.loadMax = res[6].value;
124 (her: HttpErrorResponse) => {
125 console.log ('loadConfig failed: ' + her.message);