17a6b2ea65e20c9ae4ddecd7c0fd5ce5dd6d6f1e
[dcaegen2/services.git] /
1 /*
2     Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8             http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
17 import { Dashboard } from "../../core/models/dashboard.model";
18 import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
19 import { CreateDashboardComponent } from "./create-dashboard/create-dashboard.component";
20
21 import { AdminService } from "../../core/services/admin.service";
22
23 // DB modal components
24 import { RestApiService } from "src/app/core/services/rest-api.service";
25
26 // Notify
27 import { ToastrNotificationService } from "src/app/core/services/toastr-notification.service";
28 // Loading spinner
29 import { NgxSpinnerService } from "ngx-spinner";
30
31 @Component({
32   selector: 'app-dashboard-list',
33   templateUrl: './dashboard-list.component.html',
34   styleUrls: ['./dashboard-list.component.css']
35 })
36 export class DashboardListComponent implements OnInit {
37   @Output() passEntry: EventEmitter<any> = new EventEmitter();
38   dbList: any = [];
39   dbs: Dashboard[] = [];
40
41   loading: Boolean = true;
42
43   tempDbDetail: Dashboard;
44   dashboardDeteleModelShow = true;
45
46   // nameArr = [];
47
48   constructor(
49     private adminService: AdminService,
50     private dashboardApiService: RestApiService,
51     private notificationService: ToastrNotificationService,
52     private modalService: NgbModal,
53     private spinner: NgxSpinnerService
54   ) {
55     // Set page title
56     this.adminService.setTitle("SIDEBAR.DASHBOARDLIST");
57     this.initList();
58
59   }
60
61   ngOnInit() {
62     this.spinner.show();
63   }
64   initList() {
65     this.initData().then(data => {
66       this.initDbsList(this.dbList).then(data => {
67         this.dbs = data;
68         console.log(this.dbs, "dasboard-dbs[]")
69       });
70     });
71   }
72
73   async initData() {
74     this.dbList = [];
75     this.dbList = await this.getDbList();
76     setTimeout(() => {
77       this.spinner.hide();
78     }, 500);
79   }
80
81   getDbList() {
82     var data: any;
83     data = this.dashboardApiService.getDashboardList().toPromise();
84     return data;
85   }
86
87   async initDbsList(dbList: []) {
88     var d: Dashboard[] = [];
89
90     if (dbList.length > 0) {
91       for (var i = 0; i < dbList.length; i++) {
92         let data = dbList[i];
93         let feed = {
94           name: data["name"],
95           host: data["host"],
96           port: data["port"],
97           login: data["login"],
98           pass: data["pass"],
99           enabled: data["enabled"]
100         };
101         d.push(feed);
102       }
103     }
104     return d;
105   }
106
107   openDashboardModal(thisIndex: number) {
108     var modalRef, index;
109     index = thisIndex;
110     console.log(index, "index,add or edit");
111     modalRef = this.modalService.open(CreateDashboardComponent, {
112       size: "lg",
113       centered: true
114     });
115     modalRef.componentInstance.dashboard = this.dbs[index];
116     modalRef.componentInstance.passEntry.subscribe(receiveEntry => {
117       this.dbs[index] = receiveEntry;
118       let host = this.dbs[index].host;
119       let enabled = this.dbs[index].enabled;
120       console.log(receiveEntry);
121       if (enabled == true) {
122         // Db name found, to update db
123         this.dashboardApiService.createUpadteDashboard(this.dbs[index]).subscribe(
124           res => {
125             console.log(res);
126             if (res.statusCode == 200) {
127               this.initList();
128               this.notificationService.success("SUCCESSFULLY_UPDATED");
129             } else {
130               this.notificationService.error("FAILED_UPDATED");
131             }
132             modalRef.close();
133           },
134           err => {
135             this.notificationService.error(err);
136             modalRef.close();
137           }
138         );
139       } else {
140         this.dashboardApiService.deleteDashboard(this.dbs[thisIndex]).subscribe(
141           res => {
142             console.log(res);
143             if (res.statusCode == 200) {
144               this.initList();
145               this.notificationService.success("SUCCESSFULLY_DELETED");
146             } else {
147               this.dbs[thisIndex].enabled = true;
148               this.notificationService.error("FAILED_DELETED");
149             }
150             modalRef.close();
151           },
152           err => {
153             this.notificationService.error(err);
154             modalRef.close();
155           }
156         );
157
158       }
159
160     });
161   }
162 }