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