1/**
2 * Controller for sensors-overview
3 *
4 * @module app/serverHealth
5 * @exports sensorsOverviewController
6 * @name sensorsOverviewController
7 */
8
9window.angular && (function (angular) {
10    'use strict';
11    angular
12        .module('app.overview')
13        .controller('sensorsOverviewController', [
14            '$scope',
15            '$log',
16            '$window',
17            'APIUtils',
18            'dataService',
19            'Constants',
20            function($scope, $log, $window, APIUtils, dataService, Constants){
21                $scope.dataService = dataService;
22
23                $scope.dropdown_selected = false;
24
25                $scope.$log = $log;
26                $scope.customSearch = "";
27                $scope.searchTerms = [];
28                $scope.messages = Constants.MESSAGES.SENSOR;
29                $scope.selectedSeverity = {
30                    all: true,
31                    normal: false,
32                    warning: false,
33                    critical: false
34                };
35                $scope.export_name = "sensors.json";
36                $scope.loading = false;
37                $scope.jsonData = function(data){
38                    var dt = {};
39                    data.data.forEach(function(item){
40                        dt[item.original_data.key] = item.original_data.value;
41                    });
42                    return JSON.stringify(dt);
43                };
44
45                $scope.clear = function(){
46                    $scope.customSearch = "";
47                    $scope.searchTerms = [];
48                }
49
50                $scope.doSearchOnEnter = function (event) {
51                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
52                    if (event.keyCode === 13 &&
53                        search.length >= 2) {
54                        $scope.searchTerms = $scope.customSearch.split(" ");
55                    }else{
56                        if(search.length == 0){
57                            $scope.searchTerms = [];
58                        }
59                    }
60                };
61
62                $scope.doSearchOnClick = function() {
63                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
64                    if (search.length >= 2) {
65                        $scope.searchTerms = $scope.customSearch.split(" ");
66                    }else{
67                        if(search.length == 0){
68                            $scope.searchTerms = [];
69                        }
70                    }
71                }
72
73                $scope.toggleSeverityAll = function(){
74                    $scope.selectedSeverity.all = !$scope.selectedSeverity.all;
75
76                    if($scope.selectedSeverity.all){
77                        $scope.selectedSeverity.normal = false;
78                        $scope.selectedSeverity.warning = false;
79                        $scope.selectedSeverity.critical = false;
80                    }
81                }
82
83                $scope.toggleSeverity = function(severity){
84                    $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
85
86                   if(['normal', 'warning', 'critical'].indexOf(severity) > -1){
87                       if($scope.selectedSeverity[severity] == false &&
88                          (!$scope.selectedSeverity.normal &&
89                           !$scope.selectedSeverity.warning &&
90                           !$scope.selectedSeverity.critical
91                          )){
92                           $scope.selectedSeverity.all = true;
93                           return;
94                       }
95                   }
96
97                    if($scope.selectedSeverity.normal &&
98                       $scope.selectedSeverity.warning &&
99                       $scope.selectedSeverity.critical){
100                        $scope.selectedSeverity.all = true;
101                        $scope.selectedSeverity.normal = false;
102                        $scope.selectedSeverity.warning = false;
103                        $scope.selectedSeverity.critical = false;
104                    }else{
105                        $scope.selectedSeverity.all = false;
106                    }
107                }
108
109                $scope.filterBySeverity = function(sensor){
110                    if($scope.selectedSeverity.all) return true;
111
112                    return( (sensor.severity_flags.normal && $scope.selectedSeverity.normal) ||
113                            (sensor.severity_flags.warning && $scope.selectedSeverity.warning) ||
114                            (sensor.severity_flags.critical && $scope.selectedSeverity.critical)
115                    );
116                }
117                $scope.filterBySearchTerms = function(sensor){
118
119                    if(!$scope.searchTerms.length) return true;
120
121                    for(var i = 0, length = $scope.searchTerms.length; i < length; i++){
122                        if(sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
123                    }
124                    return true;
125                }
126
127                $scope.loadSensorData = function(){
128                    $scope.loading = true;
129                    APIUtils.getAllSensorStatus(function(data, originalData){
130                        $scope.data = data;
131                        $scope.originalData = originalData;
132                        dataService.sensorData = data;
133                        $scope.export_data = JSON.stringify(originalData);
134                        $scope.loading = false;
135                    });
136                };
137
138                $scope.loadSensorData();
139            }
140        ]
141    );
142
143})(angular);