diff --git a/app/serialization.py b/app/serialization.py index 3a6699f6b..dfe2f4bb6 100644 --- a/app/serialization.py +++ b/app/serialization.py @@ -163,7 +163,9 @@ def serialize_host( if "reporter" in fields: serialized_host["reporter"] = host.reporter if "per_reporter_staleness" in fields: - serialized_host["per_reporter_staleness"] = host.per_reporter_staleness + serialized_host["per_reporter_staleness"] = _serialize_per_reporter_staleness( + host, staleness, staleness_timestamps + ) if "stale_timestamp" in fields: serialized_host["stale_timestamp"] = stale_timestamp and _serialize_staleness_to_string(stale_timestamp) if "stale_warning_timestamp" in fields: @@ -407,3 +409,21 @@ def serialize_staleness_to_dict(staleness_obj) -> dict: "immutable_time_to_stale_warning": staleness_obj.immutable_time_to_stale_warning, "immutable_time_to_delete": staleness_obj.immutable_time_to_delete, } + + +def _serialize_per_reporter_staleness(host, staleness, staleness_timestamps): + for reporter in host.per_reporter_staleness: + if host.system_profile_facts.get("host_type") == "edge": + stale_timestamp = staleness_timestamps.stale_timestamp( + _deserialize_datetime(host.per_reporter_staleness[reporter]["last_check_in"]), + staleness["immutable_time_to_stale"], + ) + else: + stale_timestamp = staleness_timestamps.stale_timestamp( + _deserialize_datetime(host.per_reporter_staleness[reporter]["last_check_in"]), + staleness["conventional_time_to_stale"], + ) + + host.per_reporter_staleness[reporter]["stale_timestamp"] = _serialize_staleness_to_string(stale_timestamp) + + return host.per_reporter_staleness diff --git a/tests/fixtures/mq_fixtures.py b/tests/fixtures/mq_fixtures.py index fd2f230e4..88459fe4e 100644 --- a/tests/fixtures/mq_fixtures.py +++ b/tests/fixtures/mq_fixtures.py @@ -1,5 +1,6 @@ import json from datetime import datetime +from datetime import timezone from unittest.mock import patch import pytest @@ -87,7 +88,9 @@ def mq_create_hosts_in_all_states(mq_create_or_update_host): @pytest.fixture(scope="function") def mq_create_deleted_hosts(mq_create_or_update_host): with patch("app.models.datetime") as mock_datetime: - mock_datetime.now.return_value = datetime(year=2023, month=4, day=2, hour=1, minute=1, second=1) + mock_datetime.now.return_value = datetime( + year=2023, month=4, day=2, hour=1, minute=1, second=1, tzinfo=timezone.utc + ) mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw) staleness_timestamps = get_staleness_timestamps() diff --git a/tests/test_xjoin.py b/tests/test_xjoin.py index c47572e9e..1df33459c 100644 --- a/tests/test_xjoin.py +++ b/tests/test_xjoin.py @@ -881,7 +881,7 @@ def test_response_processed_properly(graphql_query_with_response, api_get): "puptoo": { "check_in_succeeded": True, "last_check_in": "2020-02-10T08:07:03.354307+00:00", - "stale_timestamp": "2020-02-10T08:07:03.354307+00:00", + "stale_timestamp": "2020-02-11T13:07:03.354307+00:00", } }, "subscription_manager_id": None, @@ -912,7 +912,7 @@ def test_response_processed_properly(graphql_query_with_response, api_get): "yupana": { "check_in_succeeded": True, "last_check_in": "2020-02-10T08:07:03.354307+00:00", - "stale_timestamp": "2020-02-10T08:07:03.354307+00:00", + "stale_timestamp": "2020-02-11T13:07:03.354307+00:00", } }, "subscription_manager_id": None,