Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 1 | /** |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 2 | * Controller for sensors-overview |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 3 | * |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 4 | * @module app/serverHealth |
| 5 | * @exports sensorsOverviewController |
| 6 | * @name sensorsOverviewController |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 7 | * @version 0.1.0 |
| 8 | */ |
| 9 | |
| 10 | window.angular && (function (angular) { |
| 11 | 'use strict'; |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 12 | angular |
| 13 | .module('app.overview') |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 14 | .controller('sensorsOverviewController', [ |
CamVan Nguyen | 23217da | 2018-03-22 00:22:50 -0500 | [diff] [blame] | 15 | '$rootScope', |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 16 | '$scope', |
| 17 | '$log', |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 18 | '$window', |
| 19 | 'APIUtils', |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 20 | 'dataService', |
Iftekharul Islam | 81a49de | 2018-02-08 13:28:09 -0600 | [diff] [blame] | 21 | 'Constants', |
CamVan Nguyen | 23217da | 2018-03-22 00:22:50 -0500 | [diff] [blame] | 22 | function($rootScope, $scope, $log, $window, APIUtils, dataService, Constants){ |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 23 | $scope.dataService = dataService; |
| 24 | |
| 25 | $scope.dropdown_selected = false; |
| 26 | |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 27 | $scope.$log = $log; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 28 | $scope.customSearch = ""; |
| 29 | $scope.searchTerms = []; |
Iftekharul Islam | 81a49de | 2018-02-08 13:28:09 -0600 | [diff] [blame] | 30 | $scope.messages = Constants.MESSAGES.SENSOR; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 31 | $scope.selectedSeverity = { |
| 32 | all: true, |
| 33 | normal: false, |
| 34 | warning: false, |
| 35 | critical: false |
| 36 | }; |
| 37 | $scope.export_name = "sensors.json"; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 38 | $scope.loading = false; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 39 | $scope.jsonData = function(data){ |
| 40 | var dt = {}; |
| 41 | data.data.forEach(function(item){ |
| 42 | dt[item.original_data.key] = item.original_data.value; |
| 43 | }); |
| 44 | return JSON.stringify(dt); |
| 45 | }; |
| 46 | |
Iftekharul Islam | 171c6a1 | 2017-08-11 08:35:47 -0500 | [diff] [blame] | 47 | $scope.clear = function(){ |
| 48 | $scope.customSearch = ""; |
| 49 | $scope.searchTerms = []; |
| 50 | } |
| 51 | |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 52 | $scope.doSearchOnEnter = function (event) { |
| 53 | var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,''); |
| 54 | if (event.keyCode === 13 && |
| 55 | search.length >= 2) { |
| 56 | $scope.searchTerms = $scope.customSearch.split(" "); |
| 57 | }else{ |
| 58 | if(search.length == 0){ |
| 59 | $scope.searchTerms = []; |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | $scope.doSearchOnClick = function() { |
| 65 | var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,''); |
| 66 | if (search.length >= 2) { |
| 67 | $scope.searchTerms = $scope.customSearch.split(" "); |
| 68 | }else{ |
| 69 | if(search.length == 0){ |
| 70 | $scope.searchTerms = []; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $scope.toggleSeverityAll = function(){ |
Iftekharul Islam | c22425f | 2017-09-06 10:04:14 -0500 | [diff] [blame] | 76 | $scope.selectedSeverity.all = !$scope.selectedSeverity.all; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 77 | |
| 78 | if($scope.selectedSeverity.all){ |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 79 | $scope.selectedSeverity.warning = false; |
| 80 | $scope.selectedSeverity.critical = false; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | $scope.toggleSeverity = function(severity){ |
| 85 | $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity]; |
| 86 | |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame] | 87 | if(['warning', 'critical'].indexOf(severity) > -1){ |
| 88 | if($scope.selectedSeverity[severity] == false && |
| 89 | (!$scope.selectedSeverity.warning && |
| 90 | !$scope.selectedSeverity.critical |
| 91 | )){ |
| 92 | $scope.selectedSeverity.all = true; |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 97 | if($scope.selectedSeverity.warning && |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 98 | $scope.selectedSeverity.critical){ |
| 99 | $scope.selectedSeverity.all = true; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 100 | $scope.selectedSeverity.warning = false; |
| 101 | $scope.selectedSeverity.critical = false; |
| 102 | }else{ |
| 103 | $scope.selectedSeverity.all = false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | $scope.filterBySeverity = function(sensor){ |
| 108 | if($scope.selectedSeverity.all) return true; |
| 109 | |
| 110 | return( (sensor.severity_flags.normal && $scope.selectedSeverity.normal) || |
| 111 | (sensor.severity_flags.warning && $scope.selectedSeverity.warning) || |
| 112 | (sensor.severity_flags.critical && $scope.selectedSeverity.critical) |
| 113 | ); |
| 114 | } |
| 115 | $scope.filterBySearchTerms = function(sensor){ |
| 116 | |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 117 | if(!$scope.searchTerms.length) return true; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 118 | |
| 119 | for(var i = 0, length = $scope.searchTerms.length; i < length; i++){ |
| 120 | if(sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | $scope.loadSensorData = function(){ |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 126 | $scope.loading = true; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 127 | APIUtils.getAllSensorStatus(function(data, originalData){ |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 128 | $scope.data = data; |
| 129 | $scope.originalData = originalData; |
Iftekharul Islam | c153592 | 2017-06-19 12:49:04 -0500 | [diff] [blame] | 130 | dataService.sensorData = data; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 131 | $scope.export_data = JSON.stringify(originalData); |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 132 | $scope.loading = false; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 133 | }); |
| 134 | }; |
| 135 | |
| 136 | $scope.loadSensorData(); |
CamVan Nguyen | 23217da | 2018-03-22 00:22:50 -0500 | [diff] [blame] | 137 | |
| 138 | var refreshDataListener = $rootScope.$on('refresh-data', function(event, args) { |
| 139 | $scope.loadSensorData(); |
| 140 | }); |
| 141 | |
| 142 | $scope.$on('$destroy', function() { |
| 143 | refreshDataListener(); |
| 144 | }); |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 145 | } |
| 146 | ] |
| 147 | ); |
| 148 | |
CamVan Nguyen | 23217da | 2018-03-22 00:22:50 -0500 | [diff] [blame] | 149 | })(angular); |