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