|
@@ -1,57 +1,35 @@
|
|
|
from django.conf import settings
|
|
|
-from django.db.models import Count, Q, F, Subquery, OuterRef, IntegerField
|
|
|
+from django.db.models import Q, F, IntegerField
|
|
|
from django.db.models.functions import Cast
|
|
|
from django.db.models.fields.json import KeyTextTransform
|
|
|
from celery import shared_task
|
|
|
from organizations_ext.models import Organization
|
|
|
-from projects.models import Project
|
|
|
from .models import SubscriptionQuotaWarning
|
|
|
from .email import WarnQuotaEmail
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
def warn_organization_throttle():
|
|
|
- """ Warn user about approaching 80% of allotted events """
|
|
|
+ """Warn user about approaching 80% of allotted events"""
|
|
|
if not settings.BILLING_ENABLED:
|
|
|
return
|
|
|
|
|
|
- queryset = Organization.objects.filter(
|
|
|
- djstripe_customers__subscriptions__status="active",
|
|
|
- ).filter(
|
|
|
- Q(djstripe_customers__subscriptions__subscriptionquotawarning=None)
|
|
|
- | Q(
|
|
|
- djstripe_customers__subscriptions__subscriptionquotawarning__notice_last_sent__lt=F(
|
|
|
- "djstripe_customers__subscriptions__current_period_start"
|
|
|
- ),
|
|
|
+ queryset = (
|
|
|
+ Organization.objects.with_event_counts()
|
|
|
+ .filter(
|
|
|
+ djstripe_customers__subscriptions__status="active",
|
|
|
)
|
|
|
- )
|
|
|
-
|
|
|
- projects = Project.objects.filter(organization=OuterRef("pk")).values(
|
|
|
- "organization"
|
|
|
- )
|
|
|
- total_issue_events = projects.annotate(
|
|
|
- total=Count(
|
|
|
- "issue__event",
|
|
|
- filter=Q(
|
|
|
- issue__event__created__gte=OuterRef(
|
|
|
+ .filter(
|
|
|
+ Q(djstripe_customers__subscriptions__subscriptionquotawarning=None)
|
|
|
+ | Q(
|
|
|
+ djstripe_customers__subscriptions__subscriptionquotawarning__notice_last_sent__lt=F(
|
|
|
"djstripe_customers__subscriptions__current_period_start"
|
|
|
- )
|
|
|
- ),
|
|
|
+ ),
|
|
|
+ )
|
|
|
)
|
|
|
- ).values("total")
|
|
|
- total_transaction_events = projects.annotate(
|
|
|
- total=Count(
|
|
|
- "transactiongroup__transactionevent",
|
|
|
- filter=Q(
|
|
|
- transactiongroup__transactionevent__created__gte=OuterRef(
|
|
|
- "djstripe_customers__subscriptions__current_period_start"
|
|
|
- )
|
|
|
- ),
|
|
|
- )
|
|
|
- ).values("total")
|
|
|
+ )
|
|
|
|
|
|
queryset = queryset.annotate(
|
|
|
- event_count=Subquery(total_issue_events) + Subquery(total_transaction_events),
|
|
|
plan_event_count=Cast(
|
|
|
KeyTextTransform(
|
|
|
"events", "djstripe_customers__subscriptions__plan__product__metadata"
|
|
@@ -62,13 +40,15 @@ def warn_organization_throttle():
|
|
|
|
|
|
# 80% to 100% of event quota
|
|
|
queryset = queryset.filter(
|
|
|
- event_count__gte=F("plan_event_count") * 0.80,
|
|
|
- event_count__lte=F("plan_event_count"),
|
|
|
+ total_event_count__gte=F("plan_event_count") * 0.80,
|
|
|
+ total_event_count__lte=F("plan_event_count"),
|
|
|
)
|
|
|
|
|
|
+ print("-----------")
|
|
|
+ print(queryset.query)
|
|
|
for org in queryset:
|
|
|
subscription = org.djstripe_customers.first().subscription
|
|
|
- send_email_warn_quota.delay(subscription.pk, org.event_count)
|
|
|
+ send_email_warn_quota.delay(subscription.pk, org.total_event_count)
|
|
|
warning, created = SubscriptionQuotaWarning.objects.get_or_create(
|
|
|
subscription=subscription
|
|
|
)
|