From 34e5c775e0c7f61bb41cea570405d3b0e2447f8e Mon Sep 17 00:00:00 2001 From: Andy Clyde Date: Wed, 9 Oct 2024 13:33:35 +0100 Subject: [PATCH] Ensure we extend memberships where necessary --- CHANGELOG.md | 3 +++ ixp_tracker/importers.py | 20 +++++++++++--------- pyproject.toml | 2 +- tests/test_members_import.py | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63a358a..e93c415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 0.6 +- extend a membership if created date for imported record is before end date of previous record + ## 0.5 - fix membership starting after it ended - fix import for AS being a member of an IX for multiple prefixes diff --git a/ixp_tracker/importers.py b/ixp_tracker/importers.py index 89c871d..81add49 100644 --- a/ixp_tracker/importers.py +++ b/ixp_tracker/importers.py @@ -212,15 +212,17 @@ def do_process_member_data(all_member_data): # Avoid re-adding a member for the same start_date continue if membership.end_date > created_date: - logger.warning("We might have overlapping memberships", extra=log_data) - # Most recent membership has ended so create a new membership record - membership = IXPMembershipRecord( - member=member, - start_date=created_date, - is_rs_peer=member_data["is_rs_peer"], - speed=member_data["speed"] - ) - logger.debug("Created new membership as previous one ended", extra=log_data) + logger.debug("Extending membership", extra=log_data) + membership.end_date = None + else: + # Most recent membership has ended so create a new membership record + membership = IXPMembershipRecord( + member=member, + start_date=created_date, + is_rs_peer=member_data["is_rs_peer"], + speed=member_data["speed"] + ) + logger.debug("Created new membership as previous one ended", extra=log_data) membership.save() logger.debug("Imported IXP member record", extra=log_data) diff --git a/pyproject.toml b/pyproject.toml index 40d406b..d03fa92 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "django-ixp-tracker" -version = "0.5" +version = "0.6" description = "Library to retrieve and manipulate data about IXPs" readme = "README.md" requires-python = ">=3.8" diff --git a/tests/test_members_import.py b/tests/test_members_import.py index 0b641f4..7e052d6 100644 --- a/tests/test_members_import.py +++ b/tests/test_members_import.py @@ -179,6 +179,37 @@ def test_adds_new_membership_for_existing_member_marked_as_left(): assert current_membership.first().end_date is None +def test_extends_membership_for_member_marked_as_left_if_created_before_date_left(): + asn = create_asn_fixture(dummy_member_data["asn"]) + ixp = create_ixp_fixture(dummy_member_data["ix_id"]) + member = IXPMember( + ixp=ixp, + asn=asn, + last_updated=dummy_member_data["updated"], + last_active=datetime(year=2023, month=7, day=13) + ) + member.save() + membership = IXPMembershipRecord( + member=member, + start_date=datetime(year=2018, month=1, day=3).date(), + end_date=datetime(year=2018, month=7, day=13), + is_rs_peer=False, + speed=500 + ) + membership.save() + + member_data_with_created_date_before_date_left = dict(dummy_member_data) + member_data_with_created_date_before_date_left["created"] = "2018-06-24T14:15:22Z" + processor = importers.process_member_data(date_now, TestLookup()) + processor([member_data_with_created_date_before_date_left]) + + members = IXPMember.objects.all() + assert len(members) == 1 + current_membership = IXPMembershipRecord.objects.filter(member=member).order_by("-start_date") + assert len(current_membership) == 1 + assert current_membership.first().end_date is None + + def test_marks_member_as_left_that_is_no_longer_active(): asn = create_asn_fixture(dummy_member_data["asn"]) ixp = create_ixp_fixture(dummy_member_data["ix_id"]) @@ -311,8 +342,10 @@ def test_ensure_multiple_member_entries_does_not_trigger_multiple_new_membership ) membership.save() + member_data_with_created_date_after_date_left = dict(dummy_member_data) + member_data_with_created_date_after_date_left["created"] = "2023-09-24T14:15:22Z" processor = importers.process_member_data(date_now, TestLookup()) - processor([dummy_member_data, dummy_member_data]) + processor([member_data_with_created_date_after_date_left, member_data_with_created_date_after_date_left]) memberships = IXPMembershipRecord.objects.filter(member=member) assert len(memberships) == 2