a2966a4e3337910e149b38392f6365b7122b7f14
[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   selectedLangs = sessionStorage.getItem("selectedLang");
47   dashboardDeteleModelShow = true;
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.getName();
60     this.initData().then(data => {
61       this.initDbsList(this.dbList).then(data => {
62         this.dbs = data;
63         console.log(this.dbs, "dasboard-dbs[]")
64       });
65     });
66   }
67
68   ngOnInit() {
69     this.spinner.show();
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
85     return data;
86   }
87
88   async initDbsList(dbList: []) {
89     var d: Dashboard[] = [];
90
91     if (dbList.length > 0) {
92       for (var i = 0; i < dbList.length; i++) {
93         let data = dbList[i];
94         let feed = {
95           name: data["name"],
96           host: data["host"],
97           port: data["port"],
98           login: data["login"],
99           pass: data["pass"],
100           enabled: data["enabled"]
101         };
102         d.push(feed);
103       }
104     }
105     return d;
106   }
107
108
109   openCreateModal() {
110     let thisIndex = -1;
111     this.openDashboardModal(thisIndex);
112   }
113
114   openDashboardModal(thisIndex: number) {
115     var modalRef, index;
116     this.selectedLangs = sessionStorage.getItem("selectedLang");
117     let tips = "";
118
119
120     index = thisIndex;
121     console.log(index, "index,add or edit");
122     this.tempDbDetail = new Dashboard();
123     if (index != -1) {
124       modalRef = this.modalService.open(CreateDashboardComponent, {
125         size: "lg",
126         centered: true
127       });
128       modalRef.componentInstance.dashboard = this.dbs[index];
129     } else {
130       if(this.nameArr.length == 0 && this.dbs.length>0){
131         console.log("All types have been created, you cannot create existing types again.");
132          return false;
133       }else {
134         modalRef = this.modalService.open(CreateDashboardComponent, {
135           size: "lg",
136           centered: true
137         });
138         modalRef.componentInstance.dashboard = this.tempDbDetail;
139       }
140
141     }
142     modalRef.componentInstance.nameArr = this.nameArr;
143     modalRef.componentInstance.passEntry.subscribe(receiveEntry => {
144       this.tempDbDetail = receiveEntry;
145       let host = this.tempDbDetail.host;
146       console.log(receiveEntry);
147       if (index != -1) {
148         // Db name found, to update db
149         this.dashboardApiService.upadteDashboard(this.tempDbDetail).subscribe(
150           res => {
151             console.log(res);
152             if (res.statusCode == 200) {
153               this.dbs[index] = this.tempDbDetail;
154               if (this.selectedLangs == "en-us") {
155                 tips = "Success updated."
156               } else if (this.selectedLangs == "zh-hans") {
157                 tips = "更新成功。"
158               } else if (this.selectedLangs == "zh-hant") {
159                 tips = "更新成功。"
160               }
161               this.notificationService.success('"' + host + '"' + tips);
162             } else {
163               if (this.selectedLangs == "en-us") {
164                 tips = "Fail updated."
165               } else if (this.selectedLangs == "zh-hans") {
166                 tips = "更新失败。"
167               } else if (this.selectedLangs == "zh-hant") {
168                 tips = "更新失敗。"
169               }
170               this.notificationService.error('"' + host + '"' + tips);
171             }
172             modalRef.close();
173           },
174           err => {
175             this.notificationService.error(err);
176             modalRef.close();
177           }
178         );
179       } else {
180         // Db name not found, to insert db
181         this.dashboardApiService.addDashboard(this.tempDbDetail).subscribe(
182           res => {
183             console.log(res);
184             let tips = "";
185             if (res.statusCode == 200) {
186               this.dbs.push(this.tempDbDetail);
187               this.dbs = [...this.dbs];
188               this.getName();
189               if (this.selectedLangs == "en-us") {
190                 tips = "Success inserted."
191               } else if (this.selectedLangs == "zh-hans") {
192                 tips = "新增成功。"
193               } else if (this.selectedLangs == "zh-hant") {
194                 tips = "新增成功。"
195               }
196               this.notificationService.success('"' + host + '"' + tips);
197             }else {
198               if (this.selectedLangs == "en-us") {
199                 tips = "Fail inserted."
200               } else if (this.selectedLangs == "zh-hans") {
201                 tips = "新增失败。"
202               } else if (this.selectedLangs == "zh-hant") {
203                 tips = "新增失敗。"
204               }
205               this.notificationService.error('"' + host + '"' + tips);
206             }
207             modalRef.close();
208           },
209           err => {
210             this.notificationService.error(err);
211             modalRef.close();
212           }
213         );
214       }
215     });
216   }
217
218   deleteDashboard(thisIndex: number) {
219     this.selectedLangs = sessionStorage.getItem("selectedLang");
220     const index = thisIndex, host = this.dbs[thisIndex]["host"];
221     let tips = "";
222     if (this.selectedLangs == "en-us") {
223       tips = "Are you sure you want to delete ";
224     } else if (this.selectedLangs == "zh-hans") {
225       tips = "您确定您要删除";
226     } else if (this.selectedLangs == "zh-hant") {
227       tips = "您確定您要刪除";
228     }
229     const modalRef = this.modalService.open(AlertComponent, {
230       // size: "sm",
231       centered: true
232     });
233     modalRef.componentInstance.dashboardDeteleModelShow = this.dashboardDeteleModelShow;
234     modalRef.componentInstance.message =
235       tips + '"'+host + '"' + ' ?';
236     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
237       // Delete database
238       this.dbs[thisIndex].enabled = false;
239       this.dashboardApiService.deleteDashboard(this.dbs[thisIndex]).subscribe(
240         res => {
241           console.log(res);
242           if (res.statusCode == 200) {
243             // this.dbs[index].enabled = false;
244             this.dbs.splice(index, 1);
245             this.getName();
246             if (this.selectedLangs == "en-us") {
247               tips = "Success deleted."
248             } else if (this.selectedLangs == "zh-hans") {
249               tips = "删除成功。"
250             } else if (this.selectedLangs == "zh-hant") {
251               tips = "刪除成功。"
252             }
253             this.notificationService.success('"' + host + '"' + tips);
254           } else {
255             this.dbs[thisIndex].enabled = true;
256             if (this.selectedLangs == "en-us") {
257               tips = "Fail deleted."
258             } else if (this.selectedLangs == "zh-hans") {
259               tips = "删除失败。"
260             } else if (this.selectedLangs == "zh-hant") {
261               tips = "刪除失敗。"
262             }
263             this.notificationService.error('"' + host + '"' + tips);
264           }
265           modalRef.close();
266         },
267         err => {
268           this.notificationService.error(err);
269           modalRef.close();
270         }
271       );
272     });
273   }
274
275   getName(){
276     this.dashboardApiService.getDashboardName().subscribe(data => {
277       this.nameArr = data;
278       console.log(this.nameArr,"this.nameArr111");
279     });
280     console.log(this.nameArr,"this.nameArr222");
281   }
282
283 }