From b8627bd9a9adf32bb95498809ed7303294da414d Mon Sep 17 00:00:00 2001 From: Richard Oliveri Date: Fri, 17 Nov 2023 15:42:47 -0500 Subject: [PATCH] feat(RHINENG-2631): Support new reporter values for yupana --- api/filtering/filtering.py | 7 +++++++ app/models.py | 8 ++++++++ tests/test_models.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_xjoin.py | 5 +++++ 4 files changed, 56 insertions(+) diff --git a/api/filtering/filtering.py b/api/filtering/filtering.py index 94a2270f8e..0f3910897b 100644 --- a/api/filtering/filtering.py +++ b/api/filtering/filtering.py @@ -19,6 +19,7 @@ from app.auth.identity import IdentityType from app.exceptions import ValidationException from app.logging import get_logger +from app.models import OLD_TO_NEW_REPORTER_MAP from app.utils import Tag from app.validators import is_custom_date as is_timestamp from app.xjoin import staleness_filter @@ -288,6 +289,12 @@ def build_registered_with_filter(registered_with): prs_list.append({"NOT": {"insights_id": {"eq": None}}}) reg_with_copy.remove("insights") if reg_with_copy: + # When filtering on old reporter name, include the names of the + # new reporters associated with the old reporter. + for old_reporter in OLD_TO_NEW_REPORTER_MAP: + if old_reporter in reg_with_copy: + reg_with_copy.extend(OLD_TO_NEW_REPORTER_MAP[old_reporter]) + reg_with_copy = list(set(reg_with_copy)) # Remove duplicates for item in reg_with_copy: prs_item = { "per_reporter_staleness": { diff --git a/app/models.py b/app/models.py index 763faecd08..668d6f3b17 100644 --- a/app/models.py +++ b/app/models.py @@ -55,6 +55,11 @@ # set edge host stale_timestamp way out in future to Year 2260 EDGE_HOST_STALE_TIMESTAMP = datetime(2260, 1, 1, tzinfo=timezone.utc) +# Used when updating per_reporter_staleness from old to new keys. +NEW_TO_OLD_REPORTER_MAP = {"satellite": "yupana", "discovery": "yupana"} +# Used in filtering. +OLD_TO_NEW_REPORTER_MAP = {"yupana": ("satellite", "discovery")} + class ProviderType(str, Enum): ALIBABA = "alibaba" @@ -322,6 +327,9 @@ def _update_per_reporter_staleness(self, reporter): if not self.per_reporter_staleness.get(reporter): self.per_reporter_staleness[reporter] = {} + if old_reporter := NEW_TO_OLD_REPORTER_MAP.get(reporter): + self.per_reporter_staleness.pop(old_reporter, None) + self.per_reporter_staleness[reporter].update( stale_timestamp=self.stale_timestamp.isoformat(), last_check_in=datetime.now(timezone.utc).isoformat(), diff --git a/tests/test_models.py b/tests/test_models.py index 139364f88f..e017cf297d 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -479,6 +479,42 @@ def test_update_per_reporter_staleness(db_create_host, models_datetime_mock): } +@pytest.mark.parametrize( + "new_reporter", + ["satellite", "discovery"], +) +def test_update_per_reporter_staleness_yupana_replacement(db_create_host, models_datetime_mock, new_reporter): + yupana_stale_timestamp = models_datetime_mock + timedelta(days=1) + input_host = Host( + {"fqdn": "fqdn"}, display_name="display_name", reporter="yupana", stale_timestamp=yupana_stale_timestamp + ) + existing_host = db_create_host(host=input_host) + + assert existing_host.per_reporter_staleness == { + "yupana": { + "last_check_in": models_datetime_mock.isoformat(), + "stale_timestamp": yupana_stale_timestamp.isoformat(), + "check_in_succeeded": True, + } + } + + yupana_stale_timestamp += timedelta(days=1) + + update_host = Host( + {"fqdn": "fqdn"}, display_name="display_name", reporter=new_reporter, stale_timestamp=yupana_stale_timestamp + ) + existing_host.update(update_host) + + # datetime will not change because the datetime.now() method is patched + assert existing_host.per_reporter_staleness == { + new_reporter: { + "last_check_in": models_datetime_mock.isoformat(), + "stale_timestamp": yupana_stale_timestamp.isoformat(), + "check_in_succeeded": True, + } + } + + @pytest.mark.parametrize( "provider", ( diff --git a/tests/test_xjoin.py b/tests/test_xjoin.py index 3a1c7c4912..4b6c6b6c87 100644 --- a/tests/test_xjoin.py +++ b/tests/test_xjoin.py @@ -10,6 +10,7 @@ from api.system_profile import SAP_SYSTEM_QUERY from api.tag import TAGS_QUERY from app import process_spec +from app.models import OLD_TO_NEW_REPORTER_MAP from app.models import ProviderType from tests.helpers.api_utils import build_hosts_url from tests.helpers.api_utils import build_system_profile_sap_sids_url @@ -488,6 +489,10 @@ def test_query_variables_tags_with_search(field, mocker, graphql_query_empty_res # Build the expected PRS filter based on reporters def _build_prs_array(mocker, reporters): prs_array = [] + for old_reporter in OLD_TO_NEW_REPORTER_MAP: + if old_reporter in reporters: + reporters.extend(OLD_TO_NEW_REPORTER_MAP[old_reporter]) + reporters = list(set(reporters)) # Remove duplicates for reporter in reporters: prs_item = { "per_reporter_staleness": {