1/**
2 * Controller for sensors-overview
3 *
4 * @module app/serverHealth
5 * @exports sensorsOverviewController
6 * @name sensorsOverviewController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11  angular.module('app.overview').controller('sensorsOverviewController', [
12    '$scope', '$log', '$window', 'APIUtils', 'dataService', 'Constants',
13    function($scope, $log, $window, APIUtils, dataService, Constants) {
14      $scope.dataService = dataService;
15
16      $scope.dropdown_selected = false;
17
18      $scope.$log = $log;
19      $scope.customSearch = '';
20      $scope.searchTerms = [];
21      $scope.messages = Constants.MESSAGES.SENSOR;
22      $scope.selectedSeverity =
23          {all: true, normal: false, warning: false, critical: false};
24      $scope.export_name = 'sensors.json';
25      $scope.loading = false;
26      $scope.jsonData = function(data) {
27        var dt = {};
28        data.data.forEach(function(item) {
29          dt[item.original_data.key] = item.original_data.value;
30        });
31        return JSON.stringify(dt);
32      };
33
34      $scope.clear = function() {
35        $scope.customSearch = '';
36        $scope.searchTerms = [];
37      };
38
39      $scope.doSearchOnEnter = function(event) {
40        var search =
41            $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
42        if (event.keyCode === 13 && search.length >= 2) {
43          $scope.searchTerms = $scope.customSearch.split(' ');
44        } else {
45          if (search.length == 0) {
46            $scope.searchTerms = [];
47          }
48        }
49      };
50
51      $scope.doSearchOnClick = function() {
52        var search =
53            $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
54        if (search.length >= 2) {
55          $scope.searchTerms = $scope.customSearch.split(' ');
56        } else {
57          if (search.length == 0) {
58            $scope.searchTerms = [];
59          }
60        }
61      };
62
63      $scope.toggleSeverityAll = function() {
64        $scope.selectedSeverity.all = !$scope.selectedSeverity.all;
65
66        if ($scope.selectedSeverity.all) {
67          $scope.selectedSeverity.normal = false;
68          $scope.selectedSeverity.warning = false;
69          $scope.selectedSeverity.critical = false;
70        }
71      };
72
73      $scope.toggleSeverity = function(severity) {
74        $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
75
76        if (['normal', 'warning', 'critical'].indexOf(severity) > -1) {
77          if ($scope.selectedSeverity[severity] == false &&
78              (!$scope.selectedSeverity.normal &&
79               !$scope.selectedSeverity.warning &&
80               !$scope.selectedSeverity.critical)) {
81            $scope.selectedSeverity.all = true;
82            return;
83          }
84        }
85
86        if ($scope.selectedSeverity.normal && $scope.selectedSeverity.warning &&
87            $scope.selectedSeverity.critical) {
88          $scope.selectedSeverity.all = true;
89          $scope.selectedSeverity.normal = false;
90          $scope.selectedSeverity.warning = false;
91          $scope.selectedSeverity.critical = false;
92        } else {
93          $scope.selectedSeverity.all = false;
94        }
95      };
96
97      $scope.filterBySeverity = function(sensor) {
98        if ($scope.selectedSeverity.all) return true;
99
100        return (
101            (sensor.severity_flags.normal && $scope.selectedSeverity.normal) ||
102            (sensor.severity_flags.warning &&
103             $scope.selectedSeverity.warning) ||
104            (sensor.severity_flags.critical &&
105             $scope.selectedSeverity.critical));
106      };
107      $scope.filterBySearchTerms = function(sensor) {
108        if (!$scope.searchTerms.length) return true;
109
110        for (var i = 0, length = $scope.searchTerms.length; i < length; i++) {
111          if (sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) ==
112              -1)
113            return false;
114        }
115        return true;
116      };
117
118      $scope.loadSensorData = function() {
119        $scope.loading = true;
120        APIUtils.getAllSensorStatus(function(data, originalData) {
121          $scope.data = data;
122          $scope.originalData = originalData;
123          $scope.export_data = JSON.stringify(originalData);
124          $scope.loading = false;
125        });
126      };
127
128      $scope.loadSensorData();
129    }
130  ]);
131})(angular);
132