From 41ba118cc48059cb4dac128e99c006274fd442de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Mart=C3=ADn?= Date: Wed, 9 Oct 2024 11:12:39 +0200 Subject: [PATCH] feat(scan): add scan duration (#5305) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dependabot[bot] Co-authored-by: Hugo Pereira Brito <101209179+HugoPBrito@users.noreply.github.com> Co-authored-by: Sergio Co-authored-by: Prowler Bot Co-authored-by: sergargar <38561120+sergargar@users.noreply.github.com> Co-authored-by: Rubén De la Torre Vico Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Pepe Fagoaga Co-authored-by: Daniel Barranquero <74871504+danibarranqueroo@users.noreply.github.com> --- prowler/lib/scan/scan.py | 11 ++++++++++- tests/lib/scan/scan_test.py | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/prowler/lib/scan/scan.py b/prowler/lib/scan/scan.py index 48cd9335fb..4de9ca5d4e 100644 --- a/prowler/lib/scan/scan.py +++ b/prowler/lib/scan/scan.py @@ -1,3 +1,4 @@ +import datetime from typing import Generator from prowler.lib.check.check import execute, import_check, update_audit_metadata @@ -18,6 +19,7 @@ class Scan: _service_checks_completed: dict[str, set[str]] _progress: float = 0.0 _findings: list = [] + _duration: int = 0 def __init__(self, provider: Provider, checks_to_execute: list[str]): """ @@ -61,6 +63,10 @@ class Scan: self._number_of_checks_completed / self._number_of_checks_to_execute * 100 ) + @property + def duration(self) -> int: + return self._duration + @property def findings(self) -> list: return self._findings @@ -95,6 +101,8 @@ class Scan: audit_progress=0, ) + start_time = datetime.datetime.now() + for check_name in checks_to_execute: try: # Recover service from check name @@ -149,7 +157,6 @@ class Scan: ] yield self.progress, findings - # If check does not exists in the provider or is from another provider except ModuleNotFoundError: logger.error( @@ -159,6 +166,8 @@ class Scan: logger.error( f"{check_name} - {error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" ) + # Update the scan duration when all checks are completed + self._duration = int((datetime.datetime.now() - start_time).total_seconds()) except Exception as error: logger.error( f"{check_name} - {error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" diff --git a/tests/lib/scan/scan_test.py b/tests/lib/scan/scan_test.py index 2ccf92f57e..0efe631429 100644 --- a/tests/lib/scan/scan_test.py +++ b/tests/lib/scan/scan_test.py @@ -201,6 +201,7 @@ class TestScan: ) assert scan.service_checks_completed == {} assert scan.progress == 0 + assert scan.duration == 0 assert scan.get_completed_services() == set() assert scan.get_completed_checks() == set() @@ -235,6 +236,8 @@ class TestScan: assert results[0][1] == mock_execute.side_effect() assert results[0][0] == 100.0 assert scan.progress == 100.0 + # Since the scan is mocked, the duration will always be 0 for now + assert scan.duration == 0 assert scan._number_of_checks_completed == 1 assert scan.service_checks_completed == { "accessanalyzer": {"accessanalyzer_enabled"},