328ceeb5f3cd1edb3de1577c41ac623a16c90de1
[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, OnInit, ViewChild, ElementRef} from '@angular/core';
17 import {DashboardApiService} from "src/app/core/services/dashboard-api.service";
18 import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
19 import {Template,newTemplate} from "src/app/core/models/template.model";
20 // Loading spinner
21 import {NgxSpinnerService} from "ngx-spinner";
22
23 // modal
24 import {NewTemplateModalComponent} from "./new-template-modal/new-template-modal.component";
25 import {EditTemplateModalComponent} from "./edit-template-modal/edit-template-modal.component";
26 import {AlertComponent} from "src/app/core/alert/alert.component";
27 // notify
28 import {ToastrNotificationService} from "src/app/core/services/toastr-notification.service";
29
30 @Component({
31   selector: 'app-template-list',
32   templateUrl: './template-list.component.html',
33   styleUrls: ['./template-list.component.css']
34 })
35 export class TemplateListComponent {
36   template_list: any = [];
37   templates: Template[] = [];
38   temps: Template[] = [];
39   Template_New: Template;
40   Template_Newbody: newTemplate;
41   dashboardDeteleModelShow = true;
42   loadingIndicator: boolean = true;
43   selectedLangs = sessionStorage.getItem("selectedLang");
44   mesgNoData = {
45     emptyMessage: `
46       <div class="d-flex justify-content-center">
47         <div class="p-2">
48           <label class="dl-nodata">No Data</label>
49         </div>
50       </div>
51     `
52   };
53   @ViewChild("searchText") searchText: ElementRef;
54   constructor(
55     private modalService: NgbModal,
56     private dashboardApiService: DashboardApiService,
57     private spinner: NgxSpinnerService,
58     private notificationService: ToastrNotificationService,
59   ) {
60     setTimeout(() => {
61       this.loadingIndicator = false;
62     }, 5000);
63
64     this.initData().then(data => {
65       this.initTemplateList(this.template_list).then(
66         data => {
67           // for cache of datatable
68           this.temps = [...data];
69           this.templates = data;
70           setTimeout(() => {
71             this.spinner.hide();
72           }, 500);
73         }
74       );
75     });
76   }
77
78   ngOnInit() {
79     this.spinner.show();
80   }
81
82   async initData() {
83     this.template_list = [];
84     this.template_list = await this.getTemplateList();
85     this.Template_New = new Template();
86     this.Template_Newbody = new newTemplate();
87     return true;
88   }
89
90
91   getTemplateList() {
92     return this.dashboardApiService.getTemplateAll().toPromise();
93   }
94
95   async initTemplateList(template_list: []) {
96     var t: Template[] = [];
97     for (var i = 0; i < template_list.length; i++) {
98       let data = template_list[i];
99       let feed = {
100         id: data["id"],
101         name: data["name"],
102         submitted: data["submitted"],
103         body: data["body"],
104         note: data["note"],
105         topic: data["topic"],
106         designType: data["designType"],
107       };
108       t.push(feed);
109     }
110     return t;
111   }
112
113   newTemplateModal() {
114     this.selectedLangs = sessionStorage.getItem("selectedLang");
115     let tips = "";
116     const modalRef = this.modalService.open(NewTemplateModalComponent, {
117       windowClass: "dl-md-modal templatess",
118       centered: true
119     });
120     this.Template_New = new Template();
121     this.Template_Newbody = new newTemplate();
122     modalRef.componentInstance.template = this.Template_Newbody;
123     modalRef.componentInstance.selectedLangs = this.selectedLangs;
124     modalRef.componentInstance.templatelist_length = this.template_list.length;
125     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
126       this.Template_Newbody = receivedEntry;
127       let name = this.Template_Newbody.name;
128       this.dashboardApiService
129         .createNewTemplate(this.Template_Newbody)
130         .subscribe(
131           res => {
132             if (res.statusCode == 200) {
133               this.Template_New = res.returnBody;
134               console.log(this.Template_New,"this.Template_New add Success");
135               this.template_list.push(this.Template_New);
136               this.template_list = [...this.template_list];
137               console.log(this.template_list, "this.template_list,inserted");
138               if (this.selectedLangs == "en-us") {
139                 tips = "Successfully created."
140               } else if (this.selectedLangs == "zh-hans") {
141                 tips = "新增成功。"
142               } else if (this.selectedLangs == "zh-hant") {
143                 tips = "新增成功。"
144               }
145               this.notificationService.success(tips);
146             } else {
147               if (this.selectedLangs == "en-us") {
148                 tips = "Fail created."
149               } else if (this.selectedLangs == "zh-hans") {
150                 tips = "新增失败。"
151               } else if (this.selectedLangs == "zh-hant") {
152                 tips = "新增失敗。"
153               }
154               this.notificationService.error(tips);
155             }
156             modalRef.close();
157           },
158           err => {
159             this.notificationService.error(err);
160             modalRef.close();
161           }
162         );
163     });
164   }
165
166   onActivate(event) {
167     const emitType = event.type;
168     if(emitType == "dblclick"){
169       console.log('Activate Event', event);
170       let id = event.row.id;
171       this.editTemplateModal(id);
172     }
173
174   }
175
176   editTemplateModal(id) {
177     this.selectedLangs = sessionStorage.getItem("selectedLang");
178     let tips = "";
179     const index = this.template_list.findIndex(t => t.id === id);
180     // const name = this.template_list[index].name;
181     const modalRef = this.modalService.open(EditTemplateModalComponent, {
182       windowClass: "dl-md-modal templatess",
183       centered: true
184     });
185     modalRef.componentInstance.selectedLangs = this.selectedLangs;
186     modalRef.componentInstance.edittemplate = this.template_list[index];
187     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
188       const name = receivedEntry.name;
189       this.Template_New = receivedEntry;
190       this.dashboardApiService
191         .updateNewTemplate(this.Template_New)
192         .subscribe(
193           res => {
194
195             if (res.statusCode == 200) {
196               this.template_list[index] = this.Template_New;
197               this.template_list = [...this.template_list];
198               console.log(this.template_list, "this.template_list,update");
199               if (this.selectedLangs == "en-us") {
200                 tips = "Successfully updated."
201               } else if (this.selectedLangs == "zh-hans") {
202                 tips = "更新成功。"
203               } else if (this.selectedLangs == "zh-hant") {
204                 tips = "更新成功。"
205               }
206               this.notificationService.success('"' + name + '"' + tips);
207
208             } else {
209               if (this.selectedLangs == "en-us") {
210                 tips = "Fail updated."
211               } else if (this.selectedLangs == "zh-hans") {
212                 tips = "更新失败。"
213               } else if (this.selectedLangs == "zh-hant") {
214                 tips = "更新失敗。"
215               }
216               this.notificationService.error('"' + name + '"' + tips);
217             }
218             modalRef.close();
219           },
220           err => {
221             this.notificationService.error(err);
222             modalRef.close();
223           }
224         );
225     })
226   }
227
228   deleteTemplateModel(id: number) {
229     this.selectedLangs = sessionStorage.getItem("selectedLang");
230     let tips = "";
231     if (this.selectedLangs == "en-us") {
232       tips = "Are you sure you want to delete ";
233     } else if (this.selectedLangs == "zh-hans") {
234       tips = "您确定您要删除";
235     } else if (this.selectedLangs == "zh-hant") {
236       tips = "您確定您要刪除";
237     }
238     const index = this.template_list.findIndex(t => t.id === id);
239     const modalRef = this.modalService.open(AlertComponent, {
240       size: "sm",
241       centered: true
242     });
243     const name = this.template_list[index].name;
244     // modalRef.componentInstance.dashboardDeteleModelShow = this.dashboardDeteleModelShow;
245     modalRef.componentInstance.message =
246       tips + '"' + name + '"' + ' ?';
247     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
248       // Delete database
249       this.dashboardApiService.DeleteTemplate(id).subscribe(
250         res => {
251           console.log(res,"template detele res");
252           console.log(JSON.stringify(res).length,"JSON.stringify(res).length");
253           if (JSON.stringify(res).length<=2) {
254             console.log("Success deleted template");
255             this.template_list.splice(index, 1);
256             this.template_list = [...this.template_list];
257             if (this.selectedLangs == "en-us") {
258               tips = "Successfully deleted."
259             } else if (this.selectedLangs == "zh-hans") {
260               tips = "删除成功。"
261             } else if (this.selectedLangs == "zh-hant") {
262               tips = "刪除成功。"
263             }
264             this.notificationService.success(tips);
265           } else {
266             console.log("Fail deleted template");
267             if (this.selectedLangs == "en-us") {
268               tips = "Fail deleted."
269             } else if (this.selectedLangs == "zh-hans") {
270               tips = "删除失败。"
271             } else if (this.selectedLangs == "zh-hant") {
272               tips = "刪除失敗。"
273             }
274             this.notificationService.error(tips);
275           }
276
277           modalRef.close();
278         },
279         err => {
280           this.notificationService.error(err);
281           modalRef.close();
282         }
283       );
284     });
285
286   }
287
288   deployTemplate(id: number) {
289     this.selectedLangs = sessionStorage.getItem("selectedLang");
290     let tips = "";
291     const index = this.template_list.findIndex(t => t.id === id);
292     const name = this.template_list[index].name;
293     const body = this.template_list[index];
294     this.spinner.show();
295     // this.dashboardApiService.deployTemplate(id).subscribe(
296     //   res => {
297     //     console.log(res,"reshost");
298     //     const host = res.source.host,
299     //           port = res.source.port;
300     //     if (res.status == "success") {
301     //       console.log(res)
302
303     this.dashboardApiService.deployTemplateKibana(id, body).subscribe(
304       res => {
305         this.spinner.hide();
306         console.log(res,"template deploy res");
307         console.log(JSON.stringify(res).length,"JSON.stringify(res).length");
308         if (JSON.stringify(res).length<=2) {
309           if (this.selectedLangs == "en-us") {
310             tips = "deploy successful."
311           } else if (this.selectedLangs == "zh-hans") {
312             tips = "部署成功。"
313           } else if (this.selectedLangs == "zh-hant") {
314             tips = "部署成功。"
315           }
316           this.notificationService.success('"' + name + '"' + tips);
317         } else {
318           if (this.selectedLangs == "en-us") {
319             tips = "deploy fail."
320           } else if (this.selectedLangs == "zh-hans") {
321             tips = "部署失败。"
322           } else if (this.selectedLangs == "zh-hant") {
323             tips = "部署失敗。"
324           }
325           this.notificationService.error('"' + name + '"' + tips);
326         }
327       },
328       err => {
329         this.spinner.hide();
330         this.notificationService.error(err);
331       }
332     );
333     //     } else {
334     //       this.spinner.hide();
335     //       this.notificationService.error( name + " Fail deploy.");
336     //     }
337     //   },
338     //   err => {
339     //     this.spinner.hide();
340     //     this.notificationService.error(err);
341     //   }
342     // );
343
344   }
345
346   updateFilter(searchValue) {
347     const val = searchValue.toLowerCase();
348     // filter our data
349     const temps = this.temps.filter(function (d) {
350       return d.name.toLowerCase().indexOf(val) !== -1 || !val;
351     });
352     // update the rows
353     this.template_list = temps;
354   }
355
356
357 }