1/**
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';
12    angular
13        .module('app.serverHealth')
14        .config(function(paginationTemplateProvider) {
15            paginationTemplateProvider.setString(require('../../common/directives/dirPagination.tpl.html'));
16        })
17        .controller('logController', [
18            '$rootScope',
19            '$scope',
20            '$window',
21            'APIUtils',
22            'dataService',
23            'Constants',
24            '$routeParams',
25            '$filter',
26            function($rootScope, $scope, $window, APIUtils, dataService, Constants, $routeParams, $filter){
27                $scope.dataService = dataService;
28                $scope.logs = [];
29                $scope.tmz = 'EDT';
30                $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE;
31                $scope.loading = false;
32                var expandedSelectedIdOnce = false;
33
34                var sensorType = $routeParams.type;
35                var eventId = $routeParams.id;
36
37                // priority buttons
38                $scope.selectedSeverity = {
39                    all: true,
40                    low: false,
41                    medium: false,
42                    high: false
43                };
44
45                if(sensorType == 'high'){
46                    $scope.selectedSeverity.all = false;
47                    $scope.selectedSeverity.high = true;
48                }
49
50                $scope.selectedStatus = {
51                    all: true,
52                    resolved: false
53                };
54
55                $scope.customSearch = "";
56                $scope.searchItems = [];
57                $scope.selectedEvents = [];
58
59
60                if(eventId){
61                    $scope.customSearch = "#"+eventId;
62                    $scope.searchItems.push("#"+eventId);
63                }
64
65                $scope.loadLogs = function(){
66                    $scope.loading = true;
67                    APIUtils.getLogs().then(function(result){
68                        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                        }
78                        dataService.updateServerHealth(result.data);
79                        $scope.logs = result.data;
80                        $scope.originalData = result.original;
81                        $scope.loading = false;
82                    });
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
97                $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){
105                    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));
113                        return (date >= $scope.start_date &&
114                               date <= endDate );
115                    }else{
116                        return true;
117                    }
118                }
119
120                $scope.filterBySearchTerms = function(log){
121                    if(!$scope.searchItems.length) return true;
122
123                    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);
134                        }
135                    });
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
165
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
187                $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();
195
196                var refreshDataListener = $rootScope.$on('refresh-data', function(event, args){
197                    $scope.loadLogs();
198                });
199
200                $scope.$on('$destroy', function() {
201                    refreshDataListener();
202                });
203            }
204        ]
205    );
206
207})(angular);
208