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