1/**
2 * Controller for log
3 *
4 * @module app/serverHealth
5 * @exports sysLogController
6 * @name sysLogController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11  angular.module('app.serverHealth')
12      .config([
13        'paginationTemplateProvider',
14        function(paginationTemplateProvider) {
15          paginationTemplateProvider.setString(
16              require('../../common/directives/dirPagination.tpl.html'));
17        }
18      ])
19      .controller('sysLogController', [
20        '$scope', 'APIUtils', 'Constants',
21        function($scope, APIUtils, Constants) {
22          $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE;
23          $scope.loading = true;
24          $scope.sysLogs = [];
25          $scope.customSearch = '';
26          $scope.searchTerms = [];
27          $scope.sortKey = 'Id';
28          $scope.showLogDropdown = false;
29          $scope.recordTypeList =
30              ['SEL', 'Event', 'Oem'];        // From Redfish specification.
31          $scope.selectedRecordType = 'SEL';  // Default Select to SEL.
32          $scope.typeFilter = false;
33          $scope.selectedSeverityList = [];
34          $scope.severityList = ['All', 'Critical', 'Warning', 'Ok'];
35          $scope.filterTypes = [];
36          $scope.selectedType = 'All';
37
38          $scope.selectRecordType = function(recordType) {
39            $scope.selectedRecordType = recordType;
40            $scope.showLogDropdown = false;
41            $scope.filterTypes = [];
42
43            APIUtils.getSystemLogs(recordType)
44                .then(
45                    function(res) {
46                      $scope.sysLogs = res;
47                      $scope.filterTypes.push('All');
48                      $scope.sysLogs.forEach(function(log) {
49                        if ($scope.filterTypes.indexOf(log.SensorType) < 0) {
50                          $scope.filterTypes.push(log.SensorType);
51                        }
52                      });
53                    },
54                    function(error) {
55                      console.log(JSON.stringify(error));
56                    })
57                .finally(function() {
58                  $scope.loading = false;
59                });
60          };
61
62          $scope.clearSystemLogEntries = function(selectedRecordType) {
63            $scope.confirm = false;
64            APIUtils.clearSystemLogs(selectedRecordType)
65                .then(
66                    function(res) {
67                      console.log(JSON.stringify(res));
68                    },
69                    function(error) {
70                      console.log(JSON.stringify(error));
71                    })
72                .finally(function() {
73                  $scope.selectRecordType($scope.selectedRecordType);
74                });
75          };
76
77          $scope.sortBy = function(keyname, isReverse) {
78            $scope.sortKey = keyname;
79            $scope.reverse = isReverse;
80          };
81
82          $scope.clear = function() {
83            $scope.customSearch = '';
84            $scope.searchTerms = [];
85          };
86
87          $scope.doSearchOnEnter = function(event) {
88            var search =
89                $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
90            if (event.keyCode === 13 && search.length >= 2) {
91              $scope.searchTerms = $scope.customSearch.split(' ');
92            } else {
93              if (search.length == 0) {
94                $scope.searchTerms = [];
95              }
96            }
97          };
98
99          $scope.doSearchOnClick = function() {
100            var search =
101                $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
102            if (search.length >= 2) {
103              $scope.searchTerms = $scope.customSearch.split(' ');
104            } else {
105              if (search.length == 0) {
106                $scope.searchTerms = [];
107              }
108            }
109          };
110
111          $scope.filterBySearchTerms = function(log) {
112            if (!$scope.searchTerms.length) {
113              return true;
114            }
115
116            for (var i = 0, length = $scope.searchTerms.length; i < length;
117                 i++) {
118              // TODO: Form it while getting data
119              var search_text = log.Id + ' ' + log.Name.toLowerCase() + ' ' +
120                  log.Message.toLowerCase();
121              if (search_text.indexOf($scope.searchTerms[i].toLowerCase()) ==
122                  -1)
123                return false;
124            }
125            return true;
126          };
127
128          $scope.filterBySeverity = function(log) {
129            if ($scope.selectedSeverityList.length == 0) {
130              return true;
131            }
132
133            return ($scope.selectedSeverityList.indexOf(log.Severity) > -1);
134          };
135
136          $scope.filterByType = function(log) {
137            if ($scope.selectedType == 'All') {
138              return true;
139            }
140
141            return (($scope.selectedType == log.SensorType));
142          };
143
144          $scope.filterByDate = function(log) {
145            var logDate = new Date(log.Created);
146            if ($scope.start_date && $scope.end_date) {
147              return (
148                  logDate >= $scope.start_date && logDate <= $scope.end_date);
149            } else {
150              return true;
151            }
152          };
153
154          setTimeout($scope.selectRecordType($scope.selectedRecordType), 2000);
155        }
156      ]);
157})(angular);
158