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