8bb4126cd52dcdc0346dcbc2fda2a774188fe591
[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 {DashboardApiService} from "src/app/core/services/dashboard-api.service";
25
26 import {AlertComponent} from "src/app/core/alert/alert.component";
27
28 // Notify
29 import {ToastrNotificationService} from "src/app/core/services/toastr-notification.service";
30 // Loading spinner
31 import {NgxSpinnerService} from "ngx-spinner";
32
33 @Component({
34   selector: 'app-dashboard-list',
35   templateUrl: './dashboard-list.component.html',
36   styleUrls: ['./dashboard-list.component.css']
37 })
38 export class DashboardListComponent implements OnInit {
39   @Output() passEntry: EventEmitter<any> = new EventEmitter();
40   dbList: any = [];
41   dbs: Dashboard[] = [];
42
43   loading: Boolean = true;
44
45   tempDbDetail: Dashboard;
46   dashboardDeteleModelShow = true;
47
48   // nameArr = [];
49
50   constructor(
51     private adminService: AdminService,
52     private dashboardApiService: DashboardApiService,
53     private notificationService: ToastrNotificationService,
54     private modalService: NgbModal,
55     private spinner: NgxSpinnerService
56   ) {
57     // Set page title
58     this.adminService.setTitle("SIDEBAR.DASHBOARDLIST");
59     this.initList();
60
61   }
62
63   ngOnInit() {
64     this.spinner.show();
65   }
66   initList(){
67     this.initData().then(data => {
68       this.initDbsList(this.dbList).then(data => {
69         this.dbs = data;
70         console.log(this.dbs, "dasboard-dbs[]")
71       });
72     });
73   }
74
75   async initData() {
76     this.dbList = [];
77     this.dbList = await this.getDbList();
78     setTimeout(() => {
79       this.spinner.hide();
80     }, 500);
81   }
82
83   getDbList() {
84     var data: any;
85     data = this.dashboardApiService.getDashboardList().toPromise();
86     return data;
87   }
88
89   async initDbsList(dbList: []) {
90     var d: Dashboard[] = [];
91
92     if (dbList.length > 0) {
93       for (var i = 0; i < dbList.length; i++) {
94         let data = dbList[i];
95         let feed = {
96           name: data["name"],
97           host: data["host"],
98           port: data["port"],
99           login: data["login"],
100           pass: data["pass"],
101           enabled: data["enabled"]
102         };
103         d.push(feed);
104       }
105     }
106     return d;
107   }
108
109   openDashboardModal(thisIndex: number) {
110     var modalRef, index;
111     index = thisIndex;
112     console.log(index, "index,add or edit");
113     modalRef = this.modalService.open(CreateDashboardComponent, {
114       size: "lg",
115       centered: true
116     });
117     modalRef.componentInstance.dashboard = this.dbs[index];
118     modalRef.componentInstance.passEntry.subscribe(receiveEntry => {
119       this.dbs[index] = receiveEntry;
120       let host = this.dbs[index].host;
121       let enabled = this.dbs[index].enabled;
122       console.log(receiveEntry);
123       if (enabled == true) {
124         // Db name found, to update db
125         this.dashboardApiService.createUpadteDashboard(this.dbs[index]).subscribe(
126           res => {
127             console.log(res);
128             if (res.statusCode == 200) {
129               this.initList();
130               this.notificationService.success("SUCCESSFULLY_UPDATED");
131             } else {
132               this.notificationService.error("FAILED_UPDATED");
133             }
134             modalRef.close();
135           },
136           err => {
137             this.notificationService.error(err);
138             modalRef.close();
139           }
140         );
141       } else {
142         this.dashboardApiService.deleteDashboard(this.dbs[thisIndex]).subscribe(
143           res => {
144             console.log(res);
145             if (res.statusCode == 200) {
146               this.initList();
147               this.notificationService.success("SUCCESSFULLY_DELETED");
148             } else {
149               this.dbs[thisIndex].enabled = true;
150               this.notificationService.error("FAILED_DELETED");
151             }
152             modalRef.close();
153           },
154           err => {
155             this.notificationService.error(err);
156             modalRef.close();
157           }
158         );
159
160       }
161
162     });
163   }
164 }