Create Test Reporting landing page
[releng.git] / utils / test / reporting / pages / app / scripts / controllers / table.controller.js
1 'use strict';
2
3 /**
4  * @ngdoc function
5  * @name opnfvdashBoardAngularApp.controller:TableController
6  * @description
7  * # TableController
8  * Controller of the opnfvdashBoardAngularApp
9  */
10 angular.module('opnfvApp')
11     .controller('TableController', ['$scope', '$state', '$stateParams', '$http', 'TableFactory', function($scope, $state, $stateParams, $http, TableFactory) {
12
13         $scope.filterlist = [];
14         $scope.selection = [];
15         $scope.statusList = [];
16         $scope.projectList = [];
17         $scope.installerList = [];
18         $scope.versionlist = [];
19         $scope.loopci = [];
20         $scope.time = [];
21         $scope.tableDataAll = {};
22         $scope.tableInfoAll = {};
23         $scope.scenario = {};
24
25         $scope.VersionConfig = {
26             create: true,
27             valueField: 'title',
28             labelField: 'title',
29             delimiter: '|',
30             maxItems: 1,
31             placeholder: 'Version',
32             onChange: function(value) {
33                 checkElementArrayValue($scope.selection, $scope.VersionOption);
34                 $scope.selection.push(value);
35                 // console.log($scope.selection);
36                 getScenarioData();
37
38             }
39         }
40
41         $scope.LoopConfig = {
42             create: true,
43             valueField: 'title',
44             labelField: 'title',
45             delimiter: '|',
46             maxItems: 1,
47             placeholder: 'Loop',
48             onChange: function(value) {
49                 checkElementArrayValue($scope.selection, $scope.LoopOption);
50                 $scope.selection.push(value);
51                 // console.log($scope.selection);
52                 getScenarioData();
53
54             }
55         }
56
57         $scope.TimeConfig = {
58             create: true,
59             valueField: 'title',
60             labelField: 'title',
61             delimiter: '|',
62             maxItems: 1,
63             placeholder: 'Time',
64             onChange: function(value) {
65                 checkElementArrayValue($scope.selection, $scope.TimeOption);
66                 $scope.selection.push(value);
67                 // console.log($scope.selection)
68                 getScenarioData();
69
70
71             }
72         }
73
74
75         init();
76
77         function init() {
78             $scope.toggleSelection = toggleSelection;
79             getScenarioData();
80             // radioSetting();
81             getFilters();
82         }
83
84         function getFilters() {
85             TableFactory.getFilter().get({
86
87
88             }).$promise.then(function(response) {
89                 if (response != null) {
90                     $scope.statusList = response.filters.status;
91                     $scope.projectList = response.filters.projects;
92                     $scope.installerList = response.filters.installers;
93                     $scope.versionlist = response.filters.version;
94                     $scope.loopci = response.filters.loops;
95                     $scope.time = response.filters.time;
96
97                     $scope.statusListString = $scope.statusList.toString();
98                     $scope.projectListString = $scope.projectList.toString();
99                     $scope.installerListString = $scope.installerList.toString();
100                     $scope.VersionSelected = $scope.versionlist[1];
101                     $scope.LoopCiSelected = $scope.loopci[0];
102                     $scope.TimeSelected = $scope.time[0];
103                     radioSetting($scope.versionlist, $scope.loopci, $scope.time);
104
105                 } else {
106                     alert("网络错误");
107                 }
108             })
109         }
110
111         function getScenarioData() {
112
113             var utl = BASE_URL + '/scenarios';
114             var data = {
115                 'status': ['success', 'danger', 'warning'],
116                 'projects': ['functest', 'yardstick'],
117                 'installers': ['apex', 'compass', 'fuel', 'joid'],
118                 'version': $scope.VersionSelected,
119                 'loops': $scope.LoopCiSelected,
120                 'time': $scope.TimeSelected
121             };
122             var config = {
123                 headers: {
124                     'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
125                 }
126             }
127             $http.post(utl, data, config).then(function(response) {
128                 if (response.status == 200) {
129                     $scope.scenario = response.data;
130                     constructJson();
131                 }
132             })
133         }
134
135         //construct json 
136         function constructJson() {
137
138             var colspan;
139             var InstallerData;
140             var projectsInfo;
141             $scope.tableDataAll["scenario"] = [];
142
143
144             for (var item in $scope.scenario.scenarios) {
145
146                 var headData = Object.keys($scope.scenario.scenarios[item].installers).sort();
147                 var scenarioStatus = $scope.scenario.scenarios[item].status;
148                 var scenarioStatusDisplay;
149                 if (scenarioStatus == "success") {
150                     scenarioStatusDisplay = "navy";
151                 } else if (scenarioStatus == "danger") {
152                     scenarioStatusDisplay = "danger";
153                 } else if (scenarioStatus == "warning") {
154                     scenarioStatusDisplay = "warning";
155                 }
156
157                 InstallerData = headData;
158                 var projectData = [];
159                 var datadisplay = [];
160                 var projects = [];
161
162                 for (var j = 0; j < headData.length; j++) {
163
164                     projectData.push($scope.scenario.scenarios[item].installers[headData[j]]);
165                 }
166                 for (var j = 0; j < projectData.length; j++) {
167
168                     for (var k = 0; k < projectData[j].length; k++) {
169                         projects.push(projectData[j][k].project);
170                         var temArray = [];
171                         if (projectData[j][k].score == null) {
172                             temArray.push("null");
173                             temArray.push(projectData[j][k].project);
174                             temArray.push(headData[j]);
175                         } else {
176                             temArray.push(projectData[j][k].score);
177                             temArray.push(projectData[j][k].project);
178                             temArray.push(headData[j]);
179                         }
180
181
182                         if (projectData[j][k].status == "platinium") {
183                             temArray.push("primary");
184                             temArray.push("P");
185                         } else if (projectData[j][k].status == "gold") {
186                             temArray.push("danger");
187                             temArray.push("G");
188                         } else if (projectData[j][k].status == "silver") {
189                             temArray.push("warning");
190                             temArray.push("S");
191                         } else if (projectData[j][k].status == null) {
192                             temArray.push("null");
193                         }
194
195                         datadisplay.push(temArray);
196
197                     }
198
199                 }
200
201                 colspan = projects.length / headData.length;
202
203                 var tabledata = {
204                     scenarioName: item,
205                     Installer: InstallerData,
206                     projectData: projectData,
207                     projects: projects,
208                     datadisplay: datadisplay,
209                     colspan: colspan,
210                     status: scenarioStatus,
211                     statusDisplay: scenarioStatusDisplay
212                 };
213
214                 JSON.stringify(tabledata);
215                 $scope.tableDataAll.scenario.push(tabledata);
216
217                 // console.log(tabledata);
218
219             }
220
221
222             projectsInfo = $scope.tableDataAll.scenario[0].projects;
223
224             var tempHeadData = [];
225
226             for (var i = 0; i < InstallerData.length; i++) {
227                 for (var j = 0; j < colspan; j++) {
228                     tempHeadData.push(InstallerData[i]);
229                 }
230             }
231
232             //console.log(tempHeadData);
233
234             var projectsInfoAll = [];
235
236             for (var i = 0; i < projectsInfo.length; i++) {
237                 var tempA = [];
238                 tempA.push(projectsInfo[i]);
239                 tempA.push(tempHeadData[i]);
240                 projectsInfoAll.push(tempA);
241
242             }
243             //console.log(projectsInfoAll);
244
245             $scope.tableDataAll["colspan"] = colspan;
246             $scope.tableDataAll["Installer"] = InstallerData;
247             $scope.tableDataAll["Projects"] = projectsInfoAll;
248
249             // console.log($scope.tableDataAll);
250             $scope.colspan = $scope.tableDataAll.colspan;
251
252         }
253
254         //get json element size
255         function getSize(jsondata) {
256             var size = 0;
257             for (var item in jsondata) {
258                 size++;
259             }
260             return size;
261         }
262
263         $scope.colspan = $scope.tableDataAll.colspan;
264         // console.log($scope.colspan);
265
266
267         //find all same element index 
268         function getSameElementIndex(array, element) {
269             var indices = [];
270             var idx = array.indexOf(element);
271             while (idx != -1) {
272                 indices.push(idx);
273                 idx = array.indexOf(element, idx + 1);
274             }
275             //return indices;
276             var result = { element: element, index: indices };
277             JSON.stringify(result);
278             return result;
279         }
280
281         //delete element in array
282         function deletElement(array, index) {
283             array.splice(index, 1);
284
285         }
286
287         function radioSetting(array1, array2, array3) {
288             var tempVersion = [];
289             var tempLoop = [];
290             var tempTime = [];
291             for (var i = 0; i < array1.length; i++) {
292                 var temp = {
293                     title: array1[i]
294                 };
295                 tempVersion.push(temp);
296             }
297             for (var i = 0; i < array2.length; i++) {
298                 var temp = {
299                     title: array2[i]
300                 };
301                 tempLoop.push(temp);
302             }
303             for (var i = 0; i < array3.length; i++) {
304                 var temp = {
305                     title: array3[i]
306                 };
307                 tempTime.push(temp);
308             }
309             $scope.VersionOption = tempVersion;
310             $scope.LoopOption = tempLoop;
311             $scope.TimeOption = tempTime;
312         }
313
314         //remove element in the array
315         function removeArrayValue(arr, value) {
316             for (var i = 0; i < arr.length; i++) {
317                 if (arr[i] == value) {
318                     arr.splice(i, 1);
319                     break;
320                 }
321             }
322         }
323
324         //check if exist element
325         function checkElementArrayValue(arrayA, arrayB) {
326             for (var i = 0; i < arrayB.length; i++) {
327                 if (arrayA.indexOf(arrayB[i].title) > -1) {
328                     removeArrayValue(arrayA, arrayB[i].title);
329                 }
330             }
331         }
332
333         function toggleSelection(status) {
334             var idx = $scope.selection.indexOf(status);
335
336             if (idx > -1) {
337                 $scope.selection.splice(idx, 1);
338                 filterData($scope.selection)
339             } else {
340                 $scope.selection.push(status);
341                 filterData($scope.selection)
342             }
343             // console.log($scope.selection);
344
345         }
346
347         //filter function
348         function filterData(selection) {
349
350             $scope.selectInstallers = [];
351             $scope.selectProjects = [];
352             $scope.selectStatus = [];
353             for (var i = 0; i < selection.length; i++) {
354                 if ($scope.statusListString.indexOf(selection[i]) > -1) {
355                     $scope.selectStatus.push(selection[i]);
356                 }
357                 if ($scope.projectListString.indexOf(selection[i]) > -1) {
358                     $scope.selectProjects.push(selection[i]);
359                 }
360                 if ($scope.installerListString.indexOf(selection[i]) > -1) {
361                     $scope.selectInstallers.push(selection[i]);
362                 }
363             }
364
365             $scope.colspan = $scope.selectProjects.length;
366             //when some selection is empty, we set it full
367             if ($scope.selectInstallers.length == 0) {
368                 $scope.selectInstallers = $scope.installerList;
369
370             }
371             if ($scope.selectProjects.length == 0) {
372                 $scope.selectProjects = $scope.projectList;
373                 $scope.colspan = $scope.tableDataAll.colspan;
374             }
375             if ($scope.selectStatus.length == 0) {
376                 $scope.selectStatus = $scope.statusList
377             }
378
379             // console.log($scope.selectStatus);
380             // console.log($scope.selectProjects);
381
382         }
383
384
385     }]);