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