Module: OpenRouterUsageTracker::Trackable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/open_router_usage_tracker/trackable.rb

Instance Method Summary collapse

Instance Method Details

#daily_usage_summary_for(day:, provider:, model:) ⇒ OpenRouterUsageTracker::DailySummary?

Finds a specific daily summary for a given day, provider, and model. This is the primary, high-performance method for usage checks.

Parameters:

  • day (Date)

    The date to check. Pass ‘Time.zone.today` to be timezone-aware.

  • provider (String)

    The provider name (e.g., ‘open_router’).

  • model (String)

    The model name (e.g., ‘openai/gpt-4o’).

Returns:



19
20
21
# File 'app/models/concerns/open_router_usage_tracker/trackable.rb', line 19

def daily_usage_summary_for(day:, provider:, model:)
  daily_summaries.find_by(day: day, provider: provider, model: model)
end

#total_cost_in_range(range, provider:, model: nil) ⇒ BigDecimal

Calculates the total cost from the daily summaries within a given date range. It can be filtered by provider and, optionally, by model.

Parameters:

  • range (Range<Date>)

    The date range to query (e.g., 1.month.ago.to_date..Date.current).

  • provider (String)

    The provider name (e.g., ‘open_ai’).

  • model (String, nil) (defaults to: nil)

    The optional model name.

Returns:

  • (BigDecimal)

    The total cost.



30
31
32
33
34
# File 'app/models/concerns/open_router_usage_tracker/trackable.rb', line 30

def total_cost_in_range(range, provider:, model: nil)
  summaries = daily_summaries.where(day: range, provider: provider)
  summaries = summaries.where(model: model) if model
  summaries.sum(:cost)
end