xref: /openbmc/phosphor-webui/app/server-health/controllers/log-controller.js (revision ee78862d71b818cffb15b30086fdc707490293c4)
18b4828a6SIftekharul Islam/**
28b4828a6SIftekharul Islam * Controller for log
38b4828a6SIftekharul Islam *
48b4828a6SIftekharul Islam * @module app/serverHealth
58b4828a6SIftekharul Islam * @exports logController
68b4828a6SIftekharul Islam * @name logController
78b4828a6SIftekharul Islam */
88b4828a6SIftekharul Islam
98b4828a6SIftekharul Islamwindow.angular && (function(angular) {
108b4828a6SIftekharul Islam  'use strict';
11d27bb135SAndrew Geissler  angular.module('app.serverHealth')
1226539864SEd Tanous      .config([
1326539864SEd Tanous        'paginationTemplateProvider',
1426539864SEd Tanous        function(paginationTemplateProvider) {
15d27bb135SAndrew Geissler          paginationTemplateProvider.setString(
16d27bb135SAndrew Geissler              require('../../common/directives/dirPagination.tpl.html'));
1726539864SEd Tanous        }
1826539864SEd Tanous      ])
198b4828a6SIftekharul Islam      .controller('logController', [
20d27bb135SAndrew Geissler        '$scope', '$window', 'APIUtils', 'dataService', 'Constants',
21d27bb135SAndrew Geissler        '$routeParams', '$filter',
22d27bb135SAndrew Geissler        function(
23d27bb135SAndrew Geissler            $scope, $window, APIUtils, dataService, Constants, $routeParams,
24d27bb135SAndrew Geissler            $filter) {
258b4828a6SIftekharul Islam          $scope.dataService = dataService;
268b4828a6SIftekharul Islam          $scope.logs = [];
27a4ae7dbaSGunnar Mills          $scope.filteredLogs = [];
28569ccf66Sbeccabroek          $scope.tmz = '';
298b4828a6SIftekharul Islam          $scope.itemsPerPage = Constants.PAGINATION.LOG_ITEMS_PER_PAGE;
30428375e8SMichael Davis          $scope.loading = false;
3196bbf310SIftekharul Islam          var expandedSelectedIdOnce = false;
32428375e8SMichael Davis
33428375e8SMichael Davis          var sensorType = $routeParams.type;
3496bbf310SIftekharul Islam          var eventId = $routeParams.id;
35428375e8SMichael Davis
368b4828a6SIftekharul Islam          // priority buttons
37d27bb135SAndrew Geissler          $scope.selectedSeverity =
38d27bb135SAndrew Geissler              {all: true, low: false, medium: false, high: false};
39428375e8SMichael Davis
40428375e8SMichael Davis          if (sensorType == 'high') {
41428375e8SMichael Davis            $scope.selectedSeverity.all = false;
42428375e8SMichael Davis            $scope.selectedSeverity.high = true;
43428375e8SMichael Davis          }
44428375e8SMichael Davis
45d27bb135SAndrew Geissler          $scope.selectedStatus = {all: true, resolved: false};
468b4828a6SIftekharul Islam
47ba5e3f34SAndrew Geissler          $scope.customSearch = '';
488b4828a6SIftekharul Islam          $scope.searchItems = [];
498b4828a6SIftekharul Islam          $scope.selectedEvents = [];
508b4828a6SIftekharul Islam
5196bbf310SIftekharul Islam          if (eventId) {
52ba5e3f34SAndrew Geissler            $scope.customSearch = '#' + eventId;
53ba5e3f34SAndrew Geissler            $scope.searchItems.push('#' + eventId);
5496bbf310SIftekharul Islam          }
5596bbf310SIftekharul Islam
568b4828a6SIftekharul Islam          $scope.loadLogs = function() {
57428375e8SMichael Davis            $scope.loading = true;
58428375e8SMichael Davis            APIUtils.getLogs().then(function(result) {
5996bbf310SIftekharul Islam              if (eventId && expandedSelectedIdOnce == false) {
6096bbf310SIftekharul Islam                var log = result.data.filter(function(item) {
6196bbf310SIftekharul Islam                  return item.Id == eventId;
6296bbf310SIftekharul Islam                });
6396bbf310SIftekharul Islam
6496bbf310SIftekharul Islam                if (log.length) {
6596bbf310SIftekharul Islam                  log[0].meta = true;
6696bbf310SIftekharul Islam                }
6796bbf310SIftekharul Islam                expandedSelectedIdOnce = true;
6896bbf310SIftekharul Islam              }
69c22425f2SIftekharul Islam              dataService.updateServerHealth(result.data);
70428375e8SMichael Davis              $scope.logs = result.data;
71428375e8SMichael Davis              $scope.originalData = result.original;
72428375e8SMichael Davis              $scope.loading = false;
738b4828a6SIftekharul Islam            });
748b4828a6SIftekharul Islam          };
758b4828a6SIftekharul Islam          $scope.jsonData = function(data) {
768b4828a6SIftekharul Islam            return JSON.stringify(data);
778b4828a6SIftekharul Islam          };
788b4828a6SIftekharul Islam
798b4828a6SIftekharul Islam          $scope.filterBySeverity = function(log) {
808b4828a6SIftekharul Islam            if ($scope.selectedSeverity.all) return true;
818b4828a6SIftekharul Islam
82d27bb135SAndrew Geissler            return (
83d27bb135SAndrew Geissler                (log.severity_flags.low && $scope.selectedSeverity.low) ||
848b4828a6SIftekharul Islam                (log.severity_flags.medium && $scope.selectedSeverity.medium) ||
85d27bb135SAndrew Geissler                (log.severity_flags.high && $scope.selectedSeverity.high));
86ba5e3f34SAndrew Geissler          };
878b4828a6SIftekharul Islam
888b4828a6SIftekharul Islam          $scope.filterByStatus = function(log) {
898b4828a6SIftekharul Islam            if ($scope.selectedStatus.all) return true;
90d27bb135SAndrew Geissler            return (
91d27bb135SAndrew Geissler                (log.Resolved && $scope.selectedStatus.resolved) ||
92d27bb135SAndrew Geissler                (!log.Resolved && !$scope.selectedStatus.resolved));
93ba5e3f34SAndrew Geissler          };
948b4828a6SIftekharul Islam
958b4828a6SIftekharul Islam          $scope.filterByDate = function(log) {
96857e78b1SIftekharul Islam            var endDate;
97d27bb135SAndrew Geissler            if ($scope.end_date &&
98d27bb135SAndrew Geissler                typeof $scope.end_date.getTime === 'function') {
99857e78b1SIftekharul Islam              endDate = new Date($scope.end_date.getTime());
100857e78b1SIftekharul Islam              endDate.setTime(endDate.getTime() + 86399000);
101857e78b1SIftekharul Islam            }
102857e78b1SIftekharul Islam
103857e78b1SIftekharul Islam            if ($scope.start_date && endDate) {
104dbf04811SAlexander Filippov              return (
105dbf04811SAlexander Filippov                  log.Timestamp >= $scope.start_date &&
106dbf04811SAlexander Filippov                  log.Timestamp <= endDate);
107d27bb135SAndrew Geissler            } else {
1088b4828a6SIftekharul Islam              return true;
1098b4828a6SIftekharul Islam            }
110ba5e3f34SAndrew Geissler          };
1118b4828a6SIftekharul Islam
1128b4828a6SIftekharul Islam          $scope.filterBySearchTerms = function(log) {
113*ee78862dSYoshie Muranaka            const searchableProperties = [
114*ee78862dSYoshie Muranaka              'eventID', 'severity_code', 'description', 'priority',
115*ee78862dSYoshie Muranaka              'additional_data', 'type', 'logId'
116*ee78862dSYoshie Muranaka            ];
117*ee78862dSYoshie Muranaka            let matched = false;
1188b4828a6SIftekharul Islam
119*ee78862dSYoshie Muranaka            if (!$scope.searchItems.length) return true;
120*ee78862dSYoshie Muranaka            for (const searchTerm of $scope.searchItems) {
121*ee78862dSYoshie Muranaka              if (matched) {
122*ee78862dSYoshie Muranaka                break;
1238b4828a6SIftekharul Islam              }
124*ee78862dSYoshie Muranaka              for (const prop of searchableProperties) {
125*ee78862dSYoshie Muranaka                const propVal = log[prop];
126*ee78862dSYoshie Muranaka                if (propVal &&
127*ee78862dSYoshie Muranaka                    propVal.toLowerCase().indexOf(searchTerm) !== -1) {
128*ee78862dSYoshie Muranaka                  matched = true;
129*ee78862dSYoshie Muranaka                  break;
130*ee78862dSYoshie Muranaka                }
131*ee78862dSYoshie Muranaka              }
132*ee78862dSYoshie Muranaka            }
133*ee78862dSYoshie Muranaka            return matched;
134ba5e3f34SAndrew Geissler          };
1358b4828a6SIftekharul Islam
1368b4828a6SIftekharul Islam          $scope.addSearchItem = function(searchTerms) {
137ba5e3f34SAndrew Geissler            var terms = searchTerms.split(' ');
1388b4828a6SIftekharul Islam            terms.forEach(function(searchTerm) {
1398b4828a6SIftekharul Islam              if ($scope.searchItems.indexOf(searchTerm) == -1) {
140*ee78862dSYoshie Muranaka                $scope.searchItems.push(searchTerm.toLowerCase());
1418b4828a6SIftekharul Islam              }
1428b4828a6SIftekharul Islam            });
143ba5e3f34SAndrew Geissler          };
1448b4828a6SIftekharul Islam
1458b4828a6SIftekharul Islam          $scope.clearSearchItem = function(searchTerm) {
1468b4828a6SIftekharul Islam            $scope.searchItems = [];
147ba5e3f34SAndrew Geissler          };
1488b4828a6SIftekharul Islam
1498b4828a6SIftekharul Islam          $scope.removeSearchItem = function(searchTerm) {
1508b4828a6SIftekharul Islam            var termIndex = $scope.searchItems.indexOf(searchTerm);
1518b4828a6SIftekharul Islam
1528b4828a6SIftekharul Islam            if (termIndex > -1) {
1538b4828a6SIftekharul Islam              $scope.searchItems.splice(termIndex, 1);
1548b4828a6SIftekharul Islam            }
155ba5e3f34SAndrew Geissler          };
1568b4828a6SIftekharul Islam
1578b4828a6SIftekharul Islam          $scope.$watch('all', function() {
1581ca1d7e1Sbeccabroek            $scope.filteredLogs.forEach(function(item) {
1598b4828a6SIftekharul Islam              item.selected = $scope.all;
1608b4828a6SIftekharul Islam            });
1618b4828a6SIftekharul Islam          });
1628b4828a6SIftekharul Islam
1638b4828a6SIftekharul Islam          function updateExportData() {
164d27bb135SAndrew Geissler            $scope.export_name = ($scope.selectedEvents.length == 1) ?
165d27bb135SAndrew Geissler                $scope.selectedEvents[0].Id + '.json' :
166d27bb135SAndrew Geissler                'export.json';
1678b4828a6SIftekharul Islam            var data = {};
1688b4828a6SIftekharul Islam            $scope.selectedEvents.forEach(function(item) {
1698b4828a6SIftekharul Islam              data[item.data.key] = item.data.value;
1708b4828a6SIftekharul Islam            });
1718b4828a6SIftekharul Islam            $scope.export_data = JSON.stringify(data);
1728b4828a6SIftekharul Islam          }
1738b4828a6SIftekharul Islam
174f2d74644SIftekharul Islam          $scope.accept = function() {
175f2d74644SIftekharul Islam            APIUtils.deleteLogs($scope.selectedEvents).then(function() {
176f2d74644SIftekharul Islam              $scope.confirm = false;
177f2d74644SIftekharul Islam              $scope.loadLogs();
178f2d74644SIftekharul Islam            });
179ba5e3f34SAndrew Geissler          };
180f2d74644SIftekharul Islam
181f2d74644SIftekharul Islam          $scope.resolve = function() {
182f2d74644SIftekharul Islam            var events = $scope.selectedEvents.filter(function(item) {
183f2d74644SIftekharul Islam              return item.Resolved != 1;
184f2d74644SIftekharul Islam            });
185f2d74644SIftekharul Islam
186f2d74644SIftekharul Islam            if (!events.length) return;
187f2d74644SIftekharul Islam
1887e48d081SGunnar Mills            APIUtils.resolveLogs(events).then(
1897e48d081SGunnar Mills                function(data) {
190f2d74644SIftekharul Islam                  events.forEach(function(item) {
191f2d74644SIftekharul Islam                    item.Resolved = 1;
192f2d74644SIftekharul Islam                  });
1937e48d081SGunnar Mills                },
1947e48d081SGunnar Mills                function(error) {
1957e48d081SGunnar Mills                  // TODO: Show error to user
1967e48d081SGunnar Mills                  console.log(JSON.stringify(error));
197f2d74644SIftekharul Islam                });
198ba5e3f34SAndrew Geissler          };
199f2d74644SIftekharul Islam
2008b4828a6SIftekharul Islam          $scope.$watch('logs', function() {
2018b4828a6SIftekharul Islam            $scope.selectedEvents = $scope.logs.filter(function(item) {
2028b4828a6SIftekharul Islam              return item.selected;
2038b4828a6SIftekharul Islam            });
2048b4828a6SIftekharul Islam            updateExportData();
2058b4828a6SIftekharul Islam          }, true);
2068b4828a6SIftekharul Islam
2078b4828a6SIftekharul Islam          $scope.loadLogs();
2088b4828a6SIftekharul Islam        }
209ba5e3f34SAndrew Geissler      ]);
2108b4828a6SIftekharul Islam})(angular);
211