Merged
Conversation
Added `fetch_monthly_template_usage_for_service_paginated` dao method that performs pagination at the query level, and runs the expensive part of the query only on templates on the current "page".
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces database-level pagination for the admin “Monthly Template Usage” endpoint to avoid loading usage stats for all templates at once, aiming to improve performance for large services.
Changes:
- Added a new DAO method
fetch_monthly_template_usage_for_service_paginatedthat pages by unique templates using a two-step query (page template_ids first, then fetch monthly rows for those templates). - Updated
GET /service/<id>/notifications/templates_usage/monthlyto return a paginated response shape when apagequery param is provided. - Added DAO- and REST-level tests covering pagination shape, ordering, offsets, and basic validation.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
app/dao/fact_notification_status_dao.py |
Adds the new paginated DAO query for monthly template usage. |
app/service/rest.py |
Adds query-param parsing and paginated response mode to the monthly template usage endpoint. |
tests/app/dao/test_fact_notification_status_dao.py |
Adds unit tests for the new paginated DAO behavior. |
tests/app/service/test_statistics_rest.py |
Adds endpoint tests for paginated response shape, ordering, links, and invalid args. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Added a request schema for `get_monthly_template_usage` as qParam validation was getting out of hand - Ensure that we also fetch from the `notifications` table when counting template stats and today's date falls in the start->end range - Fix a rare edge case where two templates have the same notification count associated with them that could cause inconsistencies in paging results
3 tasks
andrewleith
approved these changes
May 6, 2026
This was referenced May 6, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary | Résumé
This PR aims to improve performance of the "Monthly Template Usage" page in admin with optimizations to the underlying query. For the average service load times should be similar, the real gain is for services with high sending volume across a large number of templates. The JSON payload from the API is bound by the page size, and the browser won't choke trying to render thousands of table rows.
Added
fetch_monthly_template_usage_for_service_paginateddao method that performs pagination at the query level, splitting the query into two and running the more expensive par on the templates on the current "page" only.template_ids, sorting them by count descending. We use a combination ofoffsetandlimitto construct the page.Related Issues | Cartes liées
Test instructions | Instructions pour tester la modification
Release Instructions | Instructions pour le déploiement
mainto ensure backwards compatibility (not the associated admin PR)/template-usagepage loads without error (See all templates used link on the dashboard)Reviewer checklist | Liste de vérification du réviseur