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    var logData = [], originalData = {};
13    angular
14        .module('app.serverHealth')
15        .controller('logController', [
16            '$scope',
17            '$window',
18            'APIUtils',
19            'dataService',
20            'Constants',
21            function($scope, $window, APIUtils, dataService, Constants){
22                $scope.dataService = dataService;
23                $scope.logs = [];
24                $scope.tmz = 'EDT';
25                $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE;
26                // priority buttons
27                $scope.selectedSeverity = {
28                    all: true,
29                    low: false,
30                    medium: false,
31                    high: false
32                };
33                $scope.selectedStatus = {
34                    all: true,
35                    resolved: false
36                };
37
38                $scope.customSearch = "";
39                $scope.searchItems = [];
40                $scope.selectedEvents = [];
41
42                $scope.loadLogs = function(){
43                    APIUtils.getLogs(function(data, originalData){
44                        logData = data;
45                        originalData = originalData;
46                        $scope.logs = data;
47                        $scope.originalData = originalData;
48                    });
49                };
50                $scope.jsonData = function(data){
51                    return JSON.stringify(data);
52                };
53
54                $scope.filterBySeverity = function(log){
55                    if($scope.selectedSeverity.all) return true;
56
57                    return( (log.severity_flags.low && $scope.selectedSeverity.low) ||
58                            (log.severity_flags.medium && $scope.selectedSeverity.medium) ||
59                            (log.severity_flags.high && $scope.selectedSeverity.high)
60                    );
61                }
62
63
64                $scope.filterByStatus = function(log){
65                    if ($scope.selectedStatus.all) return true;
66                    return( (log.Resolved && $scope.selectedStatus.resolved)||
67                            (!log.Resolved && !$scope.selectedStatus.resolved)
68                    );
69                }
70
71                $scope.filterByDate = function(log){
72                    if($scope.start_date && $scope.end_date){
73                        var date = new Date(log.Timestamp);
74                        return (date >= $scope.start_date &&
75                               date <= $scope.end_date );
76                    }else{
77                        return true;
78                    }
79                }
80
81                $scope.filterBySearchTerms = function(log){
82                    if(!$scope.searchItems.length) return true;
83
84                    var flag = false;
85                    for(var i = 0, length = $scope.searchItems.length; i < length; i++){
86                        if(log.search_text.indexOf($scope.searchItems[i].toLowerCase()) == -1) return false;
87                    }
88                    return true;
89                }
90
91                $scope.addSearchItem = function(searchTerms){
92                    var terms = searchTerms.split(" ");
93                    terms.forEach(function(searchTerm){
94                        if($scope.searchItems.indexOf(searchTerm) == -1){
95                            $scope.searchItems.push(searchTerm);
96                        }
97                    });
98                }
99
100                $scope.clearSearchItem = function(searchTerm){
101                    $scope.searchItems = [];
102                }
103
104                $scope.removeSearchItem = function(searchTerm){
105                    var termIndex = $scope.searchItems.indexOf(searchTerm);
106
107                    if(termIndex > -1){
108                        $scope.searchItems.splice(termIndex,1);
109                    }
110                }
111
112                $scope.$watch('all', function(){
113                    $scope.logs.forEach(function(item){
114                        item.selected = $scope.all;
115                    });
116                });
117
118                function updateExportData(){
119                    $scope.export_name = ($scope.selectedEvents.length == 1) ? $scope.selectedEvents[0].Id + ".json" : "export.json";
120                    var data = {};
121                    $scope.selectedEvents.forEach(function(item){
122                        data[item.data.key] = item.data.value;
123                    });
124                    $scope.export_data = JSON.stringify(data);
125                }
126
127                $scope.$watch('logs', function(){
128                    $scope.selectedEvents = $scope.logs.filter(function(item){
129                        return item.selected;
130                    });
131                    updateExportData();
132                }, true);
133
134                $scope.loadLogs();
135            }
136        ]
137    );
138
139})(angular);
140