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