b6117326b9a7309f0bf65f32129b01ba944837f8
[portal/ric-dashboard.git] / webapp-frontend / src / app / services / stats / stats.service.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 import { Injectable } from '@angular/core';
21 import { HttpClient, HttpParams } from '@angular/common/http';
22 import { HttpHeaders } from '@angular/common/http';
23 import { Observable } from 'rxjs';
24 import { HttpErrorResponse } from '@angular/common/http';
25
26 @Injectable({
27     providedIn: 'root'
28 })
29
30 export class StatsService {
31     baseJSONServerUrl = 'http://localhost:3000';
32     dataMetrics = [{}];
33     latencyMetrics;
34     load: Observable<number>;
35     cpuMetrics;
36     hostURL = 'http://localhost:10080';
37     jsonURL = 'http://localhost:3000';
38     metricsPath = '/a1ric/metrics';
39     delayPath = '/a1ric/delay';
40     loadPath = '/a1ric/load';
41     delayMax = '15';
42     loadMax = '100000';
43     httpOptions = {
44             headers: new HttpHeaders({
45               'Content-Type':  'application/json',
46               'Access-Control-Allow-Origin': '*',
47               'Access-Control-Allow-Methods': '*',
48               'Access-Control-Allow-Credentials': 'true',
49               'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
50             })
51           };
52
53     private basePath = 'api/admin/';
54
55     constructor(private httpClient: HttpClient) {
56         // this.loadConfig();
57         // this.getLoad();
58     }
59
60     getMetrics() {
61
62         return this.dataMetrics; // @TODO implement the service to fetch the backend data
63     }
64
65     getLatencyMetrics() {
66         this.latencyMetrics = this.getRandomValue();
67         return this.latencyMetrics;
68     }
69
70     getLoad(): Observable<number> {
71         // this.loadMetrics = this.getRandomValue();
72         this.httpClient.get(this.hostURL + this.loadPath).subscribe((res) => {
73             console.log(res);
74             console.log('stats.service.getLoad(): ' + res['load']);
75             this.load = res['load'];
76             return this.load;
77         });
78         return this.load;
79     }
80
81     putLoad(value: number) {
82         // this.loadMetrics = this.getRandomValue();
83         const jsonValue = '{ "load": ' + value + ' }';
84         console.log(jsonValue);
85         this.httpClient.put(this.hostURL + this.loadPath, jsonValue , this.httpOptions).subscribe((res) => {
86             console.log(res);
87         });
88     }
89
90     putDelay(value: number) {
91         // this.loadMetrics = this.getRandomValue();
92         const jsonValue = '{ "delay": ' + value + ' }';
93         console.log(jsonValue);
94         this.httpClient.put(this.hostURL + this.delayPath, jsonValue , this.httpOptions).subscribe((res) => {
95             console.log(res);
96         });
97     }
98
99     getCpuMetrics() {
100         this.cpuMetrics = this.getRandomValue();
101         return this.cpuMetrics;
102     }
103
104     getRandomValue() {
105         return Math.round((Math.random() * (20 - 0)) + 0);
106     }
107
108     // Gets xApp metrics kibana url for the named application
109     getAppMetricsUrl(appName: string)  {
110         return this.httpClient.get(this.basePath + 'metrics', {
111             params: new HttpParams()
112                 .set('app', appName)
113         });
114     }
115
116     saveConfig(key: string, value: string) {
117         if (key === 'jsonURL') {
118             this.baseJSONServerUrl = value;
119         }
120         console.log('save this.baseJSONServerUrl ' + this.baseJSONServerUrl);
121         const jsonValue = '{"id": "' + key + '", "value": "' + value + '"}';
122         console.log(jsonValue);
123         this.httpClient.put(this.baseJSONServerUrl + '/config/' + key , jsonValue, this.httpOptions).subscribe((res) => {
124             console.log(res);
125         });
126
127
128     }
129
130     loadConfig() {
131         console.log('load this.baseJSONServerUrl ' + this.baseJSONServerUrl);
132         const httpOptions = {
133                 headers: new HttpHeaders({
134                   'Content-Type':  'application/json'
135                 })
136               };
137         this.httpClient.get(this.baseJSONServerUrl + '/config/', httpOptions).subscribe((res) => {
138             console.log(res);
139             this.jsonURL = res[0].value;
140             this.hostURL = res[1].value;
141             this.metricsPath = res[2].value;
142             this.delayPath = res[3].value;
143             this.loadPath = res[4].value;
144             this.delayMax = res[5].value;
145             this.loadMax = res[6].value;
146         },
147         (her: HttpErrorResponse) => {
148             console.log ('loadConfig failed: ' + her.message);
149           });
150     }
151 }
152
153 interface Delay {
154     delay: number;
155 }