Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Send less updates to Toucan (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik authored Sep 28, 2017
1 parent 7d95d8b commit e5f8122
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
27 changes: 18 additions & 9 deletions scans/scan_async_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def previous_scan(self):
"""

return croniter(self._scan_cron(), time.time()).get_prev()
return int(croniter(self._scan_cron(), time.time()).get_prev())

@property
def next_scan(self):
Expand All @@ -194,7 +194,7 @@ def next_scan(self):
float
"""
return croniter(self._scan_cron(), time.time()).get_next()
return int(croniter(self._scan_cron(), time.time()).get_next())

def _scan_interval(self):
"""
Expand Down Expand Up @@ -260,30 +260,39 @@ async def update_scan_status(self, status=None):
None
"""
if not cfg.toucan:
if not cfg.toucan or cfg['portdetection.{name}.scan_type'.format(name=self.NAME)] == ScanType.LIVE.value:
return

current_status = cfg.get('portdetection.{0}.status.*'.format(self.NAME), cache=False)

data = {
'portdetection': {
self.NAME: {
'status': {
'next_scan_start': self.next_scan,
}
}
}
}

log.debug("Current status for %s is %s", self.NAME, current_status.cfg)
next_scan = round(current_status['next_scan_start'])
if next_scan != self.next_scan:
data['portdetection'][self.NAME]['status']['next_scan_start'] = self.next_scan

if self.scan_start:
previous_scan_start = cfg['portdetection.{0}.status.scan_start'.format(self.NAME)]
previous_scan_start = current_status['scan_start']
if previous_scan_start != self.scan_start:
data['portdetection'][self.NAME]['status']['previous_scan_start'] = previous_scan_start
data['portdetection'][self.NAME]['status']['scan_start'] = self.scan_start
data['portdetection'][self.NAME]['status']['scan_start'] = self.scan_start

if status is not None:
data['portdetection'][self.NAME]['status']['code'] = status.value
log.debug('Updating status of %s to %s', self.NAME, status.value)
current_status_code = current_status['code']
if current_status_code != status.value:
data['portdetection'][self.NAME]['status']['code'] = status.value

if status is ScanStatus.IDLE and self.scan_start is not None:
data['portdetection'][self.NAME]['status']['previous_scan_duration'] = int(time.time() - self.scan_start)

await cfg.toucan.push_config(data, overwrite=True)
if data['portdetection'][self.NAME]['status']:
log.debug("Update toucan by %s with %s", self.NAME, data)
await cfg.toucan.push_config(data, overwrite=True)
10 changes: 8 additions & 2 deletions tests/test_scans/test_scan_async_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,10 @@ async def test_update_scan_status_to_in_progress(self, cfg):
cfg.toucan.push_config.return_value.set_result(MagicMock())

cfg.toucan.get.return_value = Future()
cfg.toucan.get.return_value.set_result({'portdetection.test_name.status.scan_start': 23})
cfg.toucan.get.return_value.set_result({'portdetection.test_name.status.scan_start': 23,
'portdetection.test_name.status.next_scan_start': 23,
'portdetection.test_name.status.code': 'IDLE',
'portdetection.test_name.scan_type': 'PERIODIC'})

self.thread.scan_start = 17
self.thread.NAME = 'test_name'
Expand Down Expand Up @@ -584,7 +587,10 @@ async def test_update_scan_status_to_idle(self, cfg):
cfg.toucan.push_config.return_value.set_result(MagicMock())

cfg.toucan.get.return_value = Future()
cfg.toucan.get.return_value.set_result({'portdetection.test_name.status.scan_start': 23})
cfg.toucan.get.return_value.set_result({'portdetection.test_name.status.scan_start': 23,
'portdetection.test_name.status.next_scan_start': 23,
'portdetection.test_name.status.code': 23,
'portdetection.test_name.scan_type': 'PERIODIC'})

self.thread.scan_start = 17
self.thread.NAME = 'test_name'
Expand Down
3 changes: 2 additions & 1 deletion tests/test_utils/test_async_crontab_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ def test_stopping_cron_loop(self):
yield self.task()
self.assertFalse(self.task._prepare_next_iteration.called)

@patch('utils.async_crontab_task.time.time', MagicMock(return_value=305))
def test_prepare_next_iteration(self):
self.task._is_running = True
self.task._prepare_next_iteration()

self.assertFalse(self.task._is_running)
self.task._loop.call_later.assert_called_once_with(1, self.task)
self.task._loop.call_later.assert_called_once_with(55, self.task)

def test_cron_callable(self):
self.task._cron = MagicMock()
Expand Down
2 changes: 1 addition & 1 deletion utils/async_crontab_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ def stop(self):

def _prepare_next_iteration(self):
self._is_running = False
self._loop.call_later(1, self)
self._loop.call_later(60 - int(time.time()) % 60, self)
4 changes: 2 additions & 2 deletions utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __contains__(self, item):
return item in self._cfg
return False

def get(self, key):
def get(self, key, cache=True):
"""
Get configuration value basing on key.
Expand All @@ -62,7 +62,7 @@ def get(self, key):
return_value = self._get(key)

elif self.toucan:
if key in self.timestamps and self.timestamps[key] + self.cache_time > time.time():
if cache and key in self.timestamps and self.timestamps[key] + self.cache_time > time.time():
return_value = self._get(key)

elif self.toucan.is_special(key):
Expand Down

0 comments on commit e5f8122

Please sign in to comment.