App metrics visualization manage
[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, 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';
25
26 @Injectable({
27     providedIn: 'root'
28 })
29
30 export class StatsService {
31
32     private component = 'admin';
33     private appmetricPath = 'appmetric';
34     private appId = 'appid';
35
36     baseJSONServerUrl = 'http://localhost:3000';
37     dataMetrics = [{}];
38     latencyMetrics;
39     load: Observable<number>;
40     cpuMetrics;
41     hostURL = 'http://localhost:10080';
42     jsonURL = 'http://localhost:3000';
43     metricsPath = '/a1ric/metrics';
44     delayPath = '/a1ric/delay';
45     loadPath = '/a1ric/load';
46     delayMax = '15';
47     loadMax = '100000';
48     httpOptions = {
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'
55             })
56           };
57
58     constructor(
59         private dashboardSvc: DashboardService,
60         private httpClient: HttpClient) {
61         // this.loadConfig();
62         // this.getLoad();
63     }
64
65     getMetrics() {
66         return this.dataMetrics; // @TODO implement the service to fetch the backend data
67     }
68
69     getLatencyMetrics() {
70         this.latencyMetrics = this.getRandomValue();
71         return this.latencyMetrics;
72     }
73
74     getLoad(): Observable<number> {
75         // this.loadMetrics = this.getRandomValue();
76         this.httpClient.get(this.hostURL + this.loadPath).subscribe((res) => {
77             console.log(res);
78             console.log('stats.service.getLoad(): ' + res['load']);
79             this.load = res['load'];
80             return this.load;
81         });
82         return this.load;
83     }
84
85     getRandomValue() {
86         return Math.round((Math.random() * (20 - 0)) + 0);
87     }
88
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()
94                 .set('app', appName)
95         });
96     }
97
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);
101       }
102
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);
106       }
107
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' });
111       }
112
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' });
116     }
117
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' });
121     }
122
123     saveConfig(key: string, value: string) {
124         if (key === 'jsonURL') {
125             this.baseJSONServerUrl = value;
126         }
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) => {
131             console.log(res);
132         });
133     }
134
135     loadConfig() {
136         console.log('load this.baseJSONServerUrl ' + this.baseJSONServerUrl);
137         const httpOptions = {
138                 headers: new HttpHeaders({
139                   'Content-Type':  'application/json'
140                 })
141               };
142         this.httpClient.get(this.baseJSONServerUrl + '/config/', httpOptions).subscribe((res) => {
143             console.log(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;
151         },
152         (her: HttpErrorResponse) => {
153             console.log ('loadConfig failed: ' + her.message);
154           });
155     }
156 }
157
158 interface Delay {
159     delay: number;
160 }