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