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



28
29
30
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 28

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

.cost_by_providerObject



32
33
34
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 32

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

.daily_costs(days: 30) ⇒ Object



36
37
38
39
40
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 36

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

.total_costObject

Aggregations



20
21
22
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 20

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

.total_tokensObject



24
25
26
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 24

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

Instance Method Details

#featureObject



48
49
50
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 48

def feature
  parsed_tags["feature"]
end

#parsed_tagsObject



42
43
44
45
46
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 42

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

#user_idObject



52
53
54
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 52

def user_id
  parsed_tags["user_id"]
end