Class: LlmCostTracker::Dashboard::DateRange

Inherits:
Object
  • Object
show all
Defined in:
app/services/llm_cost_tracker/dashboard/date_range.rb

Constant Summary collapse

DEFAULT_DAYS =
30
MAX_DAYS =
366

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, today:) ⇒ DateRange

Returns a new instance of DateRange.



34
35
36
37
38
39
# File 'app/services/llm_cost_tracker/dashboard/date_range.rb', line 34

def initialize(params:, today:)
  @to = self.class.parse(params, :to) || today
  @from = self.class.parse(params, :from) || (@to - (DEFAULT_DAYS - 1))
  self.class.validate!(from: @from, to: @to)
  freeze
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



9
10
11
# File 'app/services/llm_cost_tracker/dashboard/date_range.rb', line 9

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



9
10
11
# File 'app/services/llm_cost_tracker/dashboard/date_range.rb', line 9

def to
  @to
end

Class Method Details

.call(params:, today: Date.current) ⇒ Object



11
12
13
# File 'app/services/llm_cost_tracker/dashboard/date_range.rb', line 11

def self.call(params:, today: Date.current)
  new(params: params, today: today)
end

.parse(params, key) ⇒ Object



15
16
17
18
19
20
21
22
# File 'app/services/llm_cost_tracker/dashboard/date_range.rb', line 15

def self.parse(params, key)
  value = LlmCostTracker::ParameterHash.with_indifferent_access(params)[key].to_s.strip
  return nil if value.empty?

  Date.iso8601(value)
rescue ArgumentError
  nil
end

.validate!(from:, to:) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
# File 'app/services/llm_cost_tracker/dashboard/date_range.rb', line 24

def self.validate!(from:, to:)
  return if from.nil? && to.nil?

  raise InvalidFilterError, "from and to dates must be provided together" if from.nil? || to.nil?
  raise InvalidFilterError, "from date must be on or before to date" if from > to
  return if ((to - from).to_i + 1) <= MAX_DAYS

  raise InvalidFilterError, "date range cannot exceed #{MAX_DAYS} days"
end