f0eac0096860c059b05bc0b60ccd24f991578925
[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") || "en-us";
47   dashboardDeteleModelShow = true;
48
49   // nameArr = [];
50
51   constructor(
52     private adminService: AdminService,
53     private dashboardApiService: DashboardApiService,
54     private notificationService: ToastrNotificationService,
55     private modalService: NgbModal,
56     private spinner: NgxSpinnerService
57   ) {
58     // Set page title
59     this.adminService.setTitle("SIDEBAR.DASHBOARDLIST");
60     // this.getName();
61     this.initData().then(data => {
62       this.initDbsList(this.dbList).then(data => {
63         this.dbs = data;
64         console.log(this.dbs, "dasboard-dbs[]")
65       });
66     });
67   }
68
69   ngOnInit() {
70     this.spinner.show();
71   }
72
73
74   async initData() {
75     this.dbList = [];
76     this.dbList = await this.getDbList();
77     setTimeout(() => {
78       this.spinner.hide();
79     }, 500);
80   }
81
82   getDbList() {
83     var data: any;
84     data = this.dashboardApiService.getDashboardList().toPromise();
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   openDashboardModal(thisIndex: number) {
109     var modalRef, index;
110     this.selectedLangs = sessionStorage.getItem("selectedLang") || "en-us";
111     let tips = "";
112     index = thisIndex;
113     console.log(index, "index,add or edit");
114     modalRef = this.modalService.open(CreateDashboardComponent, {
115       size: "lg",
116       centered: true
117     });
118     modalRef.componentInstance.dashboard = this.dbs[index];
119     modalRef.componentInstance.passEntry.subscribe(receiveEntry => {
120       this.dbs[index] = receiveEntry;
121       let host = this.dbs[index].host;
122       let enabled = this.dbs[index].enabled;
123       console.log(receiveEntry);
124       if (enabled == true) {
125         // Db name found, to update db
126         this.dashboardApiService.createUpadteDashboard(this.dbs[index]).subscribe(
127           res => {
128             console.log(res);
129             if (res.statusCode == 200) {
130               this.initData();
131               if (this.selectedLangs == "en-us") {
132                 tips = "Success updated."
133               } else if (this.selectedLangs == "zh-hans") {
134                 tips = "更新成功。"
135               } else if (this.selectedLangs == "zh-hant") {
136                 tips = "更新成功。"
137               }
138               this.notificationService.success('"' + host + '"' + tips);
139             } else {
140               if (this.selectedLangs == "en-us") {
141                 tips = "Fail updated."
142               } else if (this.selectedLangs == "zh-hans") {
143                 tips = "更新失败。"
144               } else if (this.selectedLangs == "zh-hant") {
145                 tips = "更新失敗。"
146               }
147               this.notificationService.error('"' + host + '"' + tips);
148             }
149             modalRef.close();
150           },
151           err => {
152             this.notificationService.error(err);
153             modalRef.close();
154           }
155         );
156       } else {
157         this.dashboardApiService.deleteDashboard(this.dbs[thisIndex]).subscribe(
158           res => {
159             console.log(res);
160             if (res.statusCode == 200) {
161               this.initData();
162               if (this.selectedLangs == "en-us") {
163                 tips = "Success deleted."
164               } else if (this.selectedLangs == "zh-hans") {
165                 tips = "删除成功。"
166               } else if (this.selectedLangs == "zh-hant") {
167                 tips = "刪除成功。"
168               }
169               this.notificationService.success('"' + host + '"' + tips);
170             } else {
171               this.dbs[thisIndex].enabled = true;
172               if (this.selectedLangs == "en-us") {
173                 tips = "Fail deleted."
174               } else if (this.selectedLangs == "zh-hans") {
175                 tips = "删除失败。"
176               } else if (this.selectedLangs == "zh-hant") {
177                 tips = "刪除失敗。"
178               }
179               this.notificationService.error('"' + host + '"' + tips);
180             }
181             modalRef.close();
182           },
183           err => {
184             this.notificationService.error(err);
185             modalRef.close();
186           }
187         );
188
189       }
190
191     });
192   }
193 }