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, HttpResponse } from '@angular/common/http';
21 import { Injectable } from '@angular/core';
22 import { Observable } from 'rxjs';
23 import { DashboardService } from '../dashboard/dashboard.service';
24 import { StatsDetails, AppStats } from '../../interfaces/e2-mgr.types';
30 export class StatsService {
32 private component = 'admin';
33 private appmetricPath = 'appmetric';
34 private appId = 'appid';
36 baseJSONServerUrl = 'http://localhost:3000';
39 load: Observable<number>;
41 hostURL = 'http://localhost:10080';
42 jsonURL = 'http://localhost:3000';
43 metricsPath = '/a1ric/metrics';
44 delayPath = '/a1ric/delay';
45 loadPath = '/a1ric/load';
49 headers: new HttpHeaders({
50 'Content-Type': 'application/json',
51 'Access-Control-Allow-Origin': '*',
52 'Access-Control-Allow-Methods': '*',
53 'Access-Control-Allow-Credentials': 'true',
54 'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
59 private dashboardSvc: DashboardService,
60 private httpClient: HttpClient) {
66 return this.dataMetrics; // @TODO implement the service to fetch the backend data
70 this.latencyMetrics = this.getRandomValue();
71 return this.latencyMetrics;
74 getLoad(): Observable<number> {
75 // this.loadMetrics = this.getRandomValue();
76 this.httpClient.get(this.hostURL + this.loadPath).subscribe((res) => {
78 console.log('stats.service.getLoad(): ' + res['load']);
79 this.load = res['load'];
86 return Math.round((Math.random() * (20 - 0)) + 0);
89 // Gets xApp metrics Kibana url for the named application
90 getAppMetricsUrl(appName: string) {
91 const path = this.dashboardSvc.buildPath(this.component, null, 'metrics');
92 return this.httpClient.get(path, {
93 params: new HttpParams()
98 getAppMetrics(instanceKey: string): Observable<Array<StatsDetails>> {
99 const path = this.dashboardSvc.buildPath(this.component, instanceKey, this.appmetricPath);
100 return this.httpClient.get<Array<StatsDetails>>(path);
103 getAppMetricsById(instanceKey: string, appId: number): Observable<AppStats> {
104 const path = this.dashboardSvc.buildPath(this.component, instanceKey, this.appmetricPath, this.appId, appId);
105 return this.httpClient.get<AppStats>(path);
108 setupAppMetrics(instanceKey: string, req: StatsDetails): Observable<HttpResponse<Object>> {
109 const path = this.dashboardSvc.buildPath(this.component, instanceKey, this.appmetricPath);
110 return this.httpClient.post(path, req, { observe: 'response' });
113 editAppMetrics(instanceKey: string, req: StatsDetails): Observable<HttpResponse<Object>> {
114 const path = this.dashboardSvc.buildPath(this.component, instanceKey, this.appmetricPath);
115 return this.httpClient.put(path, req, { observe: 'response' });
118 deleteAppMetrics(instanceKey: string, appId: number): Observable<HttpResponse<Object>> {
119 const path = this.dashboardSvc.buildPath(this.component, instanceKey, this.appmetricPath, this.appId, appId);
120 return this.httpClient.delete(path, { observe: 'response' });
123 saveConfig(key: string, value: string) {
124 if (key === 'jsonURL') {
125 this.baseJSONServerUrl = value;
127 console.log('save this.baseJSONServerUrl ' + this.baseJSONServerUrl);
128 const jsonValue = '{"id": "' + key + '", "value": "' + value + '"}';
129 console.log(jsonValue);
130 this.httpClient.put(this.baseJSONServerUrl + '/config/' + key , jsonValue, this.httpOptions).subscribe((res) => {
136 console.log('load this.baseJSONServerUrl ' + this.baseJSONServerUrl);
137 const httpOptions = {
138 headers: new HttpHeaders({
139 'Content-Type': 'application/json'
142 this.httpClient.get(this.baseJSONServerUrl + '/config/', httpOptions).subscribe((res) => {
144 this.jsonURL = res[0].value;
145 this.hostURL = res[1].value;
146 this.metricsPath = res[2].value;
147 this.delayPath = res[3].value;
148 this.loadPath = res[4].value;
149 this.delayMax = res[5].value;
150 this.loadMax = res[6].value;
152 (her: HttpErrorResponse) => {
153 console.log ('loadConfig failed: ' + her.message);