Class: LlmCostTracker::LlmApiCall

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
TagsColumn
Includes:
TagAccessors
Defined in:
lib/llm_cost_tracker/llm_api_call.rb

Class Method Summary collapse

Methods included from TagsColumn

latency_column?, tags_json_column?

Methods included from TagAccessors

#feature, #parsed_tags, #user_id

Class Method Details

.average_latency_msObject



64
65
66
67
68
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 64

def self.average_latency_ms
  return nil unless latency_column?

  average(:latency_ms)&.to_f
end

.by_tag(key, value) ⇒ Object



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

def self.by_tag(key, value)
  by_tags(key => value)
end

.by_tags(tags) ⇒ Object



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

def self.by_tags(tags)
  TagQuery.apply(self, tags)
end

.cost_by_modelObject



56
57
58
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 56

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

.cost_by_providerObject



60
61
62
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 60

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

.daily_costs(days: 30) ⇒ Object



82
83
84
85
86
87
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 82

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

.latency_by_modelObject



70
71
72
73
74
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 70

def self.latency_by_model
  return {} unless latency_column?

  group(:model).average(:latency_ms).transform_values(&:to_f)
end

.latency_by_providerObject



76
77
78
79
80
# File 'lib/llm_cost_tracker/llm_api_call.rb', line 76

def self.latency_by_provider
  return {} unless latency_column?

  group(:provider).average(:latency_ms).transform_values(&:to_f)
end

.total_costObject

Aggregations



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

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

.total_tokensObject



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

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