Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(overage-billing): use relative delta and months, instead of days #5060

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/organisations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import timedelta

from app_analytics.influxdb_wrapper import get_current_api_usage
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.core.mail import send_mail
from django.db.models import F, Max, Q
Expand Down Expand Up @@ -154,7 +155,7 @@ def charge_for_api_call_count_overages():

# Get the period where we're interested in any new API usage
# notifications for the relevant billing period (ie, this month).
api_usage_notified_at = now - timedelta(days=30)
api_usage_notified_at = now - relativedelta(months=1)

# Since we're only interested in monthly billed accounts, set a wide
# threshold to catch as many billing periods that could be roughly
Expand Down
12 changes: 11 additions & 1 deletion api/tests/unit/organisations/test_unit_organisations_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
from core.helpers import get_current_site_url
from dateutil.relativedelta import relativedelta
from django.core.mail.message import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils import timezone
Expand Down Expand Up @@ -834,10 +835,19 @@ def test_charge_for_api_call_count_overages_scale_up(
organisation.subscription.subscription_id = "fancy_sub_id23"
organisation.subscription.plan = "scale-up-v2"
organisation.subscription.save()

# In order to cover an edge case found in production use, we make the
# notification date just outside the previous 30 days, because we want
# to make sure that we cover the case where someone with very high usage
# is notified in the first day of their subscription period (in a 31-day
# month).
notification_date = now - (timedelta(days=30) + timedelta(minutes=30))
assert notification_date > now - relativedelta(months=1)

OrganisationAPIUsageNotification.objects.create(
organisation=organisation,
percent_usage=100,
notified_at=now,
notified_at=notification_date,
)

mocker.patch("organisations.chargebee.chargebee.chargebee.Subscription.retrieve")
Expand Down
Loading