Skip to content

Commit

Permalink
Ensure membership end date is never before the start date
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-isoc committed Sep 19, 2024
1 parent ab88270 commit f0584c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ixp_tracker/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def do_process_member_data(all_member_data):
speed=member_data["speed"]
)
membership.save()
logger.debug("Created new membership for new member", extra=log_data)
else:
if membership.end_date is None:
# Membership is current so just update the details if needed
Expand All @@ -215,6 +216,7 @@ def do_process_member_data(all_member_data):
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)
Expand All @@ -236,6 +238,10 @@ def do_process_member_data(all_member_data):
continue
if geo_lookup.get_status(candidate.asn.number, processing_date) != "assigned":
end_of_last_month_active = (candidate.last_active.replace(day=1) - timedelta(days=1))
if end_of_last_month_active.date() < latest_membership.start_date:
# It can happen that a member is immediately marked as left as the AS is not registered to a country
# In this case make sure the date we are using for the membership end date is not before the start_date
end_of_last_month_active = latest_membership.start_date
latest_membership.end_date = end_of_last_month_active
latest_membership.save()
logger.debug("Member flagged as left due to unassigned ASN", extra={"member": candidate.asn.number})
Expand Down
26 changes: 26 additions & 0 deletions tests/test_members_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,32 @@ def test_marks_member_as_left_if_asn_is_not_assigned():
assert current_membership.first().end_date.strftime("%Y-%m-%d") == last_day_of_last_month.strftime("%Y-%m-%d")


def test_does_not_mark_as_left_before_joining_date():
asn = create_asn_fixture(dummy_member_data["asn"], "ZZ")
ixp = create_ixp_fixture(dummy_member_data["ix_id"])
first_day_of_month = datetime.utcnow().replace(day=1)
member = IXPMember(
ixp=ixp,
asn=asn,
last_updated=dummy_member_data["updated"],
last_active=datetime.utcnow()
)
member.save()
membership = IXPMembershipRecord(
member=member,
start_date=first_day_of_month.date(),
is_rs_peer=False,
speed=500
)
membership.save()

processor = importers.process_member_data(date_now, TestLookup("available"))
processor([])

current_membership = IXPMembershipRecord.objects.filter(member=member)
assert current_membership.first().end_date.strftime("%Y-%m-%d") == first_day_of_month.strftime("%Y-%m-%d")


def create_asn_fixture(as_number: int, country: str = "CH"):
asn = ASN.objects.filter(number=as_number)
if len(asn) > 0:
Expand Down

0 comments on commit f0584c8

Please sign in to comment.