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