Implement ride count fields with real-time annotations; update filters and templates for consistency and accuracy

This commit is contained in:
pacnpal
2025-02-13 16:44:30 -05:00
parent 9d6f6dab2c
commit c19aaf2f4b
10 changed files with 988 additions and 367 deletions

View File

@@ -0,0 +1,59 @@
# Park Count Fields Implementation
## Context
While implementing park views, we encountered errors where `ride_count` and `coaster_count` annotations conflicted with existing model fields of the same names. Additionally, we discovered inconsistencies in how these counts were being used across different views.
## Decision
We decided to use both approaches but with distinct names:
1. **Model Fields**:
- `ride_count`: Stored count of all rides
- `coaster_count`: Stored count of roller coasters
- Used in models and database schema
- Required for backward compatibility
2. **Annotations**:
- `current_ride_count`: Real-time count of all rides
- `current_coaster_count`: Real-time count of roller coasters
- Provide accurate, up-to-date counts
- Used in templates and filters
This approach allows us to:
- Maintain existing database schema
- Show accurate, real-time counts in the UI
- Avoid name conflicts between fields and annotations
- Keep consistent naming pattern for both types of counts
## Implementation
1. Views:
- Added base queryset method with annotations
- Used 'current_' prefix for annotated counts
- Ensured all views use the base queryset
2. Filters:
- Updated filter fields to use annotated counts
- Configured filter class to always use base queryset
- Maintained filter functionality with new field names
3. Templates:
- Updated templates to use computed counts
## Why This Pattern
1. **Consistency**: Using the 'current_' prefix clearly indicates which values are computed in real-time
2. **Compatibility**: Maintains support for existing code that relies on the stored fields
3. **Flexibility**: Allows gradual migration from stored to computed counts if desired
4. **Performance Option**: Keeps the option to use stored counts for expensive queries
## Future Considerations
We might want to:
1. Add periodic tasks to sync stored counts with computed values
2. Consider deprecating stored fields if they're not needed for performance
3. Add validation to ensure stored counts stay in sync with reality
4. Create a management command to update stored counts
## Related Files
- parks/models.py
- parks/views.py
- parks/filters.py
- parks/templates/parks/partials/park_list_item.html
- parks/tests/test_filters.py

View File

@@ -0,0 +1,39 @@
# Ride Count Field Implementation
## Context
While implementing park views, we encountered an error where a `ride_count` annotation conflicted with an existing model field of the same name. This raised a question about how to handle real-time ride counts versus stored counts.
## Decision
We decided to use both approaches but with distinct names:
1. **Model Field (`ride_count`)**:
- Kept the original field for backward compatibility
- Used in test fixtures and filtering system
- Can serve as a cached/denormalized value
2. **Annotation (`current_ride_count`)**:
- Added new annotation with a distinct name
- Provides real-time count of rides
- Used in templates for display purposes
This approach allows us to:
- Maintain existing functionality in tests and filters
- Show accurate, real-time counts in the UI
- Avoid name conflicts between fields and annotations
## Implementation
- Kept the `ride_count` IntegerField in the Park model
- Added `current_ride_count = Count('rides', distinct=True)` annotation in views
- Updated templates to use `current_ride_count` for display
## Future Considerations
We might want to:
1. Add a periodic task to sync the stored `ride_count` with the computed value
2. Consider deprecating the stored field if it's not needed for performance
3. Add validation to ensure the stored count stays in sync with reality
## Related Files
- parks/models.py
- parks/views.py
- parks/templates/parks/partials/park_list_item.html
- parks/tests/test_filters.py