NonRT-RIC A1 Northbound API
[nonrtric.git] / sdnc-a1-controller / oam / SdncReports / SdncReportsApi / src / main / webapp / js / sdnc-controller / sdnc-viewreport-controller.js
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : SDNC-FEATURES
4 * ================================================================================
5 * Copyright 2018 TechMahindra
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 myApp.controller('ReportController', ['$scope', '$http', 'viewReportService', 'growl', function($scope, $http, viewReportService, growl) {
22     $scope.isDisabled = true;
23     $scope.pagination = false;
24     $scope.selectedDevice;
25     $scope.ShowResult = false;
26     $scope.dateValidation = false;
27     $scope.NoSearchResult = false;
28     $scope.showError = false;
29     $scope.errorMessage = "";
30     $scope.showSuccess = false;
31     $scope.successMessage = "";
32     $scope.showWarning = false;
33     $scope.warningMessage = "";
34     $scope.showExecutionDetails = true;
35
36     //THIS FUNCTION WILL BE CALLED WHEN USER CLICK SUBMIT FROM UI
37     $scope.getReports = function(deviceIP, startdate, enddate) { 
38
39         $scope.ShowResult = false;
40         if (new Date(startdate) > new Date(enddate)) {
41             $scope.dateValidation = true;
42             $scope.showError = true;
43             $scope.errorMessage = "Start date cannot be greated than End date";
44             growl.error($scope.errorMessage, {
45                 title: 'Error!'
46             });
47             return false;
48         }
49         var date = new Date(startdate);
50         if (angular.isDefined(deviceIP)) {
51             $scope.DeviceIP = deviceIP;
52         }
53         if (angular.isDefined(startdate)) {
54             $scope.startDate = startdate;
55         }
56         if (angular.isDefined(enddate)) {
57             $scope.endDate = enddate;
58         }
59
60         if (deviceIP != null && startdate != null && enddate != null) {
61
62             //service call to fetch the reports start date,end date,test name
63             viewReportService.getData($scope.startDate, $scope.endDate, $scope.DeviceIP).then(function(result) {
64                     console.log("--ReportController::getdata called from controler--", JSON.stringify(result.data));
65                     if (result.status == 200) {
66                         if (result.data != null && result.data.length >= 1) {
67                                 
68                             //in case of success, build the model object to store the service output here
69                             $scope.createTestReportModel(result.data);
70                         } else {
71                             $scope.ShowResult = false;
72                             $scope.showWarning = true;
73                             $scope.warningMessage = "No result found for specified Device name !!";
74                             growl.warning($scope.warningMessage, {
75                                 title: 'Warning!'
76                             });
77                         }
78                     } else {
79                         $scope.ShowResult = false;
80                         $scope.showWarning = true;
81                         $scope.warningMessage = "No result found for specified Device name !!";
82                         growl.warning($scope.warningMessage, {
83                             title: 'Warning!'
84                         });
85                     }
86                 },
87                 function(response) {
88                     console.log("--ReportController::getdata::Error--", response);
89                 });
90         }
91     }
92
93
94     //FUNCTION WILL BE CALLED WHEN USER CLICK DOWNLOAD FROM UI
95     $scope.exportToExcel = function(tableId) { // ex: '#my-table'
96         var exportHref = Excel.tableToExcel(tableId, 'export');
97         $timeout(function() {
98             location.href = exportHref;
99         }, 100); // trigger download
100
101         console.log("--ReportController::exportToexcel--");
102     }
103
104     $scope.createTestReportModel = function(result) {
105
106         $scope.showError = false;
107         $scope.showWarning = false;
108         $scope.objTestReportModel = result;
109         $scope.objTestModel = [];
110
111
112         if ($scope.objTestReportModel.length >= 1) {
113             for (var i = 0; i < $scope.objTestReportModel.length; i++) {
114                 var objTestReport = {};
115                 objTestReport.testid = $scope.objTestReportModel[i].testid;
116                 objTestReport.deviceid = $scope.objTestReportModel[i].deviceid;
117                 objTestReport.deviceIP = $scope.objTestReportModel[i].deviceIP;
118                 objTestReport.result = $scope.objTestReportModel[i].result;
119                 objTestReport.timeStamp = $scope.objTestReportModel[i].timeStamp;
120                 objTestReport.testname = $scope.objTestReportModel[i].testName;
121
122                 var executionDetails = {};
123                 $scope.tmp = angular.fromJson($scope.objTestReportModel[i].execuationDetails); 
124                 executionDetails = $scope.tmp.output;
125
126                 if ($scope.objTestReportModel[i].testName === "Network Layer") {
127                         
128                     //fetching the statistics to show in progress bar
129                     var statistics = executionDetails.statistics;
130                     objTestReport.status = executionDetails.status;
131                     objTestReport.statistics = executionDetails.statistics;
132                     statistics = statistics.split("%");
133                     executionDetails.statistics = statistics[0];
134                     if (executionDetails.statistics == 0) {
135                         executionDetails.statisticPer = parseInt(executionDetails.statistics) + 50;
136                     } else
137                         executionDetails.statisticPer = executionDetails.statistics;
138
139                     console.log("--ReportController::CreateTestReportModel--", executionDetails.statistics);
140
141                     //fetching the avg time to show in progress bar
142                     
143                     var avgTime = executionDetails.avgTime;
144                     objTestReport.avgTime = executionDetails.avgTime;
145                     avgTime = avgTime.split("=");
146                     var Testtime = avgTime[1];
147                     executionDetails.avgTime = Testtime.slice(0, -2).trim();
148                     console.log("--ReportController::CreateTestReportModel--", executionDetails.avgTime);
149                     if (executionDetails.avgTime < 50) {
150                         executionDetails.avgTimePer = parseInt(executionDetails.avgTime) + 10;
151                     } else
152                         executionDetails.avgTimePer = executionDetails.avgTime;
153
154                 }
155
156
157                 objTestReport.executionDetails = executionDetails;
158                 $scope.objTestModel.push(objTestReport);
159                 console.log("--ReportController::CreateTestReportModel--", JSON.stringify($scope.objTestModel));
160
161             }
162             $scope.ShowResult = true;
163             $scope.pagination = true;
164         }
165         console.log("--ReportController::createTestReportModel::final TestReportModel--" + JSON.stringify($scope.objTestModel));
166         $scope.csvOrder = ['testname', 'deviceIP', 'timeStamp', 'status', 'statistics', 'avgTime', 'result'];
167
168     }
169 }]);