Skip to content

Commit

Permalink
remove cache usage
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith committed Sep 13, 2023
1 parent 816644c commit e01830e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 46 deletions.
22 changes: 2 additions & 20 deletions ee/billing/quota_limiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Dict, List, Mapping, Optional, Sequence, TypedDict, cast

import dateutil.parser
from django.core.cache import cache
from django.db.models import Q
from django.utils import timezone
from sentry_sdk import capture_exception
Expand All @@ -24,8 +23,6 @@
from posthog.utils import get_current_day

QUOTA_LIMITER_CACHE_KEY = "@posthog/quota-limits/"
QUOTA_OVERAGE_NO_DROP_EMAILED_CACHE_KEY = "@posthog/quota-overage-no-drop-emailed/"
QUOTA_OVERAGE_NO_DROP_EMAILED_CACHE_TIMEOUT = 60 * 60 * 24 * 7 # 7 days, 604800 seconds


class QuotaResource(Enum):
Expand Down Expand Up @@ -65,19 +62,6 @@ def list_limited_team_tokens(resource: QuotaResource) -> List[str]:
return [x.decode("utf-8") for x in results]


def add_quota_overage_no_drop_cache_item(organization_id: int) -> None:
now = timezone.now()
cache.set(
f"{QUOTA_OVERAGE_NO_DROP_EMAILED_CACHE_KEY}{organization_id}",
now,
timeout=QUOTA_OVERAGE_NO_DROP_EMAILED_CACHE_TIMEOUT,
)


def get_quota_overage_no_drop_cache_item(organization_id: int) -> bool:
return cache.get(f"{QUOTA_OVERAGE_NO_DROP_EMAILED_CACHE_KEY}{organization_id}", False)


class UsageCounters(TypedDict):
events: int
recordings: int
Expand All @@ -99,10 +83,8 @@ def org_quota_limited_until(organization: Organization, resource: QuotaResource)
billing_period_end = round(dateutil.parser.isoparse(organization.usage["period"][1]).timestamp())

if is_quota_limited and organization.never_drop_data:
if not get_quota_overage_no_drop_cache_item(organization.id):
add_quota_overage_no_drop_cache_item(organization.id)
if is_email_available():
send_over_quota_but_not_dropped_email_to_cs.delay(organization.id)
if is_email_available():
send_over_quota_but_not_dropped_email_to_cs.delay(organization.id)
return None

if is_quota_limited and billing_period_end:
Expand Down
25 changes: 0 additions & 25 deletions ee/billing/test/test_quota_limiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from uuid import uuid4

from dateutil.relativedelta import relativedelta
from django.core.cache import cache
from django.utils import timezone
from django.utils.timezone import now
from freezegun import freeze_time
Expand Down Expand Up @@ -207,30 +206,6 @@ def test_over_quota_but_not_dropped_org(self, mock_email, mock_is_email_availabl

self.organization.never_drop_data = False

@patch("ee.billing.quota_limiting.is_email_available", return_value=True)
@patch("ee.billing.quota_limiting.send_over_quota_but_not_dropped_email_to_cs.delay")
def test_over_quota_but_not_dropped_org_already_emailed(self, mock_email, mock_is_email_available):
self.organization.usage = None
assert org_quota_limited_until(self.organization, QuotaResource.EVENTS) is None
cache.set(
f"@posthog/quota-overage-no-drop-emailed/{self.organization.id}", timezone.now(), timeout=60 * 60 * 24 * 7
)

self.organization.usage = {
"events": {"usage": 100, "limit": 90},
"recordings": {"usage": 100, "limit": 90},
"period": ["2021-01-01T00:00:00Z", "2021-01-31T23:59:59Z"],
}
self.organization.never_drop_data = True

assert org_quota_limited_until(self.organization, QuotaResource.EVENTS) is None
assert org_quota_limited_until(self.organization, QuotaResource.RECORDINGS) is None

mock_is_email_available.assert_not_called()
mock_email.assert_not_called()

self.organization.never_drop_data = False

def test_sync_org_quota_limits(self):
with freeze_time("2021-01-01T12:59:59Z"):
other_team = create_team(organization=self.organization)
Expand Down
2 changes: 1 addition & 1 deletion posthog/tasks/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def send_email_verification(user_id: int, token: str) -> None:
def send_over_quota_but_not_dropped_email_to_cs(organization_id: int) -> None:
organization: Organization = Organization.objects.get(pk=organization_id)
message = EmailMessage(
campaign_key=f"over_quota_but_not_dropped-{organization_id}-{timezone.now().timestamp()}",
campaign_key=f"over_quota_but_not_dropped-{organization_id}",
subject=f"{organization.name} over quota, data not dropped",
template_name="over_quota_but_not_dropped",
template_context={
Expand Down

0 comments on commit e01830e

Please sign in to comment.