Skip to content

cache improvements#258

Draft
dbosk wants to merge 8 commits intomasterfrom
cache-improvements
Draft

cache improvements#258
dbosk wants to merge 8 commits intomasterfrom
cache-improvements

Conversation

@dbosk
Copy link
Owner

@dbosk dbosk commented Nov 27, 2025

Supercedes #251.

  • Deduplicate ungraded submission check and fix cache consistency
  • Add threshold-based bulk refresh optimization to submission caching
  • Enable bulk refresh for ungraded submissions and unfiltered listings
  • Add comprehensive documentation for bulk refresh optimization

dbosk and others added 8 commits November 27, 2025 19:43
Remove bucket parameter and create is_submission_ungraded() helper:

Cache consistency fix:
- Remove bucket="ungraded" parameter from get_submissions() calls
- Always fetch all submissions and filter locally
- Document why bucket parameter causes cache inconsistencies
- Move submission.assignment assignment outside filter condition
- Add comprehensive documentation of filtering strategy

Code quality improvement:
- Create is_submission_ungraded() helper function in submissions.nw
- Deduplicate inline check from three locations
- Update submissions.nw to use helper in list_ungraded_submissions()
- Update assignments.nw to import and use helper in list_assignments()
- Update results.nw to use helper instead of timestamp comparison
- Fix results.nw to use grade_matches_current_submission (more accurate)

Benefits:
- Fixes cache inconsistency when mixing bucket/non-bucket calls
- Single source of truth for ungraded logic
- Eliminates code duplication across three modules
- More accurate detection in results.nw (uses grade_matches vs timestamps)
- Better maintainability - changes only need to happen in one place

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Adds opt-in bulk refresh mechanism to reduce API calls when many cached
submissions are stale. Key changes:

- Add logging and os imports for logging module and env var reading
- Add BULK_REFRESH_THRESHOLD and BULK_REFRESH_MIN_COUNT constants
  configurable via environment variables (default 0.2 and 3)
- Add would_need_refresh() method to LazySubmission for non-mutating
  staleness checks
- Add check_bulk_refresh parameter to get_submissions decorator
- Implement pre-check phase that counts stale submissions and triggers
  bulk fetch if threshold exceeded (1 API call instead of N individual)
- Update wrappers in-place preserving object identity

Performance improvement: 40 API calls → 1 API call for -U scenarios.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Modifies submission listing functions to enable bulk refresh optimization:
- list_ungraded_submissions() always passes check_bulk_refresh=True since
  it accesses mutable attributes on all submissions to filter ungraded ones
- list_submissions() accepts check_bulk_refresh parameter and passes it
  through to get_submissions()
- Calling code enables bulk refresh when no user/category/group filters
  are applied (accesses all submissions)

This reduces API calls from N to 1 when using -U flag or listing all
submissions without filters.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Adds detailed literate programming documentation explaining the
threshold-based bulk refresh optimization:

- Performance bottleneck: lazy loading triggers N individual API calls
  when accessing all submissions (e.g., -U flag with 40 stale = 40 calls)
- Solution: pre-check phase counts stale submissions, triggers bulk
  refresh if threshold exceeded (1 API call instead of N)
- When to enable: -U flag, unfiltered listings (accessing all submissions)
- Threshold configuration: 20% with minimum 3, configurable via env vars
- Trade-offs: dramatic speedup vs pre-check overhead
- Design rationale: why decorator not wrapper, why opt-in not automatic

Documentation uses variation theory: contrasts individual vs bulk refresh,
explains the "whole" (problem), decomposes into "parts" (solution details),
and brings back together (when/how to use).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
…hacks/canvasapi.nw by keeping lazy submission caching with bulk refresh and preserving logging imports
@dbosk
Copy link
Owner Author

dbosk commented Dec 2, 2025

Merged an alternative solution in master. Should probably use the idea of LazySubmission from this branch at some point in the future.

@dbosk dbosk marked this pull request as draft December 2, 2025 09:14
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.

1 participant