Skip to content

Commit

Permalink
feat(RHINENG-2631): Support new reporter values for yupana (RedHatIns…
Browse files Browse the repository at this point in the history
  • Loading branch information
roliveri authored and thearifismail committed Dec 5, 2023
1 parent b9aa250 commit 4376ef7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/filtering/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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": {
Expand Down
8 changes: 8 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
36 changes: 36 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_xjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -490,6 +491,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": {
Expand Down

0 comments on commit 4376ef7

Please sign in to comment.