Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #269 from jjnicola/int-progress
Browse files Browse the repository at this point in the history
Show progress as int
  • Loading branch information
jjnicola committed May 13, 2020
2 parents 6786774 + 562300f commit 4bfffd0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
also ospd in editable mode. This means after running poetry install ospd will
directly be importable in the virtual python environment. [#252](https://github.com/greenbone/ospd/pull/252)
- Progress bar calculation does not take in account the dead hosts. [#266](https://github.com/greenbone/ospd/pull/266)
- Show progress as integer for get_scans. [#269](https://github.com/greenbone/ospd/pull/269)

### Fixed
- Fix stop scan. Wait for the scan process to be stopped before delete it from the process table. [#204](https://github.com/greenbone/ospd/pull/204)
Expand Down
20 changes: 12 additions & 8 deletions ospd/ospd.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,6 @@ def handle_client_stream(self, stream: Stream) -> None:

stream.close()

def calculate_progress(self, scan_id: str) -> float:
""" Calculate the total scan progress. """

return self.scan_collection.calculate_target_progress(scan_id)

def process_finished_hosts(self, scan_id: str, finished_hosts: str) -> None:
""" Process the finished hosts before launching the scans."""

Expand Down Expand Up @@ -606,7 +601,7 @@ def set_scan_progress_batch(
):
self.scan_collection.set_host_progress(scan_id, host_progress)

scan_progress = self.calculate_progress(scan_id)
scan_progress = self.scan_collection.calculate_target_progress(scan_id)
self.scan_collection.set_progress(scan_id, scan_progress)

def set_scan_host_progress(
Expand All @@ -616,6 +611,15 @@ def set_scan_host_progress(
Each time a host progress is updated, the scan progress
is updated too.
"""
if host is None or progress is None:
return

if not isinstance(progress, int):
try:
progress = int(progress)
except (TypeError, ValueError):
return

host_progress = {host: progress}
self.set_scan_progress_batch(scan_id, host_progress)

Expand Down Expand Up @@ -713,7 +717,7 @@ def _get_scan_progress_xml(self, scan_id: str):
current_progress[
'current_hosts'
] = self.scan_collection.get_current_target_progress(scan_id)
current_progress['overall'] = self.scan_collection.get_progress(scan_id)
current_progress['overall'] = self.get_scan_progress(scan_id)
current_progress['count_alive'] = self.scan_collection.get_count_alive(
scan_id
)
Expand Down Expand Up @@ -1266,7 +1270,7 @@ def check_scan_process(self, scan_id: str) -> None:
elif progress == PROGRESS_FINISHED:
scan_process.join(0)

def get_scan_progress(self, scan_id: str):
def get_scan_progress(self, scan_id: str) -> int:
""" Gives a scan's current progress value. """
return self.scan_collection.get_progress(scan_id)

Expand Down
15 changes: 8 additions & 7 deletions ospd/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def set_host_progress(
self, scan_id: str, host_progress_batch: Dict[str, int]
) -> None:
""" Sets scan_id scan's progress. """

host_progresses = self.scans_table[scan_id].get('target_progress')
host_progresses.update(host_progress_batch)

Expand Down Expand Up @@ -272,8 +273,7 @@ def get_count_alive(self, scan_id: str) -> int:
return self.scans_table[scan_id]['count_alive']

def get_current_target_progress(self, scan_id: str) -> Dict[str, int]:
""" Get a scan's current dead host count. """

""" Get a scan's current hosts progress """
return self.scans_table[scan_id]['target_progress']

def simplify_exclude_host_count(self, scan_id: str) -> int:
Expand All @@ -299,7 +299,7 @@ def simplify_exclude_host_count(self, scan_id: str) -> int:

return len(exc_hosts_list) if exc_hosts_list else 0

def calculate_target_progress(self, scan_id: str) -> float:
def calculate_target_progress(self, scan_id: str) -> int:
""" Get a target's current progress value.
The value is calculated with the progress of each single host
in the target."""
Expand All @@ -308,12 +308,13 @@ def calculate_target_progress(self, scan_id: str) -> float:
exc_hosts = self.simplify_exclude_host_count(scan_id)
count_alive = self.get_count_alive(scan_id)
count_dead = self.get_count_dead(scan_id)
host_progresses = self.scans_table[scan_id].get('target_progress')
host_progresses = self.get_current_target_progress(scan_id)

try:
t_prog = (sum(host_progresses.values()) + 100 * count_alive) / (
total_hosts - exc_hosts - count_dead
) # type: float
t_prog = int(
(sum(host_progresses.values()) + 100 * count_alive)
/ (total_hosts - exc_hosts - count_dead)
)
except ZeroDivisionError:
LOGGER.error(
"Zero division error in %s",
Expand Down
40 changes: 38 additions & 2 deletions tests/test_scan_and_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,9 @@ def test_progress(self):
daemon.set_scan_host_progress(scan_id, 'localhost1', 75)
daemon.set_scan_host_progress(scan_id, 'localhost2', 25)

self.assertEqual(daemon.calculate_progress(scan_id), 50)
self.assertEqual(
daemon.scan_collection.calculate_target_progress(scan_id), 50
)

def test_sort_host_finished(self):
daemon = DummyWrapper([])
Expand All @@ -838,9 +840,43 @@ def test_sort_host_finished(self):

daemon.sort_host_finished(scan_id, ['localhost3', 'localhost4'])

rounded_progress = int(daemon.calculate_progress(scan_id))
rounded_progress = daemon.scan_collection.calculate_target_progress(
scan_id
)
self.assertEqual(rounded_progress, 66)

def test_calculate_progress_without_current_hosts(self):
daemon = DummyWrapper([])

fs = FakeStream()
daemon.handle_command(
'<start_scan parallel="2">'
'<scanner_params />'
'<targets><target>'
'<hosts>localhost1, localhost2, localhost3, localhost4</hosts>'
'<ports>22</ports>'
'</target></targets>'
'</start_scan>',
fs,
)
response = fs.get_response()

scan_id = response.findtext('id')
daemon.set_scan_host_progress(scan_id)
daemon.set_scan_host_progress(scan_id, 'localhost3', -1)
daemon.set_scan_host_progress(scan_id, 'localhost4', 100)

daemon.sort_host_finished(scan_id, ['localhost3', 'localhost4'])

float_progress = daemon.scan_collection.calculate_target_progress(
scan_id
)
self.assertEqual(int(float_progress), 33)

daemon.scan_collection.set_progress(scan_id, float_progress)
progress = daemon.get_scan_progress(scan_id)
self.assertEqual(progress, 33)

def test_get_scan_progress_xml(self):
daemon = DummyWrapper([])

Expand Down

0 comments on commit 4bfffd0

Please sign in to comment.