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"},