Class: LlmCostTracker::Storage::ActiveRecordStore

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/storage/active_record_store.rb

Class Method Summary collapse

Class Method Details

.model_classObject



31
32
33
# File 'lib/llm_cost_tracker/storage/active_record_store.rb', line 31

def model_class
  LlmCostTracker::LlmApiCall
end

.monthly_total(time: Time.now.utc) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/llm_cost_tracker/storage/active_record_store.rb', line 22

def monthly_total(time: Time.now.utc)
  beginning_of_month = Time.new(time.year, time.month, 1, 0, 0, 0, "+00:00")

  model_class
    .where(tracked_at: beginning_of_month..time)
    .sum(:total_cost)
    .to_f
end

.save(event) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/llm_cost_tracker/storage/active_record_store.rb', line 7

def save(event)
  model_class.create!(
    provider:      event[:provider],
    model:         event[:model],
    input_tokens:  event[:input_tokens],
    output_tokens: event[:output_tokens],
    total_tokens:  event[:total_tokens],
    input_cost:    event.dig(:cost, :input_cost),
    output_cost:   event.dig(:cost, :output_cost),
    total_cost:    event.dig(:cost, :total_cost),
    tags:          event[:tags].to_json,
    tracked_at:    event[:tracked_at]
  )
end