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