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