Angular upgrade - Dynamic widget,widget catalog
[portal.git] / portal-FE-common / src / app / pages / web-analytics / web-analytics.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 import { Component, OnInit, ViewChild, Input} from '@angular/core';
39 import { WebAnalyticsService } from '../../shared/services/index';
40 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
41 import { MatTableDataSource } from '@angular/material';
42 import { MatSort, MatPaginator } from '@angular/material';
43 import { WebAnalyticsDetailsDialogComponent } from './web-analytics-details-dialog/web-analytics-details-dialog.component';
44 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
45 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
46
47 @Component({
48   selector: 'app-web-analytics',
49   templateUrl: './web-analytics.component.html',
50   styleUrls: ['./web-analytics.component.scss']
51 })
52 export class WebAnalyticsComponent implements OnInit {
53
54   application: any;
55   isAppSelectDisabled: boolean;
56   selectApp: any;
57   sortedApps: any = [];
58   userAppReports: any = [];
59   userTableAppReports: any= [];
60   userJourneyAnalytics: any = [];
61   userApps: any = [];
62   result: any;
63   isEditMode: boolean = false;
64
65   displayedColumns: string[] = ['applicationName', 'reportName', 'reportURL','delete'];
66   dataSource = new MatTableDataSource(this.userTableAppReports);
67   @ViewChild(MatSort) sort: MatSort;
68   @ViewChild(MatPaginator) paginator: MatPaginator;
69
70
71   constructor(public webAnalyticsService: WebAnalyticsService, public ngbModal: NgbModal) { }
72
73   ngOnInit() {
74
75     this.application = null;    
76     this.isAppSelectDisabled = false;
77     this.selectApp = 'All Applications';
78     this.sortedApps = [{
79         index: 0,
80         appName: 'All Applications',
81         value: 'All Applications'
82     }];
83
84     this.getAllWebAnalyticsReport();
85     //this.getUserApps();
86
87   }
88
89   getAllWebAnalyticsReport(){
90     //console.log("getAllWebAnalyticsReport called");
91     this.webAnalyticsService.getAllWebAnalyticsReport()
92     .subscribe(_data => {
93         this.result = _data;
94         if (this.result == null || this.result == 'undefined') {
95              console.log('WebAnalyticsService::getAllWebAnalyticsReport Failed: Result or result.data is null');
96         }else {
97           //console.log('WebAnalyticsService::getAllWebAnalyticsReport', this.result);
98           for (let i = 0; i < this.result.length; i++) {
99             var userTableAppReport = {
100                 reportName: this.result[i].reportName,
101                 reportSrc: this.result[i].reportSrc,
102                 appName: this.result[i].appName,
103                 appId : this.result[i].appId,
104                 resourceId : this.result[i].resourceId
105             };
106             this.userTableAppReports.push(userTableAppReport);
107           }
108           this.populateTableData(this.userTableAppReports);
109         }
110     }, error =>{
111       console.log(error);
112     });
113   }
114
115   deleteWebAnalyticsReport(deleteObj: any){
116     let confirmationMsg = 'You are about to delete this Web Analytics : ' + deleteObj.reportName+ '. Click OK to continue.';
117     this.openInformationModal("Confirmation",confirmationMsg).result.then((result) => {
118       if (result === 'Ok') {
119         this.userTableAppReports.splice(this.userTableAppReports.indexOf(deleteObj), 1);
120         this.webAnalyticsService.deleteWebAnalyticsReport(deleteObj)
121         .subscribe(_data => {
122             this.userTableAppReports = [];
123             this.getAllWebAnalyticsReport();
124         }, error =>{
125           console.log(error);
126         });
127       }
128     }, (resut) => {
129       return;
130     })
131   }
132
133   getUserApps(){
134     //console.log("getUserApps called");
135     this.webAnalyticsService.getWebAnalyticsAppReports()
136     .subscribe(_data => {
137         this.result = _data;
138         if (this.result == null || this.result == 'undefined') {
139              console.log('WebAnalyticsService::getServiceList Failed: Result or result.data is null');
140          }else {
141           for (let i = 0; i < this.result.length; i++) {
142             var userAppReport = {
143                 sizeX: 3,
144                 sizeY: 2,
145                 reportName: this.result[i].reportName,
146                 reportSrc: this.result[i].reportSrc,
147                 appName: this.result[i].appName,
148             };
149             
150             if(this.result[i].reportSrc.includes("appName")){
151                 let appName = this.result[i].reportSrc.split("appName=").splice(-1)[0];
152                 this.webAnalyticsService.getUserJourneyList(appName)
153                 .subscribe(_data => {
154                   let userJourneyReports = {
155                     sizeX: 6,
156                     sizeY: 3,
157                       title: appName+" User journey",
158                       analyticsList: this.result
159                   };  
160                   this.userJourneyAnalytics.push(userJourneyReports);
161                 }, error =>{
162                   console.log(error);
163                 });
164
165             }                               
166             this.userAppReports.push(userAppReport);
167         }
168
169         for (var i = 0; i < this.result.length; i++) {
170             var userApp = {
171                 appName: this.result[i].appName,
172             };
173
174             this.userApps.push(userApp);
175         }
176         /*angular.forEach($scope.userApps, function(value, key) {
177             var index = $scope.uniqueUserApps.indexOf(value.appName);
178
179             if (index === -1) {
180                 $scope.uniqueUserApps.push(value.appName);
181             }
182         });
183
184
185         for (let i = 1; i <= $scope.uniqueUserApps.length; i++) {
186             this.sortedApps.push({
187                 index: i,
188                 appName: $scope.uniqueUserApps[i - 1],
189                 value: $scope.uniqueUserApps[i - 1]
190             });
191         }*/
192       }
193     }, error =>{
194       //console.log(error);
195     });
196   }
197
198   openWebAnalyticsModal(rowData: any) {
199     const modalRef = this.ngbModal.open(WebAnalyticsDetailsDialogComponent, { size: 'lg' });
200     //console.log("selectedData in parent",rowData);
201     if(rowData != 'undefined' && rowData){
202       modalRef.componentInstance.userTableAppReport = rowData;
203       this.isEditMode = true;
204     }else{
205       modalRef.componentInstance.userTableAppReport  = {};
206       this.isEditMode = false;
207     }
208     modalRef.componentInstance.passEntry.subscribe((receivedEntry: any) => {
209       if(receivedEntry.httpStatusCode && receivedEntry.httpStatusCode ==='200' ||
210       receivedEntry.status && receivedEntry.status === 'OK'){
211         this.userTableAppReports = [];
212         this.getAllWebAnalyticsReport();
213       } 
214     });
215   }
216
217
218   populateTableData(userTableAppReports: Array<Object>){
219     this.dataSource = new MatTableDataSource(userTableAppReports);
220     this.dataSource.sort = this.sort;
221     this.dataSource.paginator = this.paginator;
222   }
223
224   applyFilter(filterValue: string) {
225     this.dataSource.filter = filterValue.trim().toLowerCase();
226   }
227
228   openConfirmationModal(_title: string, _message: string) {
229     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
230     modalInfoRef.componentInstance.title = _title;
231     modalInfoRef.componentInstance.message = _message;
232   }
233
234   openInformationModal(_title: string, _message: string){
235     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
236     modalInfoRef.componentInstance.title = _title;
237     modalInfoRef.componentInstance.message = _message;
238     return modalInfoRef;
239   }
240 }