Skip to content

Commit

Permalink
feat: add alive_alert_info to DAQJobHealthcheck and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
furkan-bilgin committed Nov 26, 2024
1 parent 82228a4 commit 4fbc6ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/enrgdaq/daq/jobs/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ class AlertCondition(str, Enum):
UNSATISFIED = "unsatisfied"


class HealthcheckItem(Struct):
"""Represents a healthcheck item with alert information."""
class HealthcheckItem(Struct, kw_only=True):
"""Represents a healthcheck item with alert information.
Attributes:
alert_info (DAQAlertInfo): The alert information.
alive_alert_info (Optional[DAQAlertInfo]): The alert information for when the item gets back alive (after being down).
"""

alert_info: DAQAlertInfo
alive_alert_info: Optional[DAQAlertInfo] = None


class HealthcheckStatsItem(HealthcheckItem):
Expand Down Expand Up @@ -211,14 +217,16 @@ def handle_checks(self):
item_id = hash(msgspec.json.encode(item))
if should_alert and item_id not in self._sent_alert_items:
self._sent_alert_items.add(item_id)
self._send_alert(item)
self._send_alert(item.alert_info)
elif not should_alert and item_id in self._sent_alert_items:
self._sent_alert_items.remove(item_id)
if item.alive_alert_info:
self._send_alert(item.alive_alert_info)

def _send_alert(self, item: HealthcheckItem):
def _send_alert(self, alert_info: DAQAlertInfo):
self._put_message_out(
DAQJobMessageAlert(
date=datetime.now(),
alert_info=item.alert_info,
alert_info=alert_info,
)
)
8 changes: 4 additions & 4 deletions src/tests/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_handle_checks_should_alert(self, mock_send_alert):

self.daq_job_healthcheck.handle_checks()

mock_send_alert.assert_called_once_with(self.healthcheck_item)
mock_send_alert.assert_called_once_with(self.healthcheck_item.alert_info)

@patch("enrgdaq.daq.jobs.healthcheck.DAQJobHealthcheck._send_alert")
def test_handle_checks_should_not_alert(self, mock_send_alert):
Expand All @@ -63,7 +63,7 @@ def test_handle_checks_should_not_alert_twice_in_a_row(self, mock_send_alert):

# First check should trigger an alert
self.daq_job_healthcheck.handle_checks()
mock_send_alert.assert_called_once_with(self.healthcheck_item)
mock_send_alert.assert_called_once_with(self.healthcheck_item.alert_info)

# Reset mock to check for second call
mock_send_alert.reset_mock()
Expand All @@ -84,7 +84,7 @@ def test_handle_checks_should_alert_then_not_alert_then_alert_again(

# First check should trigger an alert
self.daq_job_healthcheck.handle_checks()
mock_send_alert.assert_called_once_with(self.healthcheck_item)
mock_send_alert.assert_called_once_with(self.healthcheck_item.alert_info)

# Reset mock to check for second call
mock_send_alert.reset_mock()
Expand All @@ -99,7 +99,7 @@ def test_handle_checks_should_alert_then_not_alert_then_alert_again(
minutes=10
)
self.daq_job_healthcheck.handle_checks()
mock_send_alert.assert_called_once_with(self.healthcheck_item)
mock_send_alert.assert_called_once_with(self.healthcheck_item.alert_info)

def test_parse_interval(self):
self.assertEqual(self.healthcheck_item.parse_interval(), timedelta(minutes=5))
Expand Down

0 comments on commit 4fbc6ce

Please sign in to comment.