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