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