9d774c49d0ee4b874945d7677aabec44086b7874
[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
55   constructor(
56     private modalService: NgbModal,
57     private dashboardApiService: DashboardApiService,
58     private spinner: NgxSpinnerService,
59     private notificationService: ToastrNotificationService,
60   ) {
61     setTimeout(() => {
62       this.loadingIndicator = false;
63     }, 5000);
64
65     this.initData().then(data => {
66       this.initTemplateList(this.template_list).then(
67         data => {
68           // for cache of datatable
69           this.temps = [...data];
70           this.templates = data;
71           setTimeout(() => {
72             this.spinner.hide();
73           }, 500);
74         }
75       );
76     });
77   }
78
79   ngOnInit() {
80     this.spinner.show();
81   }
82
83   async initData() {
84     this.template_list = [];
85     this.template_list = await this.getTemplateList();
86     this.Template_New = new Template();
87     this.Template_Newbody = new newTemplate();
88     return true;
89   }
90
91
92   getTemplateList() {
93     return this.dashboardApiService.getTemplateAll().toPromise();
94   }
95
96   async initTemplateList(template_list: []) {
97     var t: Template[] = [];
98     for (var i = 0; i < template_list.length; i++) {
99       let data = template_list[i];
100       let feed = {
101         id: data["id"],
102         name: data["name"],
103         submitted: data["submitted"],
104         body: data["body"],
105         note: data["note"],
106         topic: data["topic"],
107         designType: data["designType"],
108       };
109       t.push(feed);
110     }
111     return t;
112   }
113
114   newTemplateModal() {
115     this.selectedLangs = sessionStorage.getItem("selectedLang");
116     let tips = "";
117     const modalRef = this.modalService.open(NewTemplateModalComponent, {
118       windowClass: "dl-md-modal",
119       centered: true
120     });
121     this.Template_New = new Template();
122     this.Template_Newbody = new newTemplate();
123     modalRef.componentInstance.template = this.Template_Newbody;
124     modalRef.componentInstance.selectedLangs = this.selectedLangs;
125     modalRef.componentInstance.templatelist_length = this.template_list.length;
126     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
127       this.Template_Newbody = receivedEntry;
128       let name = this.Template_Newbody.name;
129       this.dashboardApiService
130         .createNewTemplate(this.Template_Newbody)
131         .subscribe(
132           res => {
133             if (res.statusCode == 200) {
134               this.Template_New = res.returnBody;
135               console.log(this.Template_New,"this.Template_New add Success");
136               this.template_list.push(this.Template_New);
137               this.template_list = [...this.template_list];
138               console.log(this.template_list, "this.template_list,inserted");
139               if (this.selectedLangs == "en-us") {
140                 tips = "Success inserted."
141               } else if (this.selectedLangs == "zh-hans") {
142                 tips = "新增成功。"
143               } else if (this.selectedLangs == "zh-hant") {
144                 tips = "新增成功。"
145               }
146               this.notificationService.success('"' + name + '"' + tips);
147             } else {
148               if (this.selectedLangs == "en-us") {
149                 tips = "Fail inserted."
150               } else if (this.selectedLangs == "zh-hans") {
151                 tips = "新增失败。"
152               } else if (this.selectedLangs == "zh-hant") {
153                 tips = "新增失敗。"
154               }
155               this.notificationService.error('"' + name + '"' + tips);
156             }
157             modalRef.close();
158           },
159           err => {
160             this.notificationService.error(err);
161             modalRef.close();
162           }
163         );
164     });
165   }
166
167   editTemplateModal(id: number) {
168     this.selectedLangs = sessionStorage.getItem("selectedLang");
169     let tips = "";
170     const index = this.template_list.findIndex(t => t.id === id);
171     // const name = this.template_list[index].name;
172     const modalRef = this.modalService.open(EditTemplateModalComponent, {
173       windowClass: "dl-md-modal",
174       centered: true
175     });
176     modalRef.componentInstance.selectedLangs = this.selectedLangs;
177     modalRef.componentInstance.edittemplate = this.template_list[index];
178     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
179       const name = receivedEntry.name;
180       this.Template_New = receivedEntry;
181       this.dashboardApiService
182         .updateNewTemplate(this.Template_New)
183         .subscribe(
184           res => {
185
186             if (res.statusCode == 200) {
187               this.template_list[index] = this.Template_New;
188               this.template_list = [...this.template_list];
189               console.log(this.template_list, "this.template_list,update");
190               if (this.selectedLangs == "en-us") {
191                 tips = "Success updated."
192               } else if (this.selectedLangs == "zh-hans") {
193                 tips = "更新成功。"
194               } else if (this.selectedLangs == "zh-hant") {
195                 tips = "更新成功。"
196               }
197               this.notificationService.success('"' + name + '"' + tips);
198
199             } else {
200               if (this.selectedLangs == "en-us") {
201                 tips = "Fail updated."
202               } else if (this.selectedLangs == "zh-hans") {
203                 tips = "更新失败。"
204               } else if (this.selectedLangs == "zh-hant") {
205                 tips = "更新失敗。"
206               }
207               this.notificationService.error('"' + name + '"' + tips);
208             }
209             modalRef.close();
210           },
211           err => {
212             this.notificationService.error(err);
213             modalRef.close();
214           }
215         );
216     })
217   }
218
219   deleteTemplateModel(id: number) {
220     this.selectedLangs = sessionStorage.getItem("selectedLang");
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 index = this.template_list.findIndex(t => t.id === id);
230     const modalRef = this.modalService.open(AlertComponent, {
231       size: "sm",
232       centered: true
233     });
234     const name = this.template_list[index].name;
235     // modalRef.componentInstance.dashboardDeteleModelShow = this.dashboardDeteleModelShow;
236     modalRef.componentInstance.message =
237       tips + '"' + name + '"' + ' ?';
238     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
239       // Delete database
240       this.dashboardApiService.DeleteTemplate(id).subscribe(
241         res => {
242           console.log(res,"template detele res");
243           console.log(JSON.stringify(res).length,"JSON.stringify(res).length");
244           if (JSON.stringify(res).length<=2) {
245             console.log("Success deleted template");
246             this.template_list.splice(index, 1);
247             this.template_list = [...this.template_list];
248             if (this.selectedLangs == "en-us") {
249               tips = "Success deleted."
250             } else if (this.selectedLangs == "zh-hans") {
251               tips = "删除成功。"
252             } else if (this.selectedLangs == "zh-hant") {
253               tips = "刪除成功。"
254             }
255             this.notificationService.success('"' + name + '"' + tips);
256           } else {
257             console.log("Fail deleted template");
258             if (this.selectedLangs == "en-us") {
259               tips = "Fail deleted."
260             } else if (this.selectedLangs == "zh-hans") {
261               tips = "删除失败。"
262             } else if (this.selectedLangs == "zh-hant") {
263               tips = "刪除失敗。"
264             }
265             this.notificationService.error('"' + name + '"' + tips);
266           }
267
268           modalRef.close();
269         },
270         err => {
271           this.notificationService.error(err);
272           modalRef.close();
273         }
274       );
275     });
276
277   }
278
279   deployTemplate(id: number) {
280     this.selectedLangs = sessionStorage.getItem("selectedLang");
281     let tips = "";
282     const index = this.template_list.findIndex(t => t.id === id);
283     const name = this.template_list[index].name;
284     const body = this.template_list[index];
285     this.spinner.show();
286     // this.dashboardApiService.deployTemplate(id).subscribe(
287     //   res => {
288     //     console.log(res,"reshost");
289     //     const host = res.source.host,
290     //           port = res.source.port;
291     //     if (res.status == "success") {
292     //       console.log(res)
293
294     this.dashboardApiService.deployTemplateKibana(id, body).subscribe(
295       res => {
296         this.spinner.hide();
297         console.log(res,"template deploy res");
298         console.log(JSON.stringify(res).length,"JSON.stringify(res).length");
299         if (JSON.stringify(res).length<=2) {
300           if (this.selectedLangs == "en-us") {
301             tips = "Success deploy."
302           } else if (this.selectedLangs == "zh-hans") {
303             tips = "部署成功。"
304           } else if (this.selectedLangs == "zh-hant") {
305             tips = "部署成功。"
306           }
307           this.notificationService.success('"' + name + '"' + tips);
308         } else {
309           if (this.selectedLangs == "en-us") {
310             tips = "Fail deleted."
311           } else if (this.selectedLangs == "zh-hans") {
312             tips = "部署失败。"
313           } else if (this.selectedLangs == "zh-hant") {
314             tips = "部署失敗。"
315           }
316           this.notificationService.error('"' + name + '"' + tips);
317         }
318       },
319       err => {
320         this.spinner.hide();
321         this.notificationService.error(err);
322       }
323     );
324     //     } else {
325     //       this.spinner.hide();
326     //       this.notificationService.error( name + " Fail deploy.");
327     //     }
328     //   },
329     //   err => {
330     //     this.spinner.hide();
331     //     this.notificationService.error(err);
332     //   }
333     // );
334
335   }
336
337   updateFilter(searchValue) {
338     const val = searchValue.toLowerCase();
339     // filter our data
340     const temps = this.temps.filter(function (d) {
341       return d.name.toLowerCase().indexOf(val) !== -1 || !val;
342     });
343     // update the rows
344     this.template_list = temps;
345   }
346
347
348 }