blob: d87177887a1d7250f833fd084c0a2c55748cefb0 [file] [log] [blame]
Iftekharul Islam8b4828a2017-04-19 14:37:55 -05001/**
2 * Controller for log
3 *
4 * @module app/serverHealth
5 * @exports logController
6 * @name logController
7 * @version 0.1.0
8 */
9
10window.angular && (function (angular) {
11 'use strict';
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050012 angular
13 .module('app.serverHealth')
Ed Tanousbbcf6702017-10-06 13:53:06 -070014 .config(function(paginationTemplateProvider) {
15 paginationTemplateProvider.setString(require('../../common/directives/dirPagination.tpl.html'));
16 })
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050017 .controller('logController', [
Iftekharul Islama4dacb62018-03-21 10:45:52 -050018 '$rootScope',
Gunnar Millseedefd32018-02-28 17:02:34 -060019 '$scope',
20 '$window',
21 'APIUtils',
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050022 'dataService',
23 'Constants',
Michael Davis428375e2017-08-01 15:48:34 -050024 '$routeParams',
Iftekharul Islam857e78b2017-09-11 11:09:31 -050025 '$filter',
Iftekharul Islama4dacb62018-03-21 10:45:52 -050026 function($rootScope, $scope, $window, APIUtils, dataService, Constants, $routeParams, $filter){
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050027 $scope.dataService = dataService;
28 $scope.logs = [];
29 $scope.tmz = 'EDT';
30 $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE;
Michael Davis428375e2017-08-01 15:48:34 -050031 $scope.loading = false;
Iftekharul Islam96bbf312017-08-22 13:44:38 -050032 var expandedSelectedIdOnce = false;
Michael Davis428375e2017-08-01 15:48:34 -050033
34 var sensorType = $routeParams.type;
Iftekharul Islam96bbf312017-08-22 13:44:38 -050035 var eventId = $routeParams.id;
Michael Davis428375e2017-08-01 15:48:34 -050036
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050037 // priority buttons
38 $scope.selectedSeverity = {
39 all: true,
40 low: false,
41 medium: false,
42 high: false
43 };
Michael Davis428375e2017-08-01 15:48:34 -050044
45 if(sensorType == 'high'){
46 $scope.selectedSeverity.all = false;
47 $scope.selectedSeverity.high = true;
48 }
49
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050050 $scope.selectedStatus = {
51 all: true,
52 resolved: false
53 };
54
55 $scope.customSearch = "";
56 $scope.searchItems = [];
57 $scope.selectedEvents = [];
58
Iftekharul Islam96bbf312017-08-22 13:44:38 -050059
60 if(eventId){
61 $scope.customSearch = "#"+eventId;
62 $scope.searchItems.push("#"+eventId);
63 }
64
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050065 $scope.loadLogs = function(){
Michael Davis428375e2017-08-01 15:48:34 -050066 $scope.loading = true;
67 APIUtils.getLogs().then(function(result){
Iftekharul Islam96bbf312017-08-22 13:44:38 -050068 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 Islamc22425f2017-09-06 10:04:14 -050078 dataService.updateServerHealth(result.data);
Michael Davis428375e2017-08-01 15:48:34 -050079 $scope.logs = result.data;
80 $scope.originalData = result.original;
81 $scope.loading = false;
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050082 });
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 Islam8b4828a2017-04-19 14:37:55 -050097 $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 Islam857e78b2017-09-11 11:09:31 -0500105 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 Islam8b4828a2017-04-19 14:37:55 -0500113 return (date >= $scope.start_date &&
Iftekharul Islam857e78b2017-09-11 11:09:31 -0500114 date <= endDate );
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500115 }else{
116 return true;
117 }
118 }
119
120 $scope.filterBySearchTerms = function(log){
Gunnar Millseedefd32018-02-28 17:02:34 -0600121 if(!$scope.searchItems.length) return true;
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500122
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500123 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 Millseedefd32018-02-28 17:02:34 -0600134 }
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500135 });
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 Islamf2d74642017-07-10 16:42:14 -0500165
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 Islam8b4828a2017-04-19 14:37:55 -0500187 $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 Islama4dacb62018-03-21 10:45:52 -0500195
196 var refreshDataListener = $rootScope.$on('refresh-data', function(event, args){
197 $scope.loadLogs();
198 });
199
200 $scope.$on('$destroy', function() {
201 refreshDataListener();
202 });
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500203 }
204 ]
205 );
206
207})(angular);