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.normal = false;
79                        $scope.selectedSeverity.warning = false;
80                        $scope.selectedSeverity.critical = false;
81                    }
82                }
83
84                $scope.toggleSeverity = function(severity){
85                    $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
86
87                   if(['normal', 'warning', 'critical'].indexOf(severity) > -1){
88                       if($scope.selectedSeverity[severity] == false &&
89                          (!$scope.selectedSeverity.normal &&
90                           !$scope.selectedSeverity.warning &&
91                           !$scope.selectedSeverity.critical
92                          )){
93                           $scope.selectedSeverity.all = true;
94                           return;
95                       }
96                   }
97
98                    if($scope.selectedSeverity.normal &&
99                       $scope.selectedSeverity.warning &&
100                       $scope.selectedSeverity.critical){
101                        $scope.selectedSeverity.all = true;
102                        $scope.selectedSeverity.normal = false;
103                        $scope.selectedSeverity.warning = false;
104                        $scope.selectedSeverity.critical = false;
105                    }else{
106                        $scope.selectedSeverity.all = false;
107                    }
108                }
109
110                $scope.filterBySeverity = function(sensor){
111                    if($scope.selectedSeverity.all) return true;
112
113                    return( (sensor.severity_flags.normal && $scope.selectedSeverity.normal) ||
114                            (sensor.severity_flags.warning && $scope.selectedSeverity.warning) ||
115                            (sensor.severity_flags.critical && $scope.selectedSeverity.critical)
116                    );
117                }
118                $scope.filterBySearchTerms = function(sensor){
119
120                    if(!$scope.searchTerms.length) return true;
121
122                    for(var i = 0, length = $scope.searchTerms.length; i < length; i++){
123                        if(sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
124                    }
125                    return true;
126                }
127
128                $scope.loadSensorData = function(){
129                    $scope.loading = true;
130                    APIUtils.getAllSensorStatus(function(data, originalData){
131                        $scope.data = data;
132                        $scope.originalData = originalData;
133                        dataService.sensorData = data;
134                        $scope.export_data = JSON.stringify(originalData);
135                        $scope.loading = false;
136                    });
137                };
138
139                $scope.loadSensorData();
140            }
141        ]
142    );
143
144})(angular);