Add tracking and formatting for last success in alerts

Relates to #2
This commit is contained in:
IamTheFij 2018-04-13 22:02:03 -07:00
parent 47a8bdf4a0
commit d005129afe
2 changed files with 23 additions and 3 deletions

View File

@ -123,6 +123,7 @@ class Monitor(object):
self.alert_every = settings.get('alert_every') self.alert_every = settings.get('alert_every')
self.last_check = None self.last_check = None
self.last_success = None
self.total_failure_count = 0 self.total_failure_count = 0
self.alert_count = 0 self.alert_count = 0
@ -163,6 +164,7 @@ class Monitor(object):
"""Handles success tasks""" """Handles success tasks"""
self.total_failure_count = 0 self.total_failure_count = 0
self.alert_count = 0 self.alert_count = 0
self.last_success = datetime.now()
def failure(self): def failure(self):
"""Handles failure tasks and possibly raises MinitorAlert""" """Handles failure tasks and possibly raises MinitorAlert"""
@ -206,6 +208,12 @@ class Alert(object):
args.append(arg.format(**kwargs)) args.append(arg.format(**kwargs))
return args return args
def _format_datetime(self, dt):
"""Formats a datetime for an alert"""
if dt is None:
return 'Never'
return dt.isoformat()
def alert(self, monitor): def alert(self, monitor):
"""Calls the alert command for the provided monitor""" """Calls the alert command for the provided monitor"""
output, ex = call_output( output, ex = call_output(
@ -213,6 +221,7 @@ class Alert(object):
alert_count=monitor.alert_count, alert_count=monitor.alert_count,
monitor_name=monitor.name, monitor_name=monitor.name,
failure_count=monitor.total_failure_count, failure_count=monitor.total_failure_count,
last_success=self._format_datetime(monitor.last_success),
), ),
shell=isinstance(self.command, str), shell=isinstance(self.command, str),
) )

View File

@ -1,3 +1,4 @@
from datetime import datetime
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -23,18 +24,28 @@ class TestAlert(object):
'command': [ 'command': [
'echo', ( 'echo', (
'{monitor_name} has failed {failure_count} time(s)!\n' '{monitor_name} has failed {failure_count} time(s)!\n'
'We have alerted {alert_count} time(s)' 'We have alerted {alert_count} time(s)\n'
'Last success was {last_success}'
) )
] ]
} }
) )
def test_simple_alert(self, monitor, echo_alert): @pytest.mark.parametrize(
'last_success',
[
(None, 'Never'),
(datetime(2018, 4, 10), '2018-04-10T00:00:00')
]
)
def test_simple_alert(self, monitor, echo_alert, last_success):
monitor.total_failure_count = 1 monitor.total_failure_count = 1
monitor.alert_count = 1 monitor.alert_count = 1
monitor.last_success = last_success[0]
with patch.object(echo_alert.logger, 'error') as mock_error: with patch.object(echo_alert.logger, 'error') as mock_error:
echo_alert.alert(monitor) echo_alert.alert(monitor)
mock_error.assert_called_once_with( mock_error.assert_called_once_with(
'Dummy Monitor has failed 1 time(s)!\n' 'Dummy Monitor has failed 1 time(s)!\n'
'We have alerted 1 time(s)' 'We have alerted 1 time(s)\n'
'Last success was ' + last_success[1]
) )