blob: c0ad76e44ab58aa032753f1d51ed77f2b6f985ce [file] [log] [blame]
Michael Davisaeedf9e2017-04-06 14:35:56 -05001/**
Iftekharul Islamcd789502017-04-19 14:37:55 -05002 * Controller for sensors
Michael Davisaeedf9e2017-04-06 14:35:56 -05003 *
Iftekharul Islamcd789502017-04-19 14:37:55 -05004 * @module app/serverHealth
5 * @exports sensorsController
6 * @name sensorsController
Michael Davisaeedf9e2017-04-06 14:35:56 -05007 */
8
9window.angular && (function (angular) {
10 'use strict';
Iftekharul Islamc1535922017-06-19 12:49:04 -050011 var sensorData = [];
Michael Davisaeedf9e2017-04-06 14:35:56 -050012 angular
13 .module('app.overview')
14 .controller('sensorsController', [
15 '$scope',
16 '$log',
Gunnar Millseedefd32018-02-28 17:02:34 -060017 '$window',
18 'APIUtils',
Michael Davisaeedf9e2017-04-06 14:35:56 -050019 'dataService',
Iftekharul Islamd2269e22017-05-02 09:32:45 -050020 '$routeParams',
21 function($scope, $log, $window, APIUtils, dataService, $routeParams){
Michael Davisaeedf9e2017-04-06 14:35:56 -050022 $scope.dataService = dataService;
Iftekharul Islamd2269e22017-05-02 09:32:45 -050023 $scope.customSearch = "";
Michael Davisaeedf9e2017-04-06 14:35:56 -050024 $scope.dropdown_selected = false;
Iftekharul Islamcd789502017-04-19 14:37:55 -050025 $scope.$log = $log;
Iftekharul Islamd2269e22017-05-02 09:32:45 -050026 $scope.data = {};
27 $scope.searchTerms = [];
28
29 $scope.selectedSeverity = {
30 all: true,
31 normal: false,
32 warning: false,
33 critical: false
34 };
35
36 var sensorType = $routeParams.type;
37
38 $scope.export_name = sensorType + "_sensors.json";
39
40 $scope.toggleSeverityAll = function(){
41 $scope.selectedSeverity.all = !$scope.selectedSeverity.all;
42
43 if($scope.selectedSeverity.all){
44 $scope.selectedSeverity.normal = false;
45 $scope.selectedSeverity.warning = false;
46 $scope.selectedSeverity.critical = false;
47 }
48 }
49
50 $scope.toggleSeverity = function(severity){
51 $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity];
52
Gunnar Millseedefd32018-02-28 17:02:34 -060053 if($scope.selectedSeverity.normal &&
54 $scope.selectedSeverity.warning &&
Iftekharul Islamd2269e22017-05-02 09:32:45 -050055 $scope.selectedSeverity.critical){
56 $scope.selectedSeverity.all = true;
57 $scope.selectedSeverity.normal = false;
58 $scope.selectedSeverity.warning = false;
59 $scope.selectedSeverity.critical = false;
60 }else{
61 $scope.selectedSeverity.all = false;
62 }
63 }
64
65 $scope.doSearchOnEnter = function (event) {
66 var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
67 if (event.keyCode === 13 &&
68 search.length >= 2) {
69 $scope.searchTerms = $scope.customSearch.split(" ");
70 }else{
71 if(search.length == 0){
72 $scope.searchTerms = [];
73 }
74 }
75 };
76
77 $scope.doSearchOnClick = function() {
78 var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
79 if (search.length >= 2) {
80 $scope.searchTerms = $scope.customSearch.split(" ");
81 }else{
82 if(search.length == 0){
83 $scope.searchTerms = [];
84 }
85 }
86 }
87
88 $scope.jsonData = function(data){
89 var dt = {};
90 data.data.forEach(function(item){
91 dt[item.original_data.key] = item.original_data.value;
92 });
93 return JSON.stringify(dt);
94 };
95
96 $scope.filterBySeverity = function(sensor){
97 if($scope.selectedSeverity.all) return true;
98
99 return( (sensor.severity_flags.normal && $scope.selectedSeverity.normal) ||
100 (sensor.severity_flags.warning && $scope.selectedSeverity.warning) ||
101 (sensor.severity_flags.critical && $scope.selectedSeverity.critical)
102 );
103 }
104 $scope.filterBySearchTerms = function(sensor){
105
Gunnar Millseedefd32018-02-28 17:02:34 -0600106 if(!$scope.searchTerms.length) return true;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500107
108 for(var i = 0, length = $scope.searchTerms.length; i < length; i++){
109 if(sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
110 }
111 return true;
112 }
113
114 function setSensorData(){
115 var data = dataService.sensorData.sensors.filter(function(item){
116 return item.type == sensorType;
117 });
118 if(data.length){
119 sensorData = data[0];
120 $scope.data = sensorData;
121 $scope.export_data = $scope.jsonData($scope.data);
122 }
123 }
124
125 if(!dataService.sensorData.sensors){
126 APIUtils.getAllSensorStatus(function(data, originalData){
127 dataService.sensorData = data;
128 setSensorData();
129 });
130 }else{
131 setSensorData();
132 }
133
Michael Davisaeedf9e2017-04-06 14:35:56 -0500134 }
135 ]
136 );
137
Iftekharul Islamcd789502017-04-19 14:37:55 -0500138})(angular);