de76a9d6cdc05dc89436f14e0edbae020409842e
[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 { RestApiService } from "src/app/core/services/rest-api.service";
18 import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
19 import { Template } 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/shared/components/alert/alert.component";
27 // notify
28 import { ToastrNotificationService } from "src/app/shared/components/toastr-notification/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: Template;
41   dashboardDeteleModelShow = true;
42   loadingIndicator: boolean = true;
43   mesgNoData = {
44     emptyMessage: `
45       <div class="d-flex justify-content-center">
46         <div class="p-2">
47           <label class="dl-nodata">No Data</label>
48         </div>
49       </div>
50     `
51   };
52   @ViewChild("searchText") searchText: ElementRef;
53   constructor(
54     private modalService: NgbModal,
55     private dashboardApiService: RestApiService,
56     private spinner: NgxSpinnerService,
57     private notificationService: ToastrNotificationService,
58   ) {
59     setTimeout(() => {
60       this.loadingIndicator = false;
61     }, 5000);
62
63     this.initData().then(data => {
64       this.initTemplateList(this.template_list).then(
65         data => {
66           // for cache of datatable
67           this.temps = [...data];
68           this.templates = data;
69           setTimeout(() => {
70             this.spinner.hide();
71           }, 500);
72         }
73       );
74     });
75   }
76
77   ngOnInit() {
78     this.spinner.show();
79   }
80
81   async initData() {
82     this.template_list = [];
83     this.template_list = await this.getTemplateList();
84     this.Template_New = new Template();
85     this.Template_Newbody = new Template();
86     return true;
87   }
88
89
90   getTemplateList() {
91     return this.dashboardApiService.getTemplateAll().toPromise();
92   }
93
94   async initTemplateList(template_list: []) {
95     var t: Template[] = [];
96     for (var i = 0; i < template_list.length; i++) {
97       let data = template_list[i];
98       let feed = {
99         id: data["id"],
100         name: data["name"],
101         submitted: data["submitted"],
102         body: data["body"],
103         note: data["note"],
104         topicName: data["topicName"],
105         designType: data["designType"],
106         designTypeName: data["designTypeName"],
107         dbs: data["dbs"],
108       };
109       t.push(feed);
110     }
111     return t;
112   }
113
114   newTemplateModal() {
115     const modalRef = this.modalService.open(NewTemplateModalComponent, {
116       windowClass: "dl-md-modal templatess",
117       centered: true
118     });
119     this.Template_New = new Template();
120     this.Template_Newbody = new Template();
121     modalRef.componentInstance.template = this.Template_Newbody;
122     modalRef.componentInstance.templatelist_length = this.template_list.length;
123     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
124       this.Template_Newbody = receivedEntry;
125       this.dashboardApiService
126         .createNewTemplate(this.Template_Newbody)
127         .subscribe(
128           res => {
129             if (res.statusCode == 200) {
130               this.Template_New = res.returnBody;
131               this.template_list.push(this.Template_New);
132               this.template_list = [...this.template_list];
133               this.notificationService.success("SUCCESSFULLY_CREARED");
134             } else {
135               this.notificationService.error("FAILED_CREARED");
136             }
137             modalRef.close();
138           },
139           err => {
140             this.notificationService.error(err);
141             modalRef.close();
142           }
143         );
144     });
145   }
146
147   onActivate(event) {
148     const emitType = event.type;
149     if (emitType == "dblclick") {
150       let id = event.row.id;
151       this.editTemplateModal(id);
152     }
153
154   }
155
156   editTemplateModal(id) {
157     const index = this.template_list.findIndex(t => t.id === id);
158     const modalRef = this.modalService.open(EditTemplateModalComponent, {
159       windowClass: "dl-md-modal templatess",
160       centered: true
161     });
162     modalRef.componentInstance.edittemplate = this.template_list[index];
163     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
164       this.Template_New = receivedEntry;
165       this.dashboardApiService
166         .updateNewTemplate(this.Template_New)
167         .subscribe(
168           res => {
169             if (res.statusCode == 200) {
170               this.template_list[index] = this.Template_New;
171               this.template_list = [...this.template_list];
172               this.notificationService.success("SUCCESSFULLY_UPDATED");
173             } else {
174               this.notificationService.error("FAILED_UPDATED");
175             }
176             modalRef.close();
177           },
178           err => {
179             this.notificationService.error(err);
180             modalRef.close();
181           }
182         );
183     })
184   }
185
186   deleteTemplateModel(id: number) {
187     const index = this.template_list.findIndex(t => t.id === id);
188     const modalRef = this.modalService.open(AlertComponent, {
189       size: "sm",
190       centered: true
191     });
192     // modalRef.componentInstance.dashboardDeteleModelShow = this.dashboardDeteleModelShow;
193     modalRef.componentInstance.message = "ARE_YOU_SURE_DELETE";
194     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
195       // Delete database
196       this.dashboardApiService.DeleteTemplate(id).subscribe(
197         res => {
198           if (JSON.stringify(res).length <= 2) {
199             this.template_list.splice(index, 1);
200             this.template_list = [...this.template_list];
201             this.notificationService.success("SUCCESSFULLY_DELETED");
202           } else {
203             this.notificationService.error("FAILED_DELETED");
204           }
205
206           modalRef.close();
207         },
208         err => {
209           this.notificationService.error(err);
210           modalRef.close();
211         }
212       );
213     });
214
215   }
216
217   deployTemplate(id: number) {
218     const index = this.template_list.findIndex(t => t.id === id);
219     const body = this.template_list[index];
220     this.spinner.show();
221     this.dashboardApiService.deployTemplateKibana(id, body).subscribe(
222       res => {
223         this.spinner.hide();
224         let processArr = []
225         Object.keys(res).map(item =>
226           processArr.push({ name: item, status: res[item] })
227         )
228
229         if (processArr.length !== 0) {
230           processArr.map(item =>
231             item.status ?
232               setTimeout(() => { this.notificationService.success("Deploy_SUCCESSFULLY") }, 1000) :
233               setTimeout(() => { this.notificationService.error("Deploy_FAILED") }, 2000))
234         } else {
235           this.notificationService.error("Deploy_FAILED");
236         }
237       },
238       err => {
239         this.spinner.hide();
240         this.notificationService.error(err);
241       }
242     );
243   }
244
245   updateFilter(searchValue) {
246     const val = searchValue.toLowerCase();
247     // filter our data
248     const temps = this.temps.filter(function (d) {
249       return d.name.toLowerCase().indexOf(val) !== -1 || !val;
250     });
251     // update the rows
252     this.template_list = temps;
253   }
254
255
256 }