2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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';
33 selector: 'stats-dialog',
34 templateUrl: './stats-dialog.component.html',
35 styleUrls: ['./stats-dialog.component.scss']
38 export class StatsDialogComponent implements OnInit {
40 public statsDialogForm: FormGroup;
41 public processing = false;
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
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('')
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');
65 this.processing = true;
66 const setupRequest: StatsDetails = {
67 appId: this.data.appId,
68 appName: statsFormValue.appName.trim(),
69 metricUrl: statsFormValue.metricUrl.trim()
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);
76 observable = this.service.editAppMetrics(this.data.instanceKey, setupRequest);
79 finalize(() => this.loadingDialogService.stopLoading())
83 this.processing = false;
84 this.notifService.success('App metrics setup!');
85 this.dialogRef.close(true);
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;
94 this.errorService.displayError('App Metrics setup request failed: ' + msg);
95 // keep the dialog open
100 hasError(controlName: string, errorName: string) {
101 if (this.statsDialogForm.controls[controlName].hasError(errorName)) {
107 validateControl(controlName: string) {
108 if (this.statsDialogForm.controls[controlName].invalid && this.statsDialogForm.controls[controlName].touched) {