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') |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 14 | .config(function(paginationTemplateProvider) { |
| 15 | paginationTemplateProvider.setString(require('../../common/directives/dirPagination.tpl.html')); |
| 16 | }) |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 17 | .controller('logController', [ |
Iftekharul Islam | a4dacb6 | 2018-03-21 10:45:52 -0500 | [diff] [blame^] | 18 | '$rootScope', |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 19 | '$scope', |
| 20 | '$window', |
| 21 | 'APIUtils', |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 22 | 'dataService', |
| 23 | 'Constants', |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 24 | '$routeParams', |
Iftekharul Islam | 857e78b | 2017-09-11 11:09:31 -0500 | [diff] [blame] | 25 | '$filter', |
Iftekharul Islam | a4dacb6 | 2018-03-21 10:45:52 -0500 | [diff] [blame^] | 26 | function($rootScope, $scope, $window, APIUtils, dataService, Constants, $routeParams, $filter){ |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 27 | $scope.dataService = dataService; |
| 28 | $scope.logs = []; |
| 29 | $scope.tmz = 'EDT'; |
| 30 | $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 31 | $scope.loading = false; |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame] | 32 | var expandedSelectedIdOnce = false; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 33 | |
| 34 | var sensorType = $routeParams.type; |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame] | 35 | var eventId = $routeParams.id; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 36 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 37 | // priority buttons |
| 38 | $scope.selectedSeverity = { |
| 39 | all: true, |
| 40 | low: false, |
| 41 | medium: false, |
| 42 | high: false |
| 43 | }; |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 44 | |
| 45 | if(sensorType == 'high'){ |
| 46 | $scope.selectedSeverity.all = false; |
| 47 | $scope.selectedSeverity.high = true; |
| 48 | } |
| 49 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 50 | $scope.selectedStatus = { |
| 51 | all: true, |
| 52 | resolved: false |
| 53 | }; |
| 54 | |
| 55 | $scope.customSearch = ""; |
| 56 | $scope.searchItems = []; |
| 57 | $scope.selectedEvents = []; |
| 58 | |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame] | 59 | |
| 60 | if(eventId){ |
| 61 | $scope.customSearch = "#"+eventId; |
| 62 | $scope.searchItems.push("#"+eventId); |
| 63 | } |
| 64 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 65 | $scope.loadLogs = function(){ |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 66 | $scope.loading = true; |
| 67 | APIUtils.getLogs().then(function(result){ |
Iftekharul Islam | 96bbf31 | 2017-08-22 13:44:38 -0500 | [diff] [blame] | 68 | if(eventId && expandedSelectedIdOnce == false){ |
| 69 | var log = result.data.filter(function(item){ |
| 70 | return item.Id == eventId; |
| 71 | }); |
| 72 | |
| 73 | if(log.length){ |
| 74 | log[0].meta = true; |
| 75 | } |
| 76 | expandedSelectedIdOnce = true; |
| 77 | } |
Iftekharul Islam | c22425f | 2017-09-06 10:04:14 -0500 | [diff] [blame] | 78 | dataService.updateServerHealth(result.data); |
Michael Davis | 428375e | 2017-08-01 15:48:34 -0500 | [diff] [blame] | 79 | $scope.logs = result.data; |
| 80 | $scope.originalData = result.original; |
| 81 | $scope.loading = false; |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 82 | }); |
| 83 | }; |
| 84 | $scope.jsonData = function(data){ |
| 85 | return JSON.stringify(data); |
| 86 | }; |
| 87 | |
| 88 | $scope.filterBySeverity = function(log){ |
| 89 | if($scope.selectedSeverity.all) return true; |
| 90 | |
| 91 | return( (log.severity_flags.low && $scope.selectedSeverity.low) || |
| 92 | (log.severity_flags.medium && $scope.selectedSeverity.medium) || |
| 93 | (log.severity_flags.high && $scope.selectedSeverity.high) |
| 94 | ); |
| 95 | } |
| 96 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 97 | $scope.filterByStatus = function(log){ |
| 98 | if ($scope.selectedStatus.all) return true; |
| 99 | return( (log.Resolved && $scope.selectedStatus.resolved)|| |
| 100 | (!log.Resolved && !$scope.selectedStatus.resolved) |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | $scope.filterByDate = function(log){ |
Iftekharul Islam | 857e78b | 2017-09-11 11:09:31 -0500 | [diff] [blame] | 105 | var endDate; |
| 106 | if($scope.end_date && typeof $scope.end_date.getTime === 'function'){ |
| 107 | endDate = new Date($scope.end_date.getTime()); |
| 108 | endDate.setTime(endDate.getTime() + 86399000); |
| 109 | } |
| 110 | |
| 111 | if($scope.start_date && endDate){ |
| 112 | var date = new Date($filter('date')(log.Timestamp, 'MM/dd/yyyy HH:mm:ss', $scope.tmz)); |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 113 | return (date >= $scope.start_date && |
Iftekharul Islam | 857e78b | 2017-09-11 11:09:31 -0500 | [diff] [blame] | 114 | date <= endDate ); |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 115 | }else{ |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | $scope.filterBySearchTerms = function(log){ |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 121 | if(!$scope.searchItems.length) return true; |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 122 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 123 | for(var i = 0, length = $scope.searchItems.length; i < length; i++){ |
| 124 | if(log.search_text.indexOf($scope.searchItems[i].toLowerCase()) == -1) return false; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | $scope.addSearchItem = function(searchTerms){ |
| 130 | var terms = searchTerms.split(" "); |
| 131 | terms.forEach(function(searchTerm){ |
| 132 | if($scope.searchItems.indexOf(searchTerm) == -1){ |
| 133 | $scope.searchItems.push(searchTerm); |
Gunnar Mills | eedefd3 | 2018-02-28 17:02:34 -0600 | [diff] [blame] | 134 | } |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 135 | }); |
| 136 | } |
| 137 | |
| 138 | $scope.clearSearchItem = function(searchTerm){ |
| 139 | $scope.searchItems = []; |
| 140 | } |
| 141 | |
| 142 | $scope.removeSearchItem = function(searchTerm){ |
| 143 | var termIndex = $scope.searchItems.indexOf(searchTerm); |
| 144 | |
| 145 | if(termIndex > -1){ |
| 146 | $scope.searchItems.splice(termIndex,1); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | $scope.$watch('all', function(){ |
| 151 | $scope.logs.forEach(function(item){ |
| 152 | item.selected = $scope.all; |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | function updateExportData(){ |
| 157 | $scope.export_name = ($scope.selectedEvents.length == 1) ? $scope.selectedEvents[0].Id + ".json" : "export.json"; |
| 158 | var data = {}; |
| 159 | $scope.selectedEvents.forEach(function(item){ |
| 160 | data[item.data.key] = item.data.value; |
| 161 | }); |
| 162 | $scope.export_data = JSON.stringify(data); |
| 163 | } |
| 164 | |
Iftekharul Islam | f2d7464 | 2017-07-10 16:42:14 -0500 | [diff] [blame] | 165 | |
| 166 | $scope.accept = function(){ |
| 167 | APIUtils.deleteLogs($scope.selectedEvents).then(function(){ |
| 168 | $scope.confirm = false; |
| 169 | $scope.loadLogs(); |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | $scope.resolve = function(){ |
| 174 | var events = $scope.selectedEvents.filter(function(item){ |
| 175 | return item.Resolved != 1; |
| 176 | }); |
| 177 | |
| 178 | if(!events.length) return; |
| 179 | |
| 180 | APIUtils.resolveLogs(events).then(function(){ |
| 181 | events.forEach(function(item){ |
| 182 | item.Resolved = 1; |
| 183 | }); |
| 184 | }); |
| 185 | } |
| 186 | |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 187 | $scope.$watch('logs', function(){ |
| 188 | $scope.selectedEvents = $scope.logs.filter(function(item){ |
| 189 | return item.selected; |
| 190 | }); |
| 191 | updateExportData(); |
| 192 | }, true); |
| 193 | |
| 194 | $scope.loadLogs(); |
Iftekharul Islam | a4dacb6 | 2018-03-21 10:45:52 -0500 | [diff] [blame^] | 195 | |
| 196 | var refreshDataListener = $rootScope.$on('refresh-data', function(event, args){ |
| 197 | $scope.loadLogs(); |
| 198 | }); |
| 199 | |
| 200 | $scope.$on('$destroy', function() { |
| 201 | refreshDataListener(); |
| 202 | }); |
Iftekharul Islam | 8b4828a | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 203 | } |
| 204 | ] |
| 205 | ); |
| 206 | |
| 207 | })(angular); |