3f0223eb56ba6d6b4c89233be2cb23d17af3bffd
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake
4  * ================================================================================
5  * Copyright 2019 - 2020 QCT
6  *=================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  *
23  * @author Ekko Chang
24  *
25  */
26
27 import { Component, OnInit, Input, ViewChild } from "@angular/core";
28
29 import { NgbActiveModal, NgbTypeahead } from "@ng-bootstrap/ng-bootstrap";
30 import { RestApiService } from "src/app/core/services/rest-api.service";
31 import { AdminService } from "src/app/core/services/admin.service";
32 import { Topic } from "src/app/core/models/topic.model";
33 import {
34   debounceTime,
35   distinctUntilChanged,
36   filter,
37   map,
38   mergeMap
39 } from "rxjs/operators";
40 import { from, Subject, Observable, merge } from "rxjs";
41 import { Kafka } from "src/app/core/models/kafka.model";
42 import { Db } from "src/app/core/models/db.model";
43
44 @Component({
45   selector: "app-topic-modal",
46   templateUrl: "./topic-modal.component.html",
47   styleUrls: ["./topic-modal.component.css"]
48 })
49 export class TopicModalComponent implements OnInit {
50   @Input() data: Topic;
51   @Input() mode: string;
52   @Input() selectedIndex: number;
53
54   dataFormats: Array<string> = ["JSON", "XML"];
55   idExFields: Array<any> = [];
56   idExNewField: any = {};
57
58   kafkas: Array<Kafka> = [];
59   dbs: Array<Db> = [];
60   dbTypeIds: Array<string> = [];
61
62   // Autocomplete input
63   @ViewChild("instance") instance: NgbTypeahead;
64   focus$ = new Subject<string>();
65   click$ = new Subject<string>();
66   newTopicList: Array<string>;
67
68   search = (text$: Observable<string>) => {
69     const debouncedText$ = text$.pipe(
70       debounceTime(200),
71       distinctUntilChanged()
72     );
73     const clicksWithClosedPopup$ = this.click$.pipe(
74       filter(() => !this.instance.isPopupOpen())
75     );
76     const inputFocus$ = this.focus$;
77
78     return merge(debouncedText$, inputFocus$, clicksWithClosedPopup$).pipe(
79       map(term =>
80         (term === ""
81           ? this.newTopicList
82           : this.newTopicList.filter(
83               v => v.toLowerCase().indexOf(term.toLowerCase()) > -1
84             )
85         ).slice(0, 10)
86       )
87     );
88   };
89
90   constructor(
91     public activeModal: NgbActiveModal,
92     public adminService: AdminService,
93     private restApiService: RestApiService
94   ) {}
95
96   ngOnInit() {
97     // Get ID extration field
98     this.idExFields = [];
99     if (this.data.messageIdPath != null) {
100       let feed = this.data.messageIdPath.split(",");
101       for (let i = 0; i < feed.length; i++) {
102         let data = { item: feed[i] };
103         this.idExFields.push(data);
104       }
105     } else {
106       this.idExFields.push([]);
107     }
108
109     // Init data
110     this.initData();
111   }
112
113   initData() {
114     this.getKafkas();
115     this.getDbs();
116
117     if (this.mode === "new") {
118       this.getNewTopicList();
119     }
120   }
121
122   getKafkas() {
123     const get_kafkas = this.restApiService.getAllKafka().pipe(
124       mergeMap(ks => from(ks)),
125       map(k => {
126         if (
127           this.data.kafkas &&
128           this.data.kafkas.toString().includes(k.id.toString())
129         ) {
130           k.checkedToSave = true;
131         } else {
132           k.checkedToSave = false;
133         }
134         this.kafkas.push(k);
135       })
136     );
137
138     get_kafkas.subscribe();
139   }
140
141   getDbs() {
142     const get_dbs = this.restApiService.getAllDbs().pipe(
143       mergeMap(dbs => from(dbs)),
144       map(db => {
145         if (!this.dbTypeIds.includes(db.dbTypeId)) {
146           this.dbTypeIds.push(db.dbTypeId);
147         }
148         if (
149           this.data.sinkdbs &&
150           this.data.sinkdbs.toString().includes(db.id.toString())
151         ) {
152           db.checkedToSave = true;
153         } else {
154           db.checkedToSave = false;
155         }
156         this.dbs.push(db);
157       })
158     );
159
160     get_dbs.subscribe();
161   }
162
163   getNewTopicList() {
164     const get_topicName = this.restApiService.getTopicNames().pipe(
165       map(names => {
166         this.newTopicList = names;
167       })
168     );
169
170     get_topicName.subscribe();
171   }
172
173   onChabgeSelKafka(checked: boolean, id: string | number) {
174     // Array initialize
175     if (!this.data.kafkas) this.data.kafkas = [];
176
177     if (checked) {
178       // Add kafka_id into topic.kafkas
179       if (
180         this.data.kafkas &&
181         !this.data.kafkas.toString().includes(id.toString())
182       ) {
183         this.data.kafkas.push(id.toString());
184       }
185     } else {
186       // Remove kafka_id from topic.kafkas
187       if (
188         this.data.kafkas &&
189         this.data.kafkas.toString().includes(id.toString())
190       ) {
191         this.data.kafkas.forEach((k_id, index) => {
192           if (k_id.toString() === id.toString()) {
193             this.data.kafkas.splice(index, 1);
194             return;
195           }
196         });
197       }
198     }
199   }
200
201   onChabgeSelDb(checked: boolean, id: string | number) {
202     // Array initialize
203     if (!this.data.sinkdbs) this.data.sinkdbs = [];
204
205     if (checked) {
206       // Add db_id into topic.sinkdbs
207       if (
208         this.data.sinkdbs &&
209         !this.data.sinkdbs.toString().includes(id.toString())
210       ) {
211         this.data.sinkdbs.push(id.toString());
212       }
213     } else {
214       // Remove db_id from "topic.sinkdbs"
215       if (
216         this.data.sinkdbs &&
217         this.data.sinkdbs.toString().includes(id.toString())
218       ) {
219         this.data.sinkdbs.forEach((db_id, index) => {
220           if (db_id.toString() === id.toString()) {
221             this.data.sinkdbs.splice(index, 1);
222             return;
223           }
224         });
225       }
226     }
227   }
228
229   onClickAddIdField() {
230     this.idExFields.push(this.idExNewField);
231     this.idExNewField = {};
232   }
233
234   onClickDelIdField(index: number) {
235     if (this.idExFields.length > 1) {
236       this.idExFields.splice(index, 1);
237     }
238   }
239
240   onChangeSaveIdField() {
241     this.data.messageIdPath = "";
242     for (let i = 0; i < this.idExFields.length; i++) {
243       if (i == 0) {
244         this.data.messageIdPath = this.idExFields[i].item;
245       } else {
246         this.data.messageIdPath += "," + this.idExFields[i].item;
247       }
248     }
249   }
250
251   onClickMatTab(index: number) {
252     this.selectedIndex = index;
253   }
254
255   isAddingMode() {
256     let flag: boolean = false;
257
258     if (this.mode === "new") flag = true;
259
260     return flag;
261   }
262 }