Skip to content

fix(stats): compute week/month cutoffs at call time (#13789) - #13808

Open
LalitNarayanYadav wants to merge 1 commit into
internetarchive:masterfrom
LalitNarayanYadav:13789/fix/stale-date-cutoffs
Open

LalitNarayanYadav wants to merge 1 commit into
internetarchive:masterfrom
LalitNarayanYadav:13789/fix/stale-date-cutoffs

Conversation

@LalitNarayanYadav

Copy link
Copy Markdown
Contributor

Closes #13789

fix: week/month stats cutoffs are now computed when used, not at import time.

Technical

The constants DATE_ONE_DAY_AGO, DATE_ONE_WEEK_AGO, DATE_ONE_MONTH_AGO and DATE_ONE_YEAR_AGO in utils/dateutil.py were evaluated once when the module loaded, so a long-running server kept using its start date.

I turned them into functions (date_one_week_ago() etc.) and updated every place that used them: the summary() methods in core/ and reading_log_leaderboard in views/loanstats.py. The month calculation is unchanged; I'm leaving the month-length question for a separate issue.

Testing

Added test_date_cutoffs_are_computed_at_call_time in openlibrary/utils/tests/test_dateutil.py. It moves a fake clock forward a month and checks the cutoffs follow it; it fails on the old code.

I ran pytest openlibrary/utils/tests/test_dateutil.py locally (6 passed). I did not run the full suite or load the stats page, so I'm relying on CI for those.

Screenshot

N/A (no UI change)

Stakeholders

@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR, @LalitNarayanYadav!

🤖 A reviewer has been assigned for an initial pass.

PR triage checklist (maintainers / Pierre)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — no failing check-runs
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

Note

This comment was automatically generated by Pierre, Open Library's PR first-touch assistant, on behalf of @mekarpeles. This is NOT a code review or sign-off.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stats "past week/month" cutoffs are computed at import time and go stale in long-running processes

3 participants