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