- For an hourly interval we should default to the last 24 hours
- For daily, we should look back 7 days
- For weekly we should default to 6 weeks
Implementation Plan
Summary
Currently, when users load the dashboard without explicit date parameters, no default time range is applied - the system shows all available data or uses the bounds of the existing data. This feature adds intelligent default date ranges based on the selected interval:
- Hourly: Last 24 hours (yesterday to today)
- Daily: Last 7 days
- Weekly: Last 6 weeks
The implementation follows the existing filter architecture by adding default date calculation to the FilterStats concern, while preserving user-specified dates when explicitly provided.
Approach
- Add interval-specific default date calculation methods to
FilterStats
- Modify
current_start_date to return a default when no explicit date is provided
- Keep
current_end_date returning nil by default (meaning "now")
- Update
DashboardFiltersComponent to show the default dates in the UI
- Ensure the Stimulus controller preserves behavior when clearing dates (should revert to defaults, not show all data)
Files to Modify
| File |
Changes |
app/controllers/concerns/filter_stats.rb |
Add default_start_date method with interval-based logic; update current_start_date to fall back to default |
app/components/dashboard_filters_component.rb |
Update display_summary to handle default ranges |
app/javascript/controllers/site_dashboard_controller.js |
Update clearDateRange to signal intent to use defaults vs explicit empty |
test/controllers/sites_controller_test.rb |
Add tests for default date range behavior per interval |
Detailed Changes
app/controllers/concerns/filter_stats.rb
Add a new method to calculate the default start date based on the current interval:
def default_start_date
case current_interval
when "hourly"
1.day.ago.beginning_of_day.to_date
when "weekly"
6.weeks.ago.beginning_of_week.to_date
else # daily
7.days.ago.to_date
end
end
Update current_start_date (lines 78-80) to use the default when no explicit date is provided:
def current_start_date
@current_start_date ||= parse_date_param(:start_date) || default_start_date
end
app/components/dashboard_filters_component.rb
The display_summary method (lines 38-50) already handles nil dates gracefully. Since defaults will now always be present, it will show proper date ranges.
app/javascript/controllers/site_dashboard_controller.js
When users clear the date range, the current behavior sends empty strings. Since the backend will now apply defaults when dates are missing, the "clear" action will reset to the interval's default range rather than showing all data. This is the desired behavior.
Tests
Add tests in test/controllers/sites_controller_test.rb to verify:
- Hourly interval defaults to last 24 hours (yesterday to today)
- Daily interval defaults to last 7 days
- Weekly interval defaults to last 6 weeks
- Explicit date params override defaults
Checklist
Implementation Plan
Summary
Currently, when users load the dashboard without explicit date parameters, no default time range is applied - the system shows all available data or uses the bounds of the existing data. This feature adds intelligent default date ranges based on the selected interval:
The implementation follows the existing filter architecture by adding default date calculation to the
FilterStatsconcern, while preserving user-specified dates when explicitly provided.Approach
FilterStatscurrent_start_dateto return a default when no explicit date is providedcurrent_end_datereturningnilby default (meaning "now")DashboardFiltersComponentto show the default dates in the UIFiles to Modify
app/controllers/concerns/filter_stats.rbdefault_start_datemethod with interval-based logic; updatecurrent_start_dateto fall back to defaultapp/components/dashboard_filters_component.rbdisplay_summaryto handle default rangesapp/javascript/controllers/site_dashboard_controller.jsclearDateRangeto signal intent to use defaults vs explicit emptytest/controllers/sites_controller_test.rbDetailed Changes
app/controllers/concerns/filter_stats.rbAdd a new method to calculate the default start date based on the current interval:
Update
current_start_date(lines 78-80) to use the default when no explicit date is provided:app/components/dashboard_filters_component.rbThe
display_summarymethod (lines 38-50) already handles nil dates gracefully. Since defaults will now always be present, it will show proper date ranges.app/javascript/controllers/site_dashboard_controller.jsWhen users clear the date range, the current behavior sends empty strings. Since the backend will now apply defaults when dates are missing, the "clear" action will reset to the interval's default range rather than showing all data. This is the desired behavior.
Tests
Add tests in
test/controllers/sites_controller_test.rbto verify:Checklist
default_start_datemethod toFilterStatsconcern (app/controllers/concerns/filter_stats.rb)current_start_dateto use default when no explicit date provided./bin/rails t./bin/rubocop