ratings:recalculate
Recalculate rating totals for all games from individual Rating records.
Synopsis
Description
This command recalculates the aggregated rating statistics for games based on individual rating records in the database. It updates the denormalized rating fields on the games table to ensure they match the actual rating data.
This is useful for:
Fixing rating inconsistencies
Recovering from data corruption
Updating after bulk rating imports
Maintenance and verification
Options
--game=ID
Recalculate ratings for a specific game only.
Without this option, all games are recalculated.
Usage Examples
Recalculate All Games
Recalculate ratings for all games:
Output:
Recalculate Single Game
Recalculate ratings for a specific game:
Output:
DDEV Environment
Run in DDEV:
What It Recalculates
Rating Fields
The command updates the following fields on the games table:
rating_average
Average rating score (1-5 scale):
rating_count
Total number of ratings:
rating_distribution
JSON object with rating counts:
weighted_rating
Weighted rating using Bayesian average:
How It Works
1. Fetch Games
Retrieves games to process:
All games (default)
Single game (with
--gameoption)
2. Calculate Statistics
For each game:
Counts total ratings
Calculates average rating
Builds rating distribution
Computes weighted rating
3. Update Database
Updates the game record with calculated values:
4. Report Results
Displays summary of changes and statistics.
When to Use
Required Scenarios
Run ratings:recalculate when:
After Bulk Import: After running
ratings:importorratings:backfillData Inconsistencies: Rating totals don't match individual ratings
Database Recovery: Recovering from backup or migration
Manual Rating Changes: After manually modifying rating records
Suspicious Activity: After removing suspicious ratings
Maintenance
Run periodically (e.g., monthly) to ensure data integrity.
Performance
Execution Time
Games | Estimated Time |
|---|---|
100 | < 1 minute |
1,000 | 2-3 minutes |
10,000 | 10-15 minutes |
50,000 | 30-60 minutes |
Resource Usage
CPU: Moderate (calculations)
Memory: Low (processes in batches)
Database: Moderate (reads + writes)
I/O: Moderate
Optimization
The command uses batched processing:
Processes 100 games at a time
Minimizes memory usage
Prevents timeout issues
Verification
Check Results
Verify recalculation worked:
Validate Consistency
Check for inconsistencies:
Impact on System
Database Changes
Only updates the games table:
No changes to
ratingstableNo changes to user data
Preserves all historical data
Search Index
If using Meilisearch, ratings are automatically updated in the search index via model observers.
Caching
May need to clear caches after recalculation:
Troubleshooting
Slow Performance
Problem: Recalculation is very slow
Solutions:
Run during off-peak hours
Use
--gameoption for specific gamesIncrease batch size in code
Check database indexes
Optimize database queries
Memory Issues
Problem: Out of memory errors
Solutions:
Increase PHP memory limit
Reduce batch size
Process games in smaller chunks
Restart PHP-FPM
Inconsistent Results
Problem: Results don't match expectations
Solutions:
Verify rating data in database
Check for orphaned ratings
Look for duplicate ratings
Verify calculation logic
Database Locks
Problem: Database lock timeouts
Solutions:
Run during low-traffic periods
Reduce batch size
Check for long-running queries
Optimize database configuration
Best Practices
After Imports: Always run after bulk rating imports
Regular Schedule: Run monthly for data integrity
Backup First: Backup database before large recalculations
Monitor Progress: Watch output for errors
Verify Results: Spot-check results after completion
Off-Peak Hours: Run during low-traffic periods
Related Commands
ratings:import - Import latest ratings from itch.io
ratings:backfill - Backfill missing ratings
rater:mark-suspicious - Mark suspicious raters
See Also
Game model rating fields
Rating calculation algorithms