Skip to content

Conversation

@JoaquinBN
Copy link
Collaborator

Performance Optimizations

  • Reduced queries: From ~2N+1 to 2 constant queries (N = number of validators)
    • Query 1: Fetch all validators + creation dates
    • Query 2: Fetch max uptime date per validator
  • Eliminated duplicate checks: Start from last_uptime + 1 day, making existence checks unnecessary
  • Simplified leaderboard updates: Use update_user_leaderboard_entries() instead of manual entry management

Logic Improvements

  • Validators with uptime: generate from last_uptime_date + 1 day to today
  • New validators: generate from validator.created_at to today
  • Prevents duplicate contributions by design

Model Change (⚠️ Needs Review)

File: backend/contributions/models.py

Changed Contribution.set_multiplier() behavior:

  • Before: Raised ValidationError when no multiplier exists
  • After: Uses default multiplier of 1.0

This change was needed due to an error when using the new database. It may indicate a migration issue - please review to confirm this is the correct approach rather than a missing multiplier in the database.

Testing

Added comprehensive test suite in test_add_uptime.py covering:

  • Validators with existing uptime
  • New validators without uptime
  • Idempotency (no duplicates on re-run)
  • Performance validation

Performance optimizations:
- Reduced database queries from ~2N+1 to 2 constant queries
- Batch fetch all validators and their creation dates (1 query)
- Batch fetch max uptime date per validator (1 query)
- Start from day after last uptime, eliminating need for duplicate checks
- Simplified leaderboard updates using update_user_leaderboard_entries

Logic improvements:
- For validators with existing uptime: start from last_uptime_date + 1 day
- For new validators: start from validator.created_at date
- Prevents duplicate contributions by design

Model change (contributions.models.py):
- Changed Contribution.set_multiplier() to use default 1.0 multiplier instead of raising ValidationError
- This handles cases where no multiplier exists for a contribution type/date
- Consistent with --force flag behavior in management commands

Note: The model change may indicate a migration issue - the previous behavior
raised ValidationError when no multiplier existed, but the new behavior silently
defaults to 1.0. This should be reviewed to ensure it aligns with system expectations.
@JoaquinBN JoaquinBN self-assigned this Oct 23, 2025
@JoaquinBN JoaquinBN requested a review from albert-mr October 23, 2025 09:23
@albert-mr
Copy link
Collaborator

albert-mr commented Oct 23, 2025

everything looks fine to me 👍 + already clarified on discord why self.multiplier_at_creation is hardcoded fallback to 1.0

@JoaquinBN JoaquinBN linked an issue Nov 29, 2025 that may be closed by this pull request
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.

Update uptime script (in admin) not working

3 participants