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'; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 11 | angular.module('app.overview').controller('sensorsOverviewController', [ |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 12 | '$scope', '$log', '$window', 'APIUtils', 'dataService', 'Constants', |
| 13 | function($scope, $log, $window, APIUtils, dataService, Constants) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 14 | $scope.dataService = dataService; |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 15 | |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 16 | $scope.dropdown_selected = false; |
| 17 | |
| 18 | $scope.$log = $log; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 19 | $scope.customSearch = ''; |
| 20 | $scope.searchTerms = []; |
| 21 | $scope.messages = Constants.MESSAGES.SENSOR; |
| 22 | $scope.selectedSeverity = |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 23 | {all: true, normal: false, warning: false, critical: false}; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 24 | $scope.export_name = 'sensors.json'; |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 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 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 33 | |
| 34 | $scope.clear = function() { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 35 | $scope.customSearch = ''; |
| 36 | $scope.searchTerms = []; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 37 | }; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 38 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 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 = []; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 47 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 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 = []; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 59 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 60 | } |
| 61 | }; |
Michael Davis | 994a93b | 2017-04-18 10:01:04 -0500 | [diff] [blame] | 62 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 63 | $scope.toggleSeverityAll = function() { |
| 64 | $scope.selectedSeverity.all = !$scope.selectedSeverity.all; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 65 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 66 | if ($scope.selectedSeverity.all) { |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 67 | $scope.selectedSeverity.normal = false; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 68 | $scope.selectedSeverity.warning = false; |
| 69 | $scope.selectedSeverity.critical = false; |
| 70 | } |
| 71 | }; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 72 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 73 | $scope.toggleSeverity = function(severity) { |
| 74 | $scope.selectedSeverity[severity] = !$scope.selectedSeverity[severity]; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 75 | |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 76 | if (['normal', 'warning', 'critical'].indexOf(severity) > -1) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 77 | if ($scope.selectedSeverity[severity] == false && |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 78 | (!$scope.selectedSeverity.normal && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 79 | !$scope.selectedSeverity.warning && |
| 80 | !$scope.selectedSeverity.critical)) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 81 | $scope.selectedSeverity.all = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 82 | return; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 83 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 84 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 85 | |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 86 | if ($scope.selectedSeverity.normal && $scope.selectedSeverity.warning && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 87 | $scope.selectedSeverity.critical) { |
| 88 | $scope.selectedSeverity.all = true; |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 89 | $scope.selectedSeverity.normal = false; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 90 | $scope.selectedSeverity.warning = false; |
| 91 | $scope.selectedSeverity.critical = false; |
| 92 | } else { |
| 93 | $scope.selectedSeverity.all = false; |
| 94 | } |
| 95 | }; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 96 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 97 | $scope.filterBySeverity = function(sensor) { |
| 98 | if ($scope.selectedSeverity.all) return true; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 99 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 100 | return ( |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 101 | (sensor.severity_flags.normal && $scope.selectedSeverity.normal) || |
| 102 | (sensor.severity_flags.warning && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 103 | $scope.selectedSeverity.warning) || |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 104 | (sensor.severity_flags.critical && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 105 | $scope.selectedSeverity.critical)); |
| 106 | }; |
| 107 | $scope.filterBySearchTerms = function(sensor) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 108 | if (!$scope.searchTerms.length) return true; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 109 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 110 | for (var i = 0, length = $scope.searchTerms.length; i < length; i++) { |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 111 | if (sensor.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == |
| 112 | -1) |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | }; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 117 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 118 | $scope.loadSensorData = function() { |
| 119 | $scope.loading = true; |
Gunnar Mills | 615a2f8 | 2019-04-17 14:30:39 -0500 | [diff] [blame] | 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 | }); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | $scope.loadSensorData(); |
| 129 | } |
| 130 | ]); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 131 | })(angular); |