From 24312e183a097e06dba9aa466667e4a2b4580987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20=C3=81ngel=20Gonz=C3=A1lez?= Date: Tue, 12 Jun 2018 14:46:01 +0200 Subject: [PATCH] Modularized syscheck and rootcheck validations --- public/controllers/agents.js | 39 ++++++---------------------------- public/services/common-data.js | 18 ++++++++++++++++ 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/public/controllers/agents.js b/public/controllers/agents.js index b6d58ad54..7ff532971 100644 --- a/public/controllers/agents.js +++ b/public/controllers/agents.js @@ -152,47 +152,20 @@ app.controller('agentsController', function ($timeout, $scope, $location, $rootS }; // Agent data - $scope.getAgentStatusClass = (agentStatus) => agentStatus === "Active" ? "teal" : "red"; + $scope.getAgentStatusClass = agentStatus => agentStatus === "Active" ? "teal" : "red"; - $scope.formatAgentStatus = (agentStatus) => { + $scope.formatAgentStatus = agentStatus => { return ['Active','Disconnected'].includes(agentStatus) ? agentStatus : 'Never connected'; }; const validateRootCheck = () => { - $scope.agent.rootcheck.duration = 'Unknown'; - if ($scope.agent.rootcheck.end && $scope.agent.rootcheck.start) { - $scope.agent.rootcheck.duration = ((new Date($scope.agent.rootcheck.end) - new Date($scope.agent.rootcheck.start))/1000)/60; - $scope.agent.rootcheck.duration = Math.round($scope.agent.rootcheck.duration * 100) / 100; - - if($scope.agent.rootcheck.duration <= 0){ - $scope.agent.rootcheck.inProgress = true; - } - } else { - if (!$scope.agent.rootcheck.end) { - $scope.agent.rootcheck.end = 'Unknown'; - } - if (!$scope.agent.rootcheck.start){ - $scope.agent.rootcheck.start = 'Unknown'; - } - } + const result = commonData.validateRange($scope.agent.rootcheck) + $scope.agent.rootcheck = result; } const validateSysCheck = () => { - $scope.agent.syscheck.duration = 'Unknown'; - if ($scope.agent.syscheck.end && $scope.agent.syscheck.start) { - $scope.agent.syscheck.duration = ((new Date($scope.agent.syscheck.end) - new Date($scope.agent.syscheck.start))/1000)/60; - $scope.agent.syscheck.duration = Math.round($scope.agent.syscheck.duration * 100) / 100; - if($scope.agent.syscheck.duration <= 0){ - $scope.agent.syscheck.inProgress = true; - } - } else { - if (!$scope.agent.syscheck.end) { - $scope.agent.syscheck.end = 'Unknown'; - } - if (!$scope.agent.syscheck.start){ - $scope.agent.syscheck.start = 'Unknown'; - } - } + const result = commonData.validateRange($scope.agent.syscheck) + $scope.agent.syscheck = result; } $scope.getAgent = async (newAgentId,fromAutocomplete) => { diff --git a/public/services/common-data.js b/public/services/common-data.js index a2560d863..2ab0037a2 100644 --- a/public/services/common-data.js +++ b/public/services/common-data.js @@ -83,6 +83,24 @@ app.service('commonData', function ($rootScope, $timeout, genericReq, appState, } catch(error) { errorHandler.handle('An error occurred while creating custom filters for visualizations',agent ? 'Agents' : 'Overview',true); } + }, + validateRange: data => { + const result = { + duration : 'Unknown', + inProgress: false, + end : data.end || 'Unknown', + start : data.start || 'Unknown' + } + + if(data.end && data.start) { + result.duration = ((new Date(data.end) - new Date(data.start))/1000)/60; + result.duration = Math.round(result.duration * 100) / 100; + if(result.duration <= 0){ + result.inProgress = true; + } + } + + return result; } } }); \ No newline at end of file