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: Set organisation api usage to zero #4611

Merged
merged 2 commits into from
Sep 26, 2024
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
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
Loading