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(postgres-analytics/usage): fix project_id filter #4171

Merged
merged 1 commit into from
Jun 17, 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: 8 additions & 1 deletion api/app_analytics/analytics_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ def get_usage_data_from_local_db(
bucket_size=constants.ANALYTICS_READ_BUCKET_SIZE,
)
if project_id:
qs = qs.filter(project_id=project_id)
environment_ids = Environment.objects.filter(project_id=project_id).values_list(
"id", flat=True
)
# evaluate the queryset because analytics db does not have
# access to environment/project table
environment_ids = list(environment_ids)
qs = qs.filter(environment_id__in=environment_ids)

if environment_id:
qs = qs.filter(environment_id=environment_id)

Expand Down
52 changes: 52 additions & 0 deletions api/tests/unit/app_analytics/test_analytics_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
Organisation,
OrganisationSubscriptionInformationCache,
)
from projects.models import Project


@pytest.fixture
Expand Down Expand Up @@ -105,6 +106,57 @@ def test_get_usage_data_from_local_db(organisation, environment, settings):
assert data.day == today - timedelta(days=29 - count)


@pytest.mark.skipif(
"analytics" not in settings.DATABASES,
reason="Skip test if analytics database is configured",
)
@pytest.mark.django_db(databases=["analytics", "default"])
def test_get_usage_data_from_local_db_project_id_filter(
organisation: Organisation,
project: Project,
project_two: Project,
environment: Environment,
environment_two: Environment,
project_two_environment: Environment,
settings: SettingsWrapper,
):
# Given
environment_id = environment.id
now = timezone.now()
read_bucket_size = 15
settings.ANALYTICS_BUCKET_SIZE = read_bucket_size
total_count = 10

# crate one bucket for every environment
for environment_id in [
environment.id,
environment_two.id,
project_two_environment.id,
]:
APIUsageBucket.objects.create(
environment_id=environment_id,
resource=Resource.FLAGS,
total_count=total_count,
bucket_size=read_bucket_size,
created_at=now,
)
# When
usage_data_for_project_one = get_usage_data_from_local_db(
organisation, project_id=project.id
)
usage_data_for_project_two = get_usage_data_from_local_db(
organisation, project_id=project_two.id
)

# Then
assert len(usage_data_for_project_one) == 1
assert len(usage_data_for_project_two) == 1
assert (
list(usage_data_for_project_one)[0].flags == total_count * 2
) # 2 environments
assert list(usage_data_for_project_two)[0].flags == total_count # 1 environment


@pytest.mark.skipif(
"analytics" not in settings.DATABASES,
reason="Skip test if analytics database is configured",
Expand Down
Loading