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