Skip to content

Commit

Permalink
fix: Set organisation api usage to zero (#4611)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Sep 26, 2024
1 parent f9069e4 commit 008998c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/organisations/subscription_info_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,22 @@ def _update_caches_with_influx_data(

org_calls = get_top_organisations(date_start, limit)

covered_orgs = set()

for org_id, calls in org_calls.items():
subscription_info_cache = organisation_info_cache_dict.get(org_id)
covered_orgs.add(org_id)

if not subscription_info_cache:
# I don't think this is a valid case but worth checking / handling
continue
setattr(subscription_info_cache, key, calls)

for org_id in organisation_info_cache_dict:
if org_id not in covered_orgs:
subscription_info_cache = organisation_info_cache_dict.get(org_id)
setattr(subscription_info_cache, key, 0)


def _update_caches_with_chargebee_data(
organisations: typing.Iterable[Organisation],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import pytest
from django.utils import timezone
from pytest_django.fixtures import SettingsWrapper
from pytest_mock import MockerFixture
from task_processor.task_run_method import TaskRunMethod

from organisations.chargebee.metadata import ChargebeeObjMetadata
from organisations.models import (
Organisation,
OrganisationSubscriptionInformationCache,
Subscription,
)
from organisations.subscription_info_cache import update_caches
from organisations.subscriptions.constants import SubscriptionCacheEntity

Expand Down Expand Up @@ -77,3 +84,46 @@ def test_update_caches(mocker, organisation, chargebee_subscription, settings):
(day_7, ""),
(day_1, "100"),
]


def test_update_caches_empty(
mocker: MockerFixture,
organisation: Organisation,
chargebee_subscription: Subscription,
settings: SettingsWrapper,
) -> None:
# Given
settings.CHARGEBEE_API_KEY = "api-key"
settings.INFLUXDB_TOKEN = "token"
settings.TASK_RUN_METHOD = TaskRunMethod.SYNCHRONOUSLY

OrganisationSubscriptionInformationCache.objects.create(
organisation=organisation,
api_calls_24h=1,
api_calls_7d=1,
api_calls_30d=1,
)

mocked_get_top_organisations = mocker.patch(
"organisations.subscription_info_cache.get_top_organisations"
)
mocked_get_top_organisations.return_value = {}

chargebee_metadata = ChargebeeObjMetadata(seats=15, api_calls=1000000)
mocked_get_subscription_metadata = mocker.patch(
"organisations.subscription_info_cache.get_subscription_metadata_from_id"
)
mocked_get_subscription_metadata.return_value = chargebee_metadata

# When
subscription_cache_entities = (
SubscriptionCacheEntity.INFLUX,
SubscriptionCacheEntity.CHARGEBEE,
)
update_caches(subscription_cache_entities)

# Then
organisation.refresh_from_db()
assert organisation.subscription_information_cache.api_calls_24h == 0
assert organisation.subscription_information_cache.api_calls_7d == 0
assert organisation.subscription_information_cache.api_calls_30d == 0

0 comments on commit 008998c

Please sign in to comment.