Class: LlmCostTracker::Ledger::Period::Totals

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/ledger/period/totals.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(periods, time:) ⇒ Totals

Returns a new instance of Totals.



15
16
17
18
# File 'lib/llm_cost_tracker/ledger/period/totals.rb', line 15

def initialize(periods, time:)
  @periods = Period.valid_keys(periods)
  @time = time
end

Class Method Details

.call(periods, time:) ⇒ Object



11
12
13
# File 'lib/llm_cost_tracker/ledger/period/totals.rb', line 11

def self.call(periods, time:)
  new(periods, time: time).totals
end

Instance Method Details

#totalsObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/llm_cost_tracker/ledger/period/totals.rb', line 20

def totals
  return {} if periods.empty?

  values = periods.to_h { |period| [period, BigDecimal("0")] }
  period_by_name = periods.to_h { |period| [period.to_s, period] }
  LlmCostTracker::Call.find_by_sql(union_sql).each do |row|
    values[period_by_name.fetch(row.period_key)] = row.total_cost.to_d
  end
  values
end