Add multi-layer RIC instance selector
[portal/ric-dashboard.git] / dashboard / 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
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 { 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';
24
25 @Injectable({
26     providedIn: 'root'
27 })
28
29 export class StatsService {
30
31     private component = 'admin';
32
33     baseJSONServerUrl = 'http://localhost:3000';
34     dataMetrics = [{}];
35     latencyMetrics;
36     load: Observable<number>;
37     cpuMetrics;
38     hostURL = 'http://localhost:10080';
39     jsonURL = 'http://localhost:3000';
40     metricsPath = '/a1ric/metrics';
41     delayPath = '/a1ric/delay';
42     loadPath = '/a1ric/load';
43     delayMax = '15';
44     loadMax = '100000';
45     httpOptions = {
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'
52             })
53           };
54
55     constructor(
56         private dashboardSvc: DashboardService,
57         private httpClient: HttpClient) {
58         // this.loadConfig();
59         // this.getLoad();
60     }
61
62     getMetrics() {
63         return this.dataMetrics; // @TODO implement the service to fetch the backend data
64     }
65
66     getLatencyMetrics() {
67         this.latencyMetrics = this.getRandomValue();
68         return this.latencyMetrics;
69     }
70
71     getLoad(): Observable<number> {
72         // this.loadMetrics = this.getRandomValue();
73         this.httpClient.get(this.hostURL + this.loadPath).subscribe((res) => {
74             console.log(res);
75             console.log('stats.service.getLoad(): ' + res['load']);
76             this.load = res['load'];
77             return this.load;
78         });
79         return this.load;
80     }
81
82     getRandomValue() {
83         return Math.round((Math.random() * (20 - 0)) + 0);
84     }
85
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()
91                 .set('app', appName)
92         });
93     }
94
95     saveConfig(key: string, value: string) {
96         if (key === 'jsonURL') {
97             this.baseJSONServerUrl = value;
98         }
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) => {
103             console.log(res);
104         });
105     }
106
107     loadConfig() {
108         console.log('load this.baseJSONServerUrl ' + this.baseJSONServerUrl);
109         const httpOptions = {
110                 headers: new HttpHeaders({
111                   'Content-Type':  'application/json'
112                 })
113               };
114         this.httpClient.get(this.baseJSONServerUrl + '/config/', httpOptions).subscribe((res) => {
115             console.log(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;
123         },
124         (her: HttpErrorResponse) => {
125             console.log ('loadConfig failed: ' + her.message);
126           });
127     }
128 }
129
130 interface Delay {
131     delay: number;
132 }