Skip to content

Fix bug where sent in the last week email totals were always 0#2826

Merged
smcmurtry merged 2 commits intomainfrom
fix/dashboard-emails-0
Apr 2, 2026
Merged

Fix bug where sent in the last week email totals were always 0#2826
smcmurtry merged 2 commits intomainfrom
fix/dashboard-emails-0

Conversation

@smcmurtry
Copy link
Copy Markdown
Contributor

@smcmurtry smcmurtry commented Apr 1, 2026

Summary | Résumé

This PR:

  • Removes notification_type == SMS_TYPE from both billable-units DAO functions so email data is now included
  • Adds coalesce(..., 0) around sum(Notification.billable_units) in _stats_for_today_with_billable_units since Notification.billable_units is nullable for emails

Related Issues | Cartes liées

https://docs.google.com/document/d/17_hHvjjaF1EBYtteGKX-Qzcg_rqsnvpsRMLaaxybbVc/edit?tab=t.0

Reviewer checklist | Liste de vérification du réviseur

  • This PR does not break existing functionality.
  • This PR does not violate GCNotify's privacy policies.
  • This PR does not raise new security concerns. Refer to our GC Notify Risk Register document on our Google drive.
  • This PR does not significantly alter performance.
  • Additional required documentation resulting of these changes is covered (such as the README, setup instructions, a related ADR or the technical documentation).

⚠ If boxes cannot be checked off before merging the PR, they should be moved to the "Release Instructions" section with appropriate steps required to verify before release. For example, changes to celery code may require tests on staging to verify that performance has not been affected.

@smcmurtry smcmurtry marked this pull request as ready for review April 2, 2026 13:19
@smcmurtry smcmurtry requested a review from jimleroyer as a code owner April 2, 2026 13:19
Copilot AI review requested due to automatic review settings April 2, 2026 13:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes incorrect “last week” email totals in service template statistics by ensuring billable-units aggregation queries include email notifications (not just SMS) and by defaulting nullable billable-unit sums to 0 for today’s notifications query.

Changes:

  • Removed the notification_type == SMS_TYPE filter from billable-units aggregation queries so email data is included.
  • Wrapped sum(Notification.billable_units) with coalesce(..., 0) in today’s billable-units stats query.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 389 to 401
stats_from_facts = (
db.session.query(
FactNotificationStatus.notification_type.label("notification_type"),
FactNotificationStatus.notification_status.label("status"),
*([FactNotificationStatus.template_id.label("template_id")] if by_template else []),
*([func.sum(FactNotificationStatus.notification_count).label("count")]),
*([func.sum(FactNotificationStatus.billable_units).label("billable_units")]),
)
.filter(
FactNotificationStatus.service_id == service_id,
FactNotificationStatus.bst_date >= start_time,
FactNotificationStatus.key_type != KEY_TYPE_TEST,
FactNotificationStatus.notification_type == SMS_TYPE,
)
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

The behavior of this query changes to include non-SMS notification types. There are existing unit tests for fetch_notification_billable_units_for_service_for_today_and_7_previous_days, but they currently only cover SMS, so this change isn’t protected against regressions (eg email totals remaining missing/0). Add a test case that creates email (and/or letter) ft_notification_status rows and verifies they are returned/aggregated correctly.

Copilot uses AI. Check for mistakes.
Comment on lines 413 to 425
stats_for_today = (
db.session.query(
Notification.notification_type.cast(db.Text),
Notification.status,
*([Notification.template_id] if by_template else []),
*([func.count().label("count")]),
*([func.sum(Notification.billable_units).label("billable_units")]),
*([func.coalesce(func.sum(Notification.billable_units), 0).label("billable_units")]),
)
.filter(
Notification.created_at >= start_time,
Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST,
Notification.notification_type == SMS_TYPE,
)
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Now that today’s stats include email notifications, add test coverage that exercises the email path end-to-end (including that billable_units comes back as 0 for email rows). Current billable-units tests only create SMS notifications, so they won’t catch regressions in this new aggregation logic.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@whabanks whabanks left a comment

Choose a reason for hiding this comment

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

LGTM, tested with FF_USE_BILLABLE_UNITS both on and off. in Admin/API. Email counts look good, no more 0's 🚀

@smcmurtry smcmurtry enabled auto-merge (squash) April 2, 2026 15:19
@smcmurtry smcmurtry merged commit 15bf967 into main Apr 2, 2026
9 checks passed
@smcmurtry smcmurtry deleted the fix/dashboard-emails-0 branch April 2, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants