-
Notifications
You must be signed in to change notification settings - Fork 562
fix(sales-dashboard): Correct understated API usage on the organisation chart #8468
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
Open
matthewelwell
wants to merge
1
commit into
main
Choose a base branch
from
fix/sales-dashboard-understates-api-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -153,6 +153,39 @@ def map_flux_tables_to_usage_data( | |||||||||||||||||||||||||
| return list(data_by_key.values()) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| USAGE_DATA_RESOURCE_ATTRIBUTES: tuple[str, ...] = tuple( | ||||||||||||||||||||||||||
| column_name for resource in Resource if (column_name := resource.column_name) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def map_usage_data_to_daily_totals( | ||||||||||||||||||||||||||
| usage_data: Iterable[UsageData], | ||||||||||||||||||||||||||
| ) -> tuple[list[str], dict[str, list[int]]]: | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| Collapse usage data into a single total per day for each resource. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Usage data holds a row per day *and* labels combination, so a day with | ||||||||||||||||||||||||||
| traffic from more than one client application appears more than once. Any | ||||||||||||||||||||||||||
| caller wanting whole-organisation totals has to sum across those rows. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| :return: the days covered, and a list of totals per resource aligned to them | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| totals_by_day: dict[str, dict[str, int]] = {} | ||||||||||||||||||||||||||
| for data in usage_data: | ||||||||||||||||||||||||||
| totals = totals_by_day.setdefault( | ||||||||||||||||||||||||||
| str(data.day), | ||||||||||||||||||||||||||
| dict.fromkeys(USAGE_DATA_RESOURCE_ATTRIBUTES, 0), | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| for resource_attr in USAGE_DATA_RESOURCE_ATTRIBUTES: | ||||||||||||||||||||||||||
| totals[resource_attr] += getattr(data, resource_attr) | ||||||||||||||||||||||||||
|
Comment on lines
+173
to
+180
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
💅 nit. |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| days = sorted(totals_by_day) | ||||||||||||||||||||||||||
| return days, { | ||||||||||||||||||||||||||
| resource_attr: [totals_by_day[day][resource_attr] for day in days] | ||||||||||||||||||||||||||
| for resource_attr in USAGE_DATA_RESOURCE_ATTRIBUTES | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def map_flux_tables_to_feature_evaluation_data( | ||||||||||||||||||||||||||
| flux_tables: list[FluxTable], | ||||||||||||||||||||||||||
| ) -> list[FeatureEvaluationData]: | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||
| map_flux_tables_to_feature_evaluation_data, | ||||||||||||||
| map_flux_tables_to_usage_data, | ||||||||||||||
| map_influx_record_values_to_labels, | ||||||||||||||
| map_usage_data_to_daily_totals, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
|
@@ -107,3 +108,79 @@ def test_map_influx_record_values_to_labels__various_user_agents__returns_expect | |||||||||||||
|
|
||||||||||||||
| # Then | ||||||||||||||
| assert result == expected | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_map_usage_data_to_daily_totals__multiple_labels_per_day__sums_across_labels() -> ( | ||||||||||||||
| None | ||||||||||||||
| ): | ||||||||||||||
| """ | ||||||||||||||
| Usage data holds a row per day and labels combination, so a day with | ||||||||||||||
| traffic from several client applications arrives as several rows. Totals | ||||||||||||||
| must sum across them, or a single client is reported as the whole | ||||||||||||||
| organisation's usage. | ||||||||||||||
| """ | ||||||||||||||
|
Comment on lines
+116
to
+121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| # Given | ||||||||||||||
| usage_data = [ | ||||||||||||||
| UsageData( | ||||||||||||||
| day=date(2026, 6, 27), | ||||||||||||||
| flags=3_158, | ||||||||||||||
| labels={"client_application_name": "small-app"}, | ||||||||||||||
| ), | ||||||||||||||
| UsageData( | ||||||||||||||
| day=date(2026, 6, 27), | ||||||||||||||
| flags=238_574, | ||||||||||||||
| identities=10, | ||||||||||||||
| labels={"client_application_name": "busy-app"}, | ||||||||||||||
| ), | ||||||||||||||
| UsageData( | ||||||||||||||
| day=date(2026, 6, 28), | ||||||||||||||
| flags=240_000, | ||||||||||||||
| traits=5, | ||||||||||||||
| environment_document=1, | ||||||||||||||
| labels={"client_application_name": "busy-app"}, | ||||||||||||||
| ), | ||||||||||||||
| ] | ||||||||||||||
|
|
||||||||||||||
| # When | ||||||||||||||
| labels, totals = map_usage_data_to_daily_totals(usage_data) | ||||||||||||||
|
|
||||||||||||||
| # Then | ||||||||||||||
| assert labels == ["2026-06-27", "2026-06-28"] | ||||||||||||||
| assert totals == { | ||||||||||||||
| "flags": [241_732, 240_000], | ||||||||||||||
| "identities": [10, 0], | ||||||||||||||
| "traits": [0, 5], | ||||||||||||||
| "environment_document": [0, 1], | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_map_usage_data_to_daily_totals__unordered_days__returns_days_in_order() -> ( | ||||||||||||||
| None | ||||||||||||||
| ): | ||||||||||||||
| # Given | ||||||||||||||
| usage_data = [ | ||||||||||||||
| UsageData(day=date(2026, 6, 28), flags=2), | ||||||||||||||
| UsageData(day=date(2026, 6, 26), flags=1), | ||||||||||||||
| UsageData(day=date(2026, 6, 27), flags=3), | ||||||||||||||
| ] | ||||||||||||||
|
|
||||||||||||||
| # When | ||||||||||||||
| labels, totals = map_usage_data_to_daily_totals(usage_data) | ||||||||||||||
|
|
||||||||||||||
| # Then | ||||||||||||||
| assert labels == ["2026-06-26", "2026-06-27", "2026-06-28"] | ||||||||||||||
| assert totals["flags"] == [1, 3, 2] | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_map_usage_data_to_daily_totals__no_usage_data__returns_empty_series() -> None: | ||||||||||||||
| # Given / When | ||||||||||||||
| labels, totals = map_usage_data_to_daily_totals([]) | ||||||||||||||
|
|
||||||||||||||
| # Then | ||||||||||||||
| assert labels == [] | ||||||||||||||
| assert totals == { | ||||||||||||||
| "flags": [], | ||||||||||||||
| "identities": [], | ||||||||||||||
| "traits": [], | ||||||||||||||
| "environment_document": [], | ||||||||||||||
| } | ||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| from datetime import timedelta | ||
| from datetime import date, timedelta | ||
|
|
||
| import pytest | ||
| from django.test import Client, RequestFactory | ||
|
|
@@ -8,6 +8,7 @@ | |
| from pytest_mock import MockerFixture | ||
| from rest_framework.test import APIClient | ||
|
|
||
| from app_analytics.dataclasses import UsageData | ||
| from features.versioning.constants import DEFAULT_VERSION_LIMIT_DAYS | ||
| from organisations.models import ( | ||
| Organisation, | ||
|
|
@@ -65,23 +66,35 @@ def test_get_organisation_info__valid_organisation__returns_event_list( | |
|
|
||
| url = reverse("sales_dashboard:organisation_info", args=[organisation.id]) | ||
|
|
||
| event_list_mock = mocker.patch( | ||
| "sales_dashboard.views.get_event_list_for_organisation" | ||
| ) | ||
| event_list_mock.return_value = ( | ||
| {"traits": [], "identities": [], "flags": [], "environment-document": []}, | ||
| ["label1", "label2"], | ||
| ) | ||
| usage_data_mock = mocker.patch("sales_dashboard.views.get_usage_data") | ||
| usage_data_mock.return_value = [ | ||
| UsageData( | ||
| day=date(2026, 6, 27), | ||
| flags=3_158, | ||
| labels={"client_application_name": "small-app"}, | ||
| ), | ||
| UsageData( | ||
| day=date(2026, 6, 27), | ||
| flags=238_574, | ||
| labels={"client_application_name": "busy-app"}, | ||
| ), | ||
| ] | ||
| mocker.patch("sales_dashboard.views.get_events_for_organisation") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can delete? |
||
|
|
||
| # When | ||
| response = superuser_client.get(url) | ||
|
|
||
| # Then | ||
| assert "label1" in str(response.content) | ||
| assert "label2" in str(response.content) | ||
| date_start = timezone.now() - timedelta(days=180) | ||
| event_list_mock.assert_called_once_with(organisation.id, date_start) | ||
| content = str(response.content) | ||
| assert "2026-06-27" in content | ||
| # Both client applications' usage for the day, not just one of them. | ||
| assert "241732" in content | ||
| now = timezone.now() | ||
| usage_data_mock.assert_called_once_with( | ||
| organisation.id, | ||
| date_start=now - timedelta(days=180), | ||
| date_stop=now, | ||
| ) | ||
|
|
||
|
|
||
| def test_list_organisations__search_by_name__returns_matching_organisation( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Consider defining a meaningful type. This hint looks like it only exists to appease the type checker, instead of helping the code.