Skip to content

Add better default time ranges  #183

@mjc-gh

Description

@mjc-gh
  • 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

  1. Add interval-specific default date calculation methods to FilterStats
  2. Modify current_start_date to return a default when no explicit date is provided
  3. Keep current_end_date returning nil by default (meaning "now")
  4. Update DashboardFiltersComponent to show the default dates in the UI
  5. 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:

  1. Hourly interval defaults to last 24 hours (yesterday to today)
  2. Daily interval defaults to last 7 days
  3. Weekly interval defaults to last 6 weeks
  4. Explicit date params override defaults

Checklist

  • Add default_start_date method to FilterStats concern (app/controllers/concerns/filter_stats.rb)
  • Update current_start_date to use default when no explicit date provided
  • Add test: hourly interval defaults to last 24 hours
  • Add test: daily interval defaults to last 7 days
  • Add test: weekly interval defaults to last 6 weeks
  • Add test: explicit start_date param overrides default
  • Run full test suite: ./bin/rails t
  • Run Rubocop: ./bin/rubocop
  • Run Brakeman security check

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions