Class: LlmCostTracker::Dashboard::TimeSeries

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

Constant Summary collapse

DEFAULT_DAYS =
30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, from:, to:) ⇒ TimeSeries

Returns a new instance of TimeSeries.



16
17
18
19
20
# File 'app/services/llm_cost_tracker/dashboard/time_series.rb', line 16

def initialize(scope:, from:, to:)
  @scope = scope
  @to = to.to_date
  @from = from ? from.to_date : (@to - (DEFAULT_DAYS - 1))
end

Class Method Details

.call(scope: LlmCostTracker::LlmApiCall.all, from: nil, to: Date.current) ⇒ Object



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

def call(scope: LlmCostTracker::LlmApiCall.all, from: nil, to: Date.current)
  new(scope: scope, from: from, to: to).points
end

Instance Method Details

#pointsObject



22
23
24
25
26
27
28
29
# File 'app/services/llm_cost_tracker/dashboard/time_series.rb', line 22

def points
  costs = scoped_costs

  (from..to).map do |date|
    label = date.iso8601
    { label: label, cost: costs.fetch(label, 0.0) }
  end
end