blob: 963c6c0587606fdd0ef6fe0142ecec74c1ed60f1 [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')
14 .controller('logController', [
15 '$scope',
16 '$window',
17 'APIUtils',
18 'dataService',
19 'Constants',
Michael Davis428375e2017-08-01 15:48:34 -050020 '$routeParams',
21 function($scope, $window, APIUtils, dataService, Constants, $routeParams){
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050022 $scope.dataService = dataService;
23 $scope.logs = [];
24 $scope.tmz = 'EDT';
25 $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE;
Michael Davis428375e2017-08-01 15:48:34 -050026 $scope.loading = false;
Iftekharul Islam96bbf312017-08-22 13:44:38 -050027 var expandedSelectedIdOnce = false;
Michael Davis428375e2017-08-01 15:48:34 -050028
29 var sensorType = $routeParams.type;
Iftekharul Islam96bbf312017-08-22 13:44:38 -050030 var eventId = $routeParams.id;
Michael Davis428375e2017-08-01 15:48:34 -050031
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050032 // priority buttons
33 $scope.selectedSeverity = {
34 all: true,
35 low: false,
36 medium: false,
37 high: false
38 };
Michael Davis428375e2017-08-01 15:48:34 -050039
40 if(sensorType == 'high'){
41 $scope.selectedSeverity.all = false;
42 $scope.selectedSeverity.high = true;
43 }
44
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050045 $scope.selectedStatus = {
46 all: true,
47 resolved: false
48 };
49
50 $scope.customSearch = "";
51 $scope.searchItems = [];
52 $scope.selectedEvents = [];
53
Iftekharul Islam96bbf312017-08-22 13:44:38 -050054
55 if(eventId){
56 $scope.customSearch = "#"+eventId;
57 $scope.searchItems.push("#"+eventId);
58 }
59
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050060 $scope.loadLogs = function(){
Michael Davis428375e2017-08-01 15:48:34 -050061 $scope.loading = true;
62 APIUtils.getLogs().then(function(result){
Iftekharul Islam96bbf312017-08-22 13:44:38 -050063 if(eventId && expandedSelectedIdOnce == false){
64 var log = result.data.filter(function(item){
65 return item.Id == eventId;
66 });
67
68 if(log.length){
69 log[0].meta = true;
70 }
71 expandedSelectedIdOnce = true;
72 }
Iftekharul Islamc22425f2017-09-06 10:04:14 -050073 dataService.updateServerHealth(result.data);
Michael Davis428375e2017-08-01 15:48:34 -050074 $scope.logs = result.data;
75 $scope.originalData = result.original;
76 $scope.loading = false;
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050077 });
78 };
79 $scope.jsonData = function(data){
80 return JSON.stringify(data);
81 };
82
83 $scope.filterBySeverity = function(log){
84 if($scope.selectedSeverity.all) return true;
85
86 return( (log.severity_flags.low && $scope.selectedSeverity.low) ||
87 (log.severity_flags.medium && $scope.selectedSeverity.medium) ||
88 (log.severity_flags.high && $scope.selectedSeverity.high)
89 );
90 }
91
Iftekharul Islam8b4828a2017-04-19 14:37:55 -050092 $scope.filterByStatus = function(log){
93 if ($scope.selectedStatus.all) return true;
94 return( (log.Resolved && $scope.selectedStatus.resolved)||
95 (!log.Resolved && !$scope.selectedStatus.resolved)
96 );
97 }
98
99 $scope.filterByDate = function(log){
100 if($scope.start_date && $scope.end_date){
101 var date = new Date(log.Timestamp);
102 return (date >= $scope.start_date &&
103 date <= $scope.end_date );
104 }else{
105 return true;
106 }
107 }
108
109 $scope.filterBySearchTerms = function(log){
110 if(!$scope.searchItems.length) return true;
111
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500112 for(var i = 0, length = $scope.searchItems.length; i < length; i++){
113 if(log.search_text.indexOf($scope.searchItems[i].toLowerCase()) == -1) return false;
114 }
115 return true;
116 }
117
118 $scope.addSearchItem = function(searchTerms){
119 var terms = searchTerms.split(" ");
120 terms.forEach(function(searchTerm){
121 if($scope.searchItems.indexOf(searchTerm) == -1){
122 $scope.searchItems.push(searchTerm);
123 }
124 });
125 }
126
127 $scope.clearSearchItem = function(searchTerm){
128 $scope.searchItems = [];
129 }
130
131 $scope.removeSearchItem = function(searchTerm){
132 var termIndex = $scope.searchItems.indexOf(searchTerm);
133
134 if(termIndex > -1){
135 $scope.searchItems.splice(termIndex,1);
136 }
137 }
138
139 $scope.$watch('all', function(){
140 $scope.logs.forEach(function(item){
141 item.selected = $scope.all;
142 });
143 });
144
145 function updateExportData(){
146 $scope.export_name = ($scope.selectedEvents.length == 1) ? $scope.selectedEvents[0].Id + ".json" : "export.json";
147 var data = {};
148 $scope.selectedEvents.forEach(function(item){
149 data[item.data.key] = item.data.value;
150 });
151 $scope.export_data = JSON.stringify(data);
152 }
153
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500154
155 $scope.accept = function(){
156 APIUtils.deleteLogs($scope.selectedEvents).then(function(){
157 $scope.confirm = false;
158 $scope.loadLogs();
159 });
160 }
161
162 $scope.resolve = function(){
163 var events = $scope.selectedEvents.filter(function(item){
164 return item.Resolved != 1;
165 });
166
167 if(!events.length) return;
168
169 APIUtils.resolveLogs(events).then(function(){
170 events.forEach(function(item){
171 item.Resolved = 1;
172 });
173 });
174 }
175
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500176 $scope.$watch('logs', function(){
177 $scope.selectedEvents = $scope.logs.filter(function(item){
178 return item.selected;
179 });
180 updateExportData();
181 }, true);
182
183 $scope.loadLogs();
184 }
185 ]
186 );
187
188})(angular);