Skip to content

Commit 5b1d009

Browse files
committed
Gate remaining get_current_api_usage calls
1 parent 8a61a98 commit 5b1d009

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

api/organisations/tasks.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
register_task_handler,
1313
)
1414

15+
from app_analytics.analytics_db_service import get_total_events_count
1516
from app_analytics.influxdb_wrapper import get_current_api_usage
1617
from integrations.flagsmith.client import get_client
1718
from organisations import subscription_info_cache
@@ -214,10 +215,18 @@ def charge_for_api_call_count_overages(): # type: ignore[no-untyped-def]
214215
continue
215216

216217
subscription_cache = organisation.subscription_information_cache
217-
api_usage = get_current_api_usage(
218-
organisation_id=organisation.id,
219-
date_start=subscription_cache.current_billing_term_starts_at,
220-
)
218+
if flags.is_feature_enabled(
219+
"get_current_api_usage_deprecated"
220+
): # pragma: no cover
221+
api_usage = get_total_events_count(
222+
organisation,
223+
date_start=subscription_cache.current_billing_term_starts_at,
224+
)
225+
else:
226+
api_usage = get_current_api_usage(
227+
organisation_id=organisation.id,
228+
date_start=subscription_cache.current_billing_term_starts_at,
229+
)
221230

222231
# Grace period for organisations < 200% of usage.
223232
if (
@@ -347,10 +356,18 @@ def restrict_use_due_to_api_limit_grace_period_over() -> None:
347356
OrganisationBreachedGracePeriod.objects.get_or_create(organisation=organisation)
348357

349358
subscription_cache = organisation.subscription_information_cache
350-
api_usage = get_current_api_usage(
351-
organisation_id=organisation.id,
352-
date_start=now - timedelta(days=30),
353-
)
359+
if flags.is_feature_enabled(
360+
"get_current_api_usage_deprecated"
361+
): # pragma: no cover
362+
api_usage = get_total_events_count(
363+
organisation,
364+
date_start=now - timedelta(days=30),
365+
)
366+
else:
367+
api_usage = get_current_api_usage(
368+
organisation_id=organisation.id,
369+
date_start=now - timedelta(days=30),
370+
)
354371
if api_usage / subscription_cache.allowed_30d_api_calls < 1.0:
355372
logger.info(
356373
f"API use for organisation {organisation.id} has fallen to below limit, so not restricting use."

0 commit comments

Comments
 (0)