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