App metrics visualization manage
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / stats / stats-dialog.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 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 { HttpErrorResponse, HttpResponse } from '@angular/common/http';
21 import { Component, Inject, OnInit } from '@angular/core';
22 import { FormControl, FormGroup } from '@angular/forms';
23 import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
24 import { Observable } from 'rxjs';
25 import { finalize } from 'rxjs/operators';
26 import { StatsDialogFormData, StatsDetails } from '../interfaces/e2-mgr.types';
27 import { ErrorDialogService } from '../services/ui/error-dialog.service';
28 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
29 import { NotificationService } from '../services/ui/notification.service';
30 import { StatsService } from '../services/stats/stats.service';
31
32 @Component({
33   selector: 'stats-dialog',
34   templateUrl: './stats-dialog.component.html',
35   styleUrls: ['./stats-dialog.component.scss']
36 })
37
38 export class StatsDialogComponent implements OnInit {
39
40   public statsDialogForm: FormGroup;
41   public processing = false;
42
43   constructor(
44     private dialogRef: MatDialogRef<StatsDialogComponent>,
45     private service: StatsService,
46     private errorService: ErrorDialogService,
47     private loadingDialogService: LoadingDialogService,
48     private notifService: NotificationService,
49     @Inject(MAT_DIALOG_DATA) private data) {
50     // opens with empty fields; accepts no data to display
51   }
52
53   ngOnInit() {
54     this.statsDialogForm = new FormGroup({
55       appName: this.data? new FormControl(this.data.appName) : new FormControl(''),
56       metricUrl: this.data? new FormControl(this.data.metricUrl) : new FormControl('')
57     });
58   }
59
60   setupStats = (statsFormValue: StatsDialogFormData) => {
61     if (!this.statsDialogForm.valid) {
62       console.log('Invalid dialog form data, appname and/or metrics url failed to pass its validation checks');
63       return;
64     }
65     this.processing = true;
66     const setupRequest: StatsDetails = {
67       appId: this.data.appId,
68       appName: statsFormValue.appName.trim(),
69       metricUrl: statsFormValue.metricUrl.trim()
70     };
71     this.loadingDialogService.startLoading('Setting up app metrics list');
72     let observable: Observable<HttpResponse<Object>>;
73     if(!(this.data.isEdit==='true'))
74       observable = this.service.setupAppMetrics(this.data.instanceKey, setupRequest);
75     else
76       observable = this.service.editAppMetrics(this.data.instanceKey, setupRequest);
77     observable
78       .pipe(
79         finalize(() => this.loadingDialogService.stopLoading())
80       )
81       .subscribe(
82         (response: any) => {
83           this.processing = false;
84           this.notifService.success('App metrics setup!');
85           this.dialogRef.close(true);
86         },
87         ((her: HttpErrorResponse) => {
88           this.processing = false;
89           // the error field carries the server's response
90           let msg = her.message;
91           if (her.error && her.error.message) {
92             msg = her.error.message;
93           }
94           this.errorService.displayError('App Metrics setup request failed: ' + msg);
95           // keep the dialog open
96         })
97       );
98   }
99
100   hasError(controlName: string, errorName: string) {
101     if (this.statsDialogForm.controls[controlName].hasError(errorName)) {
102       return true;
103     }
104     return false;
105   }
106
107   validateControl(controlName: string) {
108     if (this.statsDialogForm.controls[controlName].invalid && this.statsDialogForm.controls[controlName].touched) {
109       return true;
110     }
111     return false;
112   }
113
114 }