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.jsonData = function(data){
36                    var dt = {};
37                    data.data.forEach(function(item){
38                        dt[item.original_data.key] = item.original_data.value;
39                    });
40                    return JSON.stringify(dt);
41                };
42
43                $scope.doSearchOnEnter = function (event) {
44                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
45                    if (event.keyCode === 13 &&
46                        search.length >= 2) {
47                        $scope.searchTerms = $scope.customSearch.split(" ");
48                    }else{
49                        if(search.length == 0){
50                            $scope.searchTerms = [];
51                        }
52                    }
53                };
54
55                $scope.doSearchOnClick = function() {
56                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
57                    if (search.length >= 2) {
58                        $scope.searchTerms = $scope.customSearch.split(" ");
59                    }else{
60                        if(search.length == 0){
61                            $scope.searchTerms = [];
62                        }
63                    }
64                }
65
66                $scope.toggleSeverityAll = function(){
67
68                    if($scope.selectedSeverity.all !== true){
69                      $scope.selectedSeverity.all = !$scope.selectedSeverity.all;
70                    }
71
72                    if($scope.selectedSeverity.all){
73                        $scope.selectedSeverity.normal = false;
74                        $scope.selectedSeverity.warning = false;
75                        $scope.selectedSeverity.critical = false;
76                    }
77                }
78
79                $scope.toggleSeverity = function(severity){
80                    $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
81
82                    if($scope.selectedSeverity.normal &&
83                       $scope.selectedSeverity.warning &&
84                       $scope.selectedSeverity.critical){
85                        $scope.selectedSeverity.all = true;
86                        $scope.selectedSeverity.normal = false;
87                        $scope.selectedSeverity.warning = false;
88                        $scope.selectedSeverity.critical = false;
89                    }else{
90                        $scope.selectedSeverity.all = false;
91                    }
92                }
93
94                $scope.filterBySeverity = function(sensor){
95                    if($scope.selectedSeverity.all) return true;
96
97                    return( (sensor.severity_flags.normal && $scope.selectedSeverity.normal) ||
98                            (sensor.severity_flags.warning && $scope.selectedSeverity.warning) ||
99                            (sensor.severity_flags.critical && $scope.selectedSeverity.critical)
100                    );
101                }
102                $scope.filterBySearchTerms = function(sensor){
103
104                    if(!$scope.searchTerms.length) return true;
105
106                    for(var i = 0, length = $scope.searchTerms.length; i < length; i++){
107                        if(sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
108                    }
109                    return true;
110                }
111
112                $scope.loadSensorData = function(){
113                    APIUtils.getAllSensorStatus(function(data, originalData){
114                        $scope.data = data;
115                        $scope.originalData = originalData;
116                        dataService.sensorData = data;
117                        $scope.export_data = JSON.stringify(originalData);
118                    });
119                };
120
121                $scope.loadSensorData();
122            }
123        ]
124    );
125
126})(angular);