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

feat: Set default billing terms for missing info cache #4614

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 12 additions & 10 deletions api/app_analytics/analytics_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,24 @@ def get_usage_data(
match period:
case constants.CURRENT_BILLING_PERIOD:
if not getattr(organisation, "subscription_information_cache", None):
return []
sub_cache = organisation.subscription_information_cache
starts_at = sub_cache.current_billing_term_starts_at or now - timedelta(
days=30
)
starts_at = now - timedelta(days=30)
else:
sub_cache = organisation.subscription_information_cache
starts_at = sub_cache.current_billing_term_starts_at or now - timedelta(
days=30
)
month_delta = relativedelta(now, starts_at).months
date_start = relativedelta(months=month_delta) + starts_at
date_stop = now

case constants.PREVIOUS_BILLING_PERIOD:
if not getattr(organisation, "subscription_information_cache", None):
return []
sub_cache = organisation.subscription_information_cache
starts_at = sub_cache.current_billing_term_starts_at or now - timedelta(
days=30
)
starts_at = now - timedelta(days=30)
else:
sub_cache = organisation.subscription_information_cache
starts_at = sub_cache.current_billing_term_starts_at or now - timedelta(
days=30
)
month_delta = relativedelta(now, starts_at).months - 1
month_delta += relativedelta(now, starts_at).years * 12
date_start = relativedelta(months=month_delta) + starts_at
Expand Down
50 changes: 38 additions & 12 deletions api/tests/unit/app_analytics/test_analytics_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,32 +385,58 @@ def test_get_feature_evaluation_data_calls_get_feature_evaluation_data_from_loca
)


@pytest.mark.parametrize(
"period",
[
CURRENT_BILLING_PERIOD,
PREVIOUS_BILLING_PERIOD,
],
)
def test_get_usage_data_returns_empty_list_when_unset_subscription_information_cache(
period: str,
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
def test_get_usage_data_returns_empty_list_when_unset_subscription_information_cache_for_previous_billing_period(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name of this test (and the following one) needs updating to reflect the new behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Fixed.

mocker: MockerFixture,
settings: SettingsWrapper,
organisation: Organisation,
) -> None:
# Given
period = PREVIOUS_BILLING_PERIOD
settings.USE_POSTGRES_FOR_ANALYTICS = True
mocked_get_usage_data_from_local_db = mocker.patch(
"app_analytics.analytics_db_service.get_usage_data_from_local_db", autospec=True
)
assert getattr(organisation, "subscription_information_cache", None) is None

# When
get_usage_data(organisation, period=period)

# Then
mocked_get_usage_data_from_local_db.assert_called_once_with(
organisation=organisation,
environment_id=None,
project_id=None,
date_start=datetime(2022, 11, 20, 9, 9, 47, 325132, tzinfo=timezone.utc),
date_stop=datetime(2022, 12, 20, 9, 9, 47, 325132, tzinfo=timezone.utc),
)


@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
def test_get_usage_data_returns_empty_list_when_unset_subscription_information_cache_for_current_billing_period(
mocker: MockerFixture,
settings: SettingsWrapper,
organisation: Organisation,
) -> None:
# Given
period = CURRENT_BILLING_PERIOD
settings.USE_POSTGRES_FOR_ANALYTICS = True
mocked_get_usage_data_from_local_db = mocker.patch(
"app_analytics.analytics_db_service.get_usage_data_from_local_db", autospec=True
)
assert getattr(organisation, "subscription_information_cache", None) is None

# When
usage_data = get_usage_data(organisation, period=period)
get_usage_data(organisation, period=period)

# Then
assert usage_data == []
mocked_get_usage_data_from_local_db.assert_not_called()
mocked_get_usage_data_from_local_db.assert_called_once_with(
organisation=organisation,
environment_id=None,
project_id=None,
date_start=datetime(2022, 12, 20, 9, 9, 47, 325132, tzinfo=timezone.utc),
date_stop=datetime(2023, 1, 19, 9, 9, 47, 325132, tzinfo=timezone.utc),
)


@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
Expand Down
Loading