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