Class: LlmCostTracker::LlmApiCall

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/llm_cost_tracker/llm_api_call.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cost_by_modelObject



30
31
32
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 30

def self.cost_by_model
  group(:model).sum(:total_cost)
end

.cost_by_providerObject



34
35
36
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 34

def self.cost_by_provider
  group(:provider).sum(:total_cost)
end

.daily_costs(days: 30) ⇒ Object



38
39
40
41
42
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 38

def self.daily_costs(days: 30)
  where(tracked_at: days.days.ago..)
    .group("DATE(tracked_at)")
    .sum(:total_cost)
end

.total_costObject

Aggregations



22
23
24
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 22

def self.total_cost
  sum(:total_cost).to_f
end

.total_tokensObject



26
27
28
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 26

def self.total_tokens
  sum(:total_tokens).to_i
end

Instance Method Details

#featureObject



50
51
52
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 50

def feature
  parsed_tags["feature"]
end

#parsed_tagsObject



44
45
46
47
48
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 44

def parsed_tags
  JSON.parse(tags || "{}")
rescue JSON::ParserError
  {}
end

#user_idObject



54
55
56
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 54

def user_id
  parsed_tags["user_id"]
end