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