Skip to content

Optimize stats api - #465

Merged
duckduckgrayduck merged 3 commits into
masterfrom
stats_api_optimize
Sep 14, 2026
Merged

duckduckgrayduck merged 3 commits into
masterfrom
stats_api_optimize

Conversation

@duckduckgrayduck

@duckduckgrayduck duckduckgrayduck commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Unfortunately I was hitting timeouts on pages with users with large document sets on production. Wasn't experiencing this on staging, but staging is much smaller.

This approach moves to stored fields for total_documents and recent_upload_count for the stats_api. It means we don't have to annotate per page anymore, so that's gone. The loss is we don't get a live statistic for these counts, but considering this gets synced once daily anyway, this tradeoff is necessary. There is a management command (that I tested in staging) that recomputes these two fields and a Celery task that runs this command for us nightly.

The compute queries the Django management command calls I ran in the Django shell on production to test, these are their times:

user totals scan: 59.59 s
user recents scan: 12.2 s
org totals scan: 53.08 s
org recents scan: 0.47 s
I think is acceptable for a nightly scan. Not something acceptable for a web request, but in a Celery task, definitely.

The computed stats approach seems to be no issue on MuckRock's side, so I'm not touching that for now.

aged_out is still necessary for the caller because it won't know when a recompute happens once the boundary is crossed, but I added an optimization there as well to make it cheaper and for it to short circuit per org. This also adds the timezone naiveness check that the MuckRock stats API got in feedback that didn't get caught here.

@duckduckgrayduck
duckduckgrayduck merged commit 2e24fe5 into master Sep 14, 2026
6 checks passed

This branch was previously deployed

1 inactive deployment
documentcloud-staging — be9b97e2 Deployed Sep 11, 2026 by duckduckgrayduck
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