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