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: various fixes for API usage alerting / billing #4158

Merged
merged 5 commits into from
Jun 13, 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
2 changes: 2 additions & 0 deletions api/app/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
AWS_SSE_LOGS_BUCKET_NAME = "test_bucket"

RETRY_WEBHOOKS = True

INFLUXDB_BUCKET = "test_bucket"
6 changes: 3 additions & 3 deletions api/app_analytics/influxdb_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
read_bucket = settings.INFLUXDB_BUCKET + "_downsampled_15m"

range_bucket_mappings = {
"24h": settings.INFLUXDB_BUCKET + "_downsampled_15m",
"7d": settings.INFLUXDB_BUCKET + "_downsampled_15m",
"30d": settings.INFLUXDB_BUCKET + "_downsampled_1h",
"-24h": settings.INFLUXDB_BUCKET + "_downsampled_15m",
"-7d": settings.INFLUXDB_BUCKET + "_downsampled_15m",
"-30d": settings.INFLUXDB_BUCKET + "_downsampled_1h",
}
retries = Retry(connect=3, read=3, redirect=3)
# Set a timeout to prevent threads being potentially stuck open due to network weirdness
Expand Down
4 changes: 4 additions & 0 deletions api/organisations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def is_paid(self):
self.has_paid_subscription() and self.subscription.cancellation_date is None
)

@property
def flagsmith_identifier(self):
return f"org.{self.id}"

def over_plan_seats_limit(self, additional_seats: int = 0):
if self.has_paid_subscription():
susbcription_metadata = self.subscription.get_subscription_metadata()
Expand Down
4 changes: 2 additions & 2 deletions api/organisations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def handle_api_usage_notifications() -> None:
"subscription_information_cache",
):
feature_enabled = flagsmith_client.get_identity_flags(
f"org.{organisation.id}.{organisation.name}",
organisation.flagsmith_identifier,
traits={"organisation_id": organisation.id},
).is_feature_enabled("api_usage_alerting")
if not feature_enabled:
Expand Down Expand Up @@ -333,7 +333,7 @@ def restrict_use_due_to_api_limit_grace_period_over() -> None:

for organisation in organisations:
flags = flagsmith_client.get_identity_flags(
f"org.{organisation.id}.{organisation.name}",
organisation.flagsmith_identifier,
traits={"organisation_id": organisation.id},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,6 @@ def test_get_top_organisations(
mocker: MockerFixture,
) -> None:
# Given
mocker.patch("app_analytics.influxdb_wrapper.range_bucket_mappings")

record_mock1 = mock.MagicMock()
record_mock1.values = {"organisation": "123-TestOrg"}
record_mock1.get_value.return_value = 23
Expand All @@ -408,13 +406,16 @@ def test_get_top_organisations(
# Then
assert dataset == {123: 23, 456: 43}

influx_mock.assert_called_once()
influx_query_call = influx_mock.call_args
assert influx_query_call.kwargs["bucket"] == "test_bucket_downsampled_1h"
assert influx_query_call.kwargs["date_start"] == "-30d"


def test_get_top_organisations_value_error(
mocker: MockerFixture,
) -> None:
# Given
mocker.patch("app_analytics.influxdb_wrapper.range_bucket_mappings")

record_mock1 = mock.MagicMock()
record_mock1.values = {"organisation": "BadData-TestOrg"}
record_mock1.get_value.return_value = 23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ def test_handle_api_usage_notifications_when_feature_flag_is_off(
# Then
mock_api_usage.assert_not_called()

client_mock.get_identity_flags.assert_called_once_with(
organisation.flagsmith_identifier, traits={"organisation_id": organisation.id}
)

assert len(mailoutbox) == 0
assert (
OrganisationAPIUsageNotification.objects.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@
{
"name": "GITHUB_APP_ID",
"value": "811209"
},
{
"name": "FLAGSMITH_ON_FLAGSMITH_SERVER_API_URL",
"value": "https://edge.api.flagsmith.com/api/v1/"
}
],
"secrets": [
Expand Down Expand Up @@ -196,6 +200,10 @@
{
"name": "GITHUB_PEM",
"valueFrom": "arn:aws:secretsmanager:eu-west-2:302456015006:secret:GITHUB_PEM-Bfoaql"
},
{
"name": "FLAGSMITH_ON_FLAGSMITH_SERVER_KEY",
"valueFrom": "arn:aws:secretsmanager:eu-west-2:302456015006:secret:ECS-API-heAdoB:FLAGSMITH_ON_FLAGSMITH_SERVER_KEY::"
}
],
"logConfiguration": {
Expand Down
Loading