Use O-RAN-SC
[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 } 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     constructor(private httpClient: HttpClient) { 
54         //this.loadConfig();
55         //this.getLoad();
56         
57     }
58
59     getMetrics() {
60
61         return this.dataMetrics; // @TODO implement the service to fetch the backend data
62     }
63
64     getLatencyMetrics() {
65         this.latencyMetrics = this.getRandomValue();
66         return this.latencyMetrics;
67     }
68
69     getLoad(): Observable<number> {
70         //this.loadMetrics = this.getRandomValue();
71         this.httpClient.get(this.hostURL + this.loadPath).subscribe((res) => {
72             console.log(res);
73             console.log('stats.service.getLoad(): ' + res['load']);
74             this.load = res['load'];
75             return this.load;
76         });
77         return this.load;
78     }
79     
80     putLoad(value: number) {
81         //this.loadMetrics = this.getRandomValue();
82         const jsonValue = '{ "load": ' + value + ' }';
83         console.log(jsonValue);
84         this.httpClient.put(this.hostURL + this.loadPath, jsonValue , this.httpOptions).subscribe((res) => {
85             console.log(res);
86         });
87     }
88
89     putDelay(value: number) {
90         //this.loadMetrics = this.getRandomValue();
91         const jsonValue = '{ "delay": ' + value + ' }';
92         console.log(jsonValue);
93         this.httpClient.put(this.hostURL + this.delayPath, jsonValue , this.httpOptions).subscribe((res) => {
94             console.log(res);
95         });
96     }
97     
98     getCpuMetrics() {
99         this.cpuMetrics = this.getRandomValue();
100         return this.cpuMetrics;
101     }
102
103     getRandomValue() {
104         return Math.round((Math.random() * (20 - 0)) + 0);
105     }
106     
107     saveConfig(key: string, value: string) {
108         if(key == 'jsonURL')
109             this.baseJSONServerUrl = value;
110
111             console.log('save this.baseJSONServerUrl '+this.baseJSONServerUrl);
112             const jsonValue = '{"id": "' + key + '", "value": "' + value + '"}';
113             console.log(jsonValue);
114             this.httpClient.put(this.baseJSONServerUrl + '/config/' + key , jsonValue, this.httpOptions).subscribe((res) => {
115                 console.log(res);
116             });
117
118         
119     }
120     
121     loadConfig() {
122         console.log('load this.baseJSONServerUrl '+this.baseJSONServerUrl);
123         const httpOptions = {
124                 headers: new HttpHeaders({
125                   'Content-Type':  'application/json'
126                 })
127               };
128         this.httpClient.get(this.baseJSONServerUrl + '/config/', httpOptions).subscribe((res) => {
129             console.log(res);
130             this.jsonURL = res[0].value;
131             this.hostURL = res[1].value;
132             this.metricsPath = res[2].value;
133             this.delayPath = res[3].value;
134             this.loadPath = res[4].value;
135             this.delayMax = res[5].value;
136             this.loadMax = res[6].value;
137         },
138         (err: HttpErrorResponse) => {
139             console.log (err.message);
140           });
141     }
142 }
143
144 interface Delay {
145     delay: number;
146 }