Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for log |
| 3 | * |
| 4 | * @module app/serverHealth |
| 5 | * @exports logController |
| 6 | * @name logController |
| 7 | * @version 0.1.0 |
| 8 | */ |
| 9 | |
| 10 | window.angular && (function (angular) { |
| 11 | 'use strict'; |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 12 | angular |
| 13 | .module('app.serverHealth') |
| 14 | .controller('logController', [ |
| 15 | '$scope', |
| 16 | '$window', |
| 17 | 'APIUtils', |
| 18 | 'dataService', |
| 19 | 'Constants', |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 20 | '$routeParams', |
| 21 | function($scope, $window, APIUtils, dataService, Constants, $routeParams){ |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 22 | $scope.dataService = dataService; |
| 23 | $scope.logs = []; |
| 24 | $scope.tmz = 'EDT'; |
| 25 | $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 26 | $scope.loading = false; |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame^] | 27 | var expandedSelectedIdOnce = false; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 28 | |
| 29 | var sensorType = $routeParams.type; |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame^] | 30 | var eventId = $routeParams.id; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 31 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 32 | // priority buttons |
| 33 | $scope.selectedSeverity = { |
| 34 | all: true, |
| 35 | low: false, |
| 36 | medium: false, |
| 37 | high: false |
| 38 | }; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 39 | |
| 40 | if(sensorType == 'high'){ |
| 41 | $scope.selectedSeverity.all = false; |
| 42 | $scope.selectedSeverity.high = true; |
| 43 | } |
| 44 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 45 | $scope.selectedStatus = { |
| 46 | all: true, |
| 47 | resolved: false |
| 48 | }; |
| 49 | |
| 50 | $scope.customSearch = ""; |
| 51 | $scope.searchItems = []; |
| 52 | $scope.selectedEvents = []; |
| 53 | |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame^] | 54 | |
| 55 | if(eventId){ |
| 56 | $scope.customSearch = "#"+eventId; |
| 57 | $scope.searchItems.push("#"+eventId); |
| 58 | } |
| 59 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 60 | $scope.loadLogs = function(){ |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 61 | $scope.loading = true; |
| 62 | APIUtils.getLogs().then(function(result){ |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame^] | 63 | if(eventId && expandedSelectedIdOnce == false){ |
| 64 | var log = result.data.filter(function(item){ |
| 65 | return item.Id == eventId; |
| 66 | }); |
| 67 | |
| 68 | if(log.length){ |
| 69 | log[0].meta = true; |
| 70 | } |
| 71 | expandedSelectedIdOnce = true; |
| 72 | } |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 73 | $scope.logs = result.data; |
| 74 | $scope.originalData = result.original; |
| 75 | $scope.loading = false; |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 76 | }); |
| 77 | }; |
| 78 | $scope.jsonData = function(data){ |
| 79 | return JSON.stringify(data); |
| 80 | }; |
| 81 | |
| 82 | $scope.filterBySeverity = function(log){ |
| 83 | if($scope.selectedSeverity.all) return true; |
| 84 | |
| 85 | return( (log.severity_flags.low && $scope.selectedSeverity.low) || |
| 86 | (log.severity_flags.medium && $scope.selectedSeverity.medium) || |
| 87 | (log.severity_flags.high && $scope.selectedSeverity.high) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | $scope.filterByStatus = function(log){ |
| 93 | if ($scope.selectedStatus.all) return true; |
| 94 | return( (log.Resolved && $scope.selectedStatus.resolved)|| |
| 95 | (!log.Resolved && !$scope.selectedStatus.resolved) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | $scope.filterByDate = function(log){ |
| 100 | if($scope.start_date && $scope.end_date){ |
| 101 | var date = new Date(log.Timestamp); |
| 102 | return (date >= $scope.start_date && |
| 103 | date <= $scope.end_date ); |
| 104 | }else{ |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $scope.filterBySearchTerms = function(log){ |
| 110 | if(!$scope.searchItems.length) return true; |
| 111 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 112 | for(var i = 0, length = $scope.searchItems.length; i < length; i++){ |
| 113 | if(log.search_text.indexOf($scope.searchItems[i].toLowerCase()) == -1) return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | $scope.addSearchItem = function(searchTerms){ |
| 119 | var terms = searchTerms.split(" "); |
| 120 | terms.forEach(function(searchTerm){ |
| 121 | if($scope.searchItems.indexOf(searchTerm) == -1){ |
| 122 | $scope.searchItems.push(searchTerm); |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | $scope.clearSearchItem = function(searchTerm){ |
| 128 | $scope.searchItems = []; |
| 129 | } |
| 130 | |
| 131 | $scope.removeSearchItem = function(searchTerm){ |
| 132 | var termIndex = $scope.searchItems.indexOf(searchTerm); |
| 133 | |
| 134 | if(termIndex > -1){ |
| 135 | $scope.searchItems.splice(termIndex,1); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | $scope.$watch('all', function(){ |
| 140 | $scope.logs.forEach(function(item){ |
| 141 | item.selected = $scope.all; |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | function updateExportData(){ |
| 146 | $scope.export_name = ($scope.selectedEvents.length == 1) ? $scope.selectedEvents[0].Id + ".json" : "export.json"; |
| 147 | var data = {}; |
| 148 | $scope.selectedEvents.forEach(function(item){ |
| 149 | data[item.data.key] = item.data.value; |
| 150 | }); |
| 151 | $scope.export_data = JSON.stringify(data); |
| 152 | } |
| 153 | |
Iftekharul Islam | f2d7464 | 2017-07-10 16:42:14 -0500 | [diff] [blame] | 154 | |
| 155 | $scope.accept = function(){ |
| 156 | APIUtils.deleteLogs($scope.selectedEvents).then(function(){ |
| 157 | $scope.confirm = false; |
| 158 | $scope.loadLogs(); |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | $scope.resolve = function(){ |
| 163 | var events = $scope.selectedEvents.filter(function(item){ |
| 164 | return item.Resolved != 1; |
| 165 | }); |
| 166 | |
| 167 | if(!events.length) return; |
| 168 | |
| 169 | APIUtils.resolveLogs(events).then(function(){ |
| 170 | events.forEach(function(item){ |
| 171 | item.Resolved = 1; |
| 172 | }); |
| 173 | }); |
| 174 | } |
| 175 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 176 | $scope.$watch('logs', function(){ |
| 177 | $scope.selectedEvents = $scope.logs.filter(function(item){ |
| 178 | return item.selected; |
| 179 | }); |
| 180 | updateExportData(); |
| 181 | }, true); |
| 182 | |
| 183 | $scope.loadLogs(); |
| 184 | } |
| 185 | ] |
| 186 | ); |
| 187 | |
| 188 | })(angular); |