mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-23 20:42:02 +00:00
feat(scan): add scan duration (#5305)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Hugo Pereira Brito <101209179+HugoPBrito@users.noreply.github.com> Co-authored-by: Sergio <sergio@prowler.com> Co-authored-by: Prowler Bot <bot@prowler.com> Co-authored-by: sergargar <38561120+sergargar@users.noreply.github.com> Co-authored-by: Rubén De la Torre Vico <rubendltv22@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Pepe Fagoaga <pepe@prowler.com> Co-authored-by: Daniel Barranquero <74871504+danibarranqueroo@users.noreply.github.com>
This commit is contained in:
@@ -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}"
|
||||
|
||||
@@ -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"},
|
||||
|
||||
Reference in New Issue
Block a user