Skip to content

fix(sales-dashboard): Correct understated API usage on the organisation chart - #8468

Open
matthewelwell wants to merge 1 commit into
mainfrom
fix/sales-dashboard-understates-api-usage
Open

fix(sales-dashboard): Correct understated API usage on the organisation chart#8468
matthewelwell wants to merge 1 commit into
mainfrom
fix/sales-dashboard-understates-api-usage

Conversation

@matthewelwell

Copy link
Copy Markdown
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Contributes to #7976

The sales dashboard read usage through its own Influx entry point, get_event_list_for_organisation, the only usage reader without a group() before aggregateWindow. Influx aggregates per series, so each label combination produced its own daily records, which were then appended into a flat list per resource instead of summed per day. Chart.js plotted the leading slice of that list, so one client application's traffic was shown as the whole organisation's usage.

That function is deleted. The sales dashboard now calls get_usage_data — the entry point the customer-facing app already uses — and reshapes the result for Chart.js in map_usage_data_to_daily_totals. One shared query means the grouping cannot go missing in one caller and not the other.

Worth noting for #7976: the API usage alert emails were correct all along. The affected organisation genuinely exceeded its plan allowance; this chart is what led us to believe otherwise.

How did you test this code?

Unit tests cover the mapper against multiple label rows per day, unordered days, and empty input, plus the sales dashboard view asserting both client applications' usage appears rather than one.

Verified against production data for the affected organisation. Summing every series over its billing sub-period returned 5,030,444 API calls from both the 15m and 1h downsampled buckets, and matched the customer-facing dashboard, while the sales dashboard chart displayed roughly 1/76th of that.

Also ran make lint, make typecheck (baseline unchanged, no errors in the touched files) and the three affected test files (54 passed).

🤖 Generated with Claude Code

…on chart

The sales dashboard read API usage through its own Influx entry point,
`get_event_list_for_organisation`, which was the only usage reader without a
`group()` before `aggregateWindow`. Influx aggregates per series, so each label
combination produced its own daily records, and the Python then appended them
into one flat list per resource rather than summing them per day. Chart.js
plotted the leading slice of that list, so a single client application's traffic
was shown as the whole organisation's usage, with the x-axis labels taken from
the first series only.

Delete that function and have the sales dashboard call `get_usage_data`, the
same entry point the customer-facing app uses, reshaping the result for
Chart.js in `map_usage_data_to_daily_totals`. Both charts now share one query,
so the grouping cannot go missing in one caller and not the other.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Sep 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated
docs Ignored Ignored Sep 4, 2026 3:22pm UTC
flagsmith-frontend-preview Ignored Ignored Sep 4, 2026 3:22pm UTC
flagsmith-frontend-staging Ignored Ignored Sep 4, 2026 3:22pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Team

Run ID: df45c0ed-dab5-40cd-9a0f-58986f3f50a1

📥 Commits

Reviewing files that changed from the base of the PR and between 2ce9498 and 5aabf8f.

📒 Files selected for processing (6)
  • api/app_analytics/influxdb_wrapper.py
  • api/app_analytics/mappers.py
  • api/sales_dashboard/views.py
  • api/tests/unit/app_analytics/test_unit_app_analytics_influxdb_wrapper.py
  • api/tests/unit/app_analytics/test_unit_app_analytics_mappers.py
  • api/tests/unit/sales_dashboard/test_unit_sales_dashboard_views.py
💤 Files with no reviewable changes (2)
  • api/app_analytics/influxdb_wrapper.py
  • api/tests/unit/app_analytics/test_unit_app_analytics_influxdb_wrapper.py

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The change replaces organisation event-list retrieval with UsageData retrieval and daily aggregation. A new mapper sums resource values across labels, sorts dates, and returns aligned series. The sales dashboard uses these totals for its traits, identities, flags, environment documents, and labels. The previous InfluxDB function and its tests are removed. Mapper and dashboard tests cover aggregation, ordering, empty input, and the 180-day query window.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 5aabf

The sales dashboard now displays daily organisation-wide usage totals rather than a single label slice. Aggregation and rendering behavior are covered for the corrected data shape, with no concrete current-head merge risk identified.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added api Issue related to the REST API fix labels Sep 4, 2026
@codecov

codecov Bot commented Sep 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.81%. Comparing base (2ce9498) to head (5aabf8f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8468      +/-   ##
==========================================
- Coverage   98.81%   98.81%   -0.01%     
==========================================
  Files        1621     1621              
  Lines       66299    66281      -18     
==========================================
- Hits        65514    65496      -18     
  Misses        785      785              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@matthewelwell
matthewelwell marked this pull request as ready for review September 4, 2026 16:38
@matthewelwell
matthewelwell requested a review from a team as a code owner September 4, 2026 16:38
@matthewelwell
matthewelwell requested review from emyller and removed request for a team September 4, 2026 16:38
@github-actions github-actions Bot added fix and removed fix labels Sep 4, 2026
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-8468 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api-test:pr-8468 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-8468 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-api:pr-8468 Finished ✅ Results
ghcr.io/flagsmith/flagsmith:pr-8468 Finished ✅ Results
ghcr.io/flagsmith/flagsmith-private-cloud:pr-8468 Finished ✅ Results

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor
✅ private-cloud · depot-ubuntu-latest-arm-16 — run #20189 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  36.7 seconds
commit  5aabf8f
info  🔄 Run: #20189 (attempt 1)

🗂️ Previous results
✅ oss · depot-ubuntu-latest-arm-16 — run #20189 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  59.8 seconds
commit  5aabf8f
info  🔄 Run: #20189 (attempt 1)

✅ private-cloud · depot-ubuntu-latest-16 — run #20189 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  31.5 seconds
commit  5aabf8f
info  🔄 Run: #20189 (attempt 1)

✅ oss · depot-ubuntu-latest-16 — run #20189 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  40.4 seconds
commit  5aabf8f
info  🔄 Run: #20189 (attempt 1)

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Visual Regression

19 screenshots compared. See report for details.
View full report

@emyller emyller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd appreciate a cleaner diff, but can't find a reason to block.


def map_usage_data_to_daily_totals(
usage_data: Iterable[UsageData],
) -> tuple[list[str], dict[str, list[int]]]:

Copy link
Copy Markdown
Contributor

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.

Comment on lines +173 to +180
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
totals_by_day = defaultdict(Counter)
for data in usage_data:
for resource_attr in USAGE_DATA_RESOURCE_ATTRIBUTES:
totals_by_day[str(data.day)][resource_attr] += getattr(data, resource_attr)

💅 nit.

Comment on lines +116 to +121
"""
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.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""
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.
"""

labels={"client_application_name": "busy-app"},
),
]
mocker.patch("sales_dashboard.views.get_events_for_organisation")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can delete?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants