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    var sensorData = [], originalData = {};
13    angular
14        .module('app.overview')
15        .controller('sensorsOverviewController', [
16            '$scope',
17            '$log',
18            '$window',
19            'APIUtils',
20            'dataService',
21            function($scope, $log, $window, APIUtils, dataService, userModel){
22                $scope.dataService = dataService;
23
24                $scope.dropdown_selected = false;
25
26                $scope.$log = $log;
27                $scope.customSearch = "";
28                $scope.searchTerms = [];
29                $scope.selectedSeverity = {
30                    all: true,
31                    normal: false,
32                    warning: false,
33                    critical: false
34                };
35                $scope.export_name = "sensors.json";
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.doSearchOnEnter = function (event) {
45                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
46                    if (event.keyCode === 13 &&
47                        search.length >= 2) {
48                        $scope.searchTerms = $scope.customSearch.split(" ");
49                    }else{
50                        if(search.length == 0){
51                            $scope.searchTerms = [];
52                        }
53                    }
54                };
55
56                $scope.doSearchOnClick = function() {
57                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
58                    if (search.length >= 2) {
59                        $scope.searchTerms = $scope.customSearch.split(" ");
60                    }else{
61                        if(search.length == 0){
62                            $scope.searchTerms = [];
63                        }
64                    }
65                }
66
67                $scope.toggleSeverityAll = function(){
68                    $scope.selectedSeverity.all = !$scope.selectedSeverity.all;
69
70                    if($scope.selectedSeverity.all){
71                        $scope.selectedSeverity.normal = false;
72                        $scope.selectedSeverity.warning = false;
73                        $scope.selectedSeverity.critical = false;
74                    }
75                }
76
77                $scope.toggleSeverity = function(severity){
78                    $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
79
80                    if($scope.selectedSeverity.normal &&
81                       $scope.selectedSeverity.warning &&
82                       $scope.selectedSeverity.critical){
83                        $scope.selectedSeverity.all = true;
84                        $scope.selectedSeverity.normal = false;
85                        $scope.selectedSeverity.warning = false;
86                        $scope.selectedSeverity.critical = false;
87                    }else{
88                        $scope.selectedSeverity.all = false;
89                    }
90                }
91
92                $scope.filterBySeverity = function(sensor){
93                    if($scope.selectedSeverity.all) return true;
94
95                    return( (sensor.severity_flags.normal && $scope.selectedSeverity.normal) ||
96                            (sensor.severity_flags.warning && $scope.selectedSeverity.warning) ||
97                            (sensor.severity_flags.critical && $scope.selectedSeverity.critical)
98                    );
99                }
100                $scope.filterBySearchTerms = function(sensor){
101
102                    if(!$scope.searchTerms.length) return true;
103
104                    for(var i = 0, length = $scope.searchTerms.length; i < length; i++){
105                        if(sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
106                    }
107                    return true;
108                }
109
110                $scope.loadSensorData = function(){
111                    APIUtils.getAllSensorStatus(function(data, originalData){
112                        sensorData = data;
113                        originalData = originalData;
114                        $scope.data = data;
115                        $scope.originalData = originalData;
116                        dataService.sensorData = sensorData;
117                        $scope.export_data = JSON.stringify(originalData);
118                    });
119                };
120
121                $scope.loadSensorData();
122            }
123        ]
124    );
125
126})(angular);