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
33          $scope.selectRecordType = function(recordType) {
34            $scope.selectedRecordType = recordType;
35            $scope.showLogDropdown = false;
36            APIUtils.getSystemLogs(recordType)
37                .then(
38                    function(res) {
39                      $scope.sysLogs = res;
40                      $scope.filterTypes.push('All');
41                      $scope.sysLogs.forEach(function(log) {
42                        if ($scope.filterTypes.indexOf(log.SensorType) < 0) {
43                          $scope.filterTypes.push(log.SensorType);
44                        }
45                      });
46                    },
47                    function(error) {
48                      console.log(JSON.stringify(error));
49                    })
50                .finally(function() {
51                  $scope.loading = false;
52                });
53          };
54
55          $scope.clearSystemLogEntries = function() {
56            $scope.confirm = false;
57            APIUtils.clearSystemLogs()
58                .then(
59                    function(res) {
60                      console.log(JSON.stringify(res));
61                    },
62                    function(error) {
63                      console.log(JSON.stringify(error));
64                    })
65                .finally(function() {
66                  $scope.selectRecordType($scope.selectedRecordType);
67                });
68          };
69
70          $scope.sortBy = function(keyname, isReverse) {
71            $scope.sortKey = keyname;
72            $scope.reverse = isReverse;
73          };
74
75          $scope.clear = function() {
76            $scope.customSearch = '';
77            $scope.searchTerms = [];
78          };
79
80          $scope.doSearchOnEnter = function(event) {
81            var search =
82                $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
83            if (event.keyCode === 13 && search.length >= 2) {
84              $scope.searchTerms = $scope.customSearch.split(' ');
85            } else {
86              if (search.length == 0) {
87                $scope.searchTerms = [];
88              }
89            }
90          };
91
92          $scope.doSearchOnClick = function() {
93            var search =
94                $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
95            if (search.length >= 2) {
96              $scope.searchTerms = $scope.customSearch.split(' ');
97            } else {
98              if (search.length == 0) {
99                $scope.searchTerms = [];
100              }
101            }
102          };
103
104          $scope.filterBySearchTerms = function(log) {
105            if (!$scope.searchTerms.length) return true;
106
107            for (var i = 0, length = $scope.searchTerms.length; i < length;
108                 i++) {
109              // TODO: Form it while getting data
110              var search_text = log.Id + ' ' + log.Name.toLowerCase() + ' ' +
111                  log.Message.toLowerCase();
112              if (search_text.indexOf($scope.searchTerms[i].toLowerCase()) ==
113                  -1)
114                return false;
115            }
116            return true;
117          };
118
119          setTimeout($scope.selectRecordType($scope.selectedRecordType), 2000);
120        }
121      ]);
122})(angular);
123