1/** 2 * Controller for log 3 * 4 * @module app/serverHealth 5 * @exports logController 6 * @name logController 7 */ 8 9window.angular && (function(angular) { 10 'use strict'; 11 angular.module('app.serverHealth') 12 .config([ 13 'paginationTemplateProvider', 14 function(paginationTemplateProvider) { 15 paginationTemplateProvider.setString( 16 require('../../common/directives/dirPagination.tpl.html')); 17 } 18 ]) 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.filteredLogs = []; 28 $scope.tmz = ''; 29 $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE; 30 $scope.loading = false; 31 var expandedSelectedIdOnce = false; 32 33 var sensorType = $routeParams.type; 34 var eventId = $routeParams.id; 35 36 // priority buttons 37 $scope.selectedSeverity = 38 {all: true, low: false, medium: false, high: false}; 39 40 if (sensorType == 'high') { 41 $scope.selectedSeverity.all = false; 42 $scope.selectedSeverity.high = true; 43 } 44 45 $scope.selectedStatus = {all: true, resolved: false}; 46 47 $scope.customSearch = ''; 48 $scope.searchItems = []; 49 $scope.selectedEvents = []; 50 51 if (eventId) { 52 $scope.customSearch = '#' + eventId; 53 $scope.searchItems.push('#' + eventId); 54 } 55 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 }); 63 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.selectUserTimezone(); 73 $scope.loading = false; 74 }); 75 }; 76 $scope.jsonData = function(data) { 77 return JSON.stringify(data); 78 }; 79 80 $scope.filterBySeverity = function(log) { 81 if ($scope.selectedSeverity.all) return true; 82 83 return ( 84 (log.severity_flags.low && $scope.selectedSeverity.low) || 85 (log.severity_flags.medium && $scope.selectedSeverity.medium) || 86 (log.severity_flags.high && $scope.selectedSeverity.high)); 87 }; 88 89 $scope.filterByStatus = function(log) { 90 if ($scope.selectedStatus.all) return true; 91 return ( 92 (log.Resolved && $scope.selectedStatus.resolved) || 93 (!log.Resolved && !$scope.selectedStatus.resolved)); 94 }; 95 96 $scope.filterByDate = function(log) { 97 var endDate; 98 if ($scope.end_date && 99 typeof $scope.end_date.getTime === 'function') { 100 endDate = new Date($scope.end_date.getTime()); 101 endDate.setTime(endDate.getTime() + 86399000); 102 } 103 104 if ($scope.start_date && endDate) { 105 var date = new Date($filter('date')( 106 log.Timestamp, 'MM/dd/yyyy HH:mm:ss', $scope.tmz)); 107 return (date >= $scope.start_date && date <= endDate); 108 } else { 109 return true; 110 } 111 }; 112 113 $scope.filterBySearchTerms = function(log) { 114 if (!$scope.searchItems.length) return true; 115 116 for (var i = 0, length = $scope.searchItems.length; i < length; 117 i++) { 118 if (log.search_text.indexOf( 119 $scope.searchItems[i].toLowerCase()) == -1) 120 return false; 121 } 122 return true; 123 }; 124 125 $scope.addSearchItem = function(searchTerms) { 126 var terms = searchTerms.split(' '); 127 terms.forEach(function(searchTerm) { 128 if ($scope.searchItems.indexOf(searchTerm) == -1) { 129 $scope.searchItems.push(searchTerm); 130 } 131 }); 132 }; 133 134 $scope.clearSearchItem = function(searchTerm) { 135 $scope.searchItems = []; 136 }; 137 138 $scope.removeSearchItem = function(searchTerm) { 139 var termIndex = $scope.searchItems.indexOf(searchTerm); 140 141 if (termIndex > -1) { 142 $scope.searchItems.splice(termIndex, 1); 143 } 144 }; 145 146 $scope.$watch('all', function() { 147 $scope.filteredLogs.forEach(function(item) { 148 item.selected = $scope.all; 149 }); 150 }); 151 152 function updateExportData() { 153 $scope.export_name = ($scope.selectedEvents.length == 1) ? 154 $scope.selectedEvents[0].Id + '.json' : 155 'export.json'; 156 var data = {}; 157 $scope.selectedEvents.forEach(function(item) { 158 data[item.data.key] = item.data.value; 159 }); 160 $scope.export_data = JSON.stringify(data); 161 } 162 163 $scope.selectUserTimezone = function() { 164 $scope.tmz = new Date().toString().match(/\(([A-Za-z\s].*)\)/)[1]; 165 }; 166 167 $scope.accept = function() { 168 APIUtils.deleteLogs($scope.selectedEvents).then(function() { 169 $scope.confirm = false; 170 $scope.loadLogs(); 171 }); 172 }; 173 174 $scope.resolve = function() { 175 var events = $scope.selectedEvents.filter(function(item) { 176 return item.Resolved != 1; 177 }); 178 179 if (!events.length) return; 180 181 APIUtils.resolveLogs(events).then(function() { 182 events.forEach(function(item) { 183 item.Resolved = 1; 184 }); 185 }); 186 }; 187 188 $scope.$watch('logs', function() { 189 $scope.selectedEvents = $scope.logs.filter(function(item) { 190 return item.selected; 191 }); 192 updateExportData(); 193 }, true); 194 195 $scope.loadLogs(); 196 } 197 ]); 198})(angular); 199