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