blob: 7493ba5f5a12605dcad65cc4dcdbc3b079a96763 [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', [
Gunnar Millseedefd32018-02-28 17:02:34 -060018 '$scope',
19 '$window',
20 'APIUtils',
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050021 'dataService',
22 'Constants',
Michael Davis428375e2017-08-01 15:48:34 -050023 '$routeParams',
Iftekharul Islam857e78b2017-09-11 11:09:31 -050024 '$filter',
Andrew Geissler4dcc1422018-04-11 12:24:22 -070025 function($scope, $window, APIUtils, dataService, Constants, $routeParams, $filter){
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050026 $scope.dataService = dataService;
27 $scope.logs = [];
28 $scope.tmz = 'EDT';
29 $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE;
Michael Davis428375e2017-08-01 15:48:34 -050030 $scope.loading = false;
Iftekharul Islam96bbf312017-08-22 13:44:38 -050031 var expandedSelectedIdOnce = false;
Michael Davis428375e2017-08-01 15:48:34 -050032
33 var sensorType = $routeParams.type;
Iftekharul Islam96bbf312017-08-22 13:44:38 -050034 var eventId = $routeParams.id;
Michael Davis428375e2017-08-01 15:48:34 -050035
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050036 // priority buttons
37 $scope.selectedSeverity = {
38 all: true,
39 low: false,
40 medium: false,
41 high: false
42 };
Michael Davis428375e2017-08-01 15:48:34 -050043
44 if(sensorType == 'high'){
45 $scope.selectedSeverity.all = false;
46 $scope.selectedSeverity.high = true;
47 }
48
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050049 $scope.selectedStatus = {
50 all: true,
51 resolved: false
52 };
53
54 $scope.customSearch = "";
55 $scope.searchItems = [];
56 $scope.selectedEvents = [];
57
Iftekharul Islam96bbf312017-08-22 13:44:38 -050058
59 if(eventId){
60 $scope.customSearch = "#"+eventId;
61 $scope.searchItems.push("#"+eventId);
62 }
63
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050064 $scope.loadLogs = function(){
Michael Davis428375e2017-08-01 15:48:34 -050065 $scope.loading = true;
66 APIUtils.getLogs().then(function(result){
Iftekharul Islam96bbf312017-08-22 13:44:38 -050067 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 Islamc22425f2017-09-06 10:04:14 -050077 dataService.updateServerHealth(result.data);
Michael Davis428375e2017-08-01 15:48:34 -050078 $scope.logs = result.data;
79 $scope.originalData = result.original;
80 $scope.loading = false;
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050081 });
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 Islam8b4828a2017-04-19 14:37:55 -050096 $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 Islam857e78b2017-09-11 11:09:31 -0500104 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 Islam8b4828a2017-04-19 14:37:55 -0500112 return (date >= $scope.start_date &&
Iftekharul Islam857e78b2017-09-11 11:09:31 -0500113 date <= endDate );
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500114 }else{
115 return true;
116 }
117 }
118
119 $scope.filterBySearchTerms = function(log){
Gunnar Millseedefd32018-02-28 17:02:34 -0600120 if(!$scope.searchItems.length) return true;
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500121
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500122 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 Millseedefd32018-02-28 17:02:34 -0600133 }
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500134 });
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 Islamf2d74642017-07-10 16:42:14 -0500164
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 Islam8b4828a2017-04-19 14:37:55 -0500186 $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 Islam8b4828a2017-04-19 14:37:55 -0500194 }
195 ]
196 );
197
198})(angular);