Skip to content

Commit

Permalink
Add grace period to restrict task
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan committed Aug 6, 2024
1 parent 1b24619 commit e3c7629
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions api/organisations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from app_analytics.influxdb_wrapper import get_current_api_usage
from django.conf import settings
from django.db.models import F, Max
from django.db.models import F, Max, Q
from django.utils import timezone
from task_processor.decorators import (
register_recurring_task,
Expand All @@ -22,6 +22,7 @@
Organisation,
OrganisationAPIBilling,
OrganisationAPIUsageNotification,
OrganisationBreachedGracePeriod,
Subscription,
)
from organisations.subscriptions.constants import FREE_PLAN_ID
Expand Down Expand Up @@ -243,13 +244,22 @@ def restrict_use_due_to_api_limit_grace_period_over() -> None:
Since free plans don't have predefined subscription periods, we
use a rolling thirty day period to filter them.
"""
grace_period = timezone.now() - timedelta(days=API_USAGE_GRACE_PERIOD)
month_start = timezone.now() - timedelta(30)
now = timezone.now()
grace_period = now - timedelta(days=API_USAGE_GRACE_PERIOD)
month_start = now - timedelta(30)
queryset = (
OrganisationAPIUsageNotification.objects.filter(
notified_at__gt=month_start,
notified_at__lt=grace_period,
percent_usage__gte=100,
Q(
notified_at__gte=month_start,
notified_at__lte=grace_period,
percent_usage__gte=100,
)
| Q(
notified_at__gte=month_start,
notified_at__lte=now,
percent_usage__gte=100,
organisation__breached_grace_period__isnull=False,
)
)
.values("organisation")
.annotate(max_value=Max("percent_usage"))
Expand Down Expand Up @@ -293,6 +303,8 @@ def restrict_use_due_to_api_limit_grace_period_over() -> None:
if not organisation.has_subscription_information_cache():
continue

OrganisationBreachedGracePeriod.objects.get_or_create(organisation=organisation)

subscription_cache = organisation.subscription_information_cache
api_usage = get_current_api_usage(organisation.id, "30d")
if api_usage / subscription_cache.allowed_30d_api_calls < 1.0:
Expand Down

0 comments on commit e3c7629

Please sign in to comment.