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