Class: LlmCostTracker::Call

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/llm_cost_tracker/call.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.average_latency_msObject



92
# File 'app/models/llm_cost_tracker/call.rb', line 92

def average_latency_ms = average(:latency_ms)&.to_f

.by_tag(key, value) ⇒ Object



61
# File 'app/models/llm_cost_tracker/call.rb', line 61

def by_tag(key, value) = by_tags(key => value)

.by_tags(tags) ⇒ Object



63
# File 'app/models/llm_cost_tracker/call.rb', line 63

def by_tags(tags) = Ledger::Tags::Query.apply(tags)

.cost_by_model(limit: nil) ⇒ Object



69
# File 'app/models/llm_cost_tracker/call.rb', line 69

def cost_by_model(limit: nil) = cost_by_column(:model, limit: limit)

.cost_by_provider(limit: nil) ⇒ Object



71
# File 'app/models/llm_cost_tracker/call.rb', line 71

def cost_by_provider(limit: nil) = cost_by_column(:provider, limit: limit)

.cost_by_tag(key, limit: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/llm_cost_tracker/call.rb', line 77

def cost_by_tag(key, limit: nil)
  label = Ledger::Tags::Sql.label_sql(connection)
  raw_value = Ledger::Tags::Sql.raw_value_sql(connection)
  relation = Ledger::Tags::Sql.join_relation(self, key)
                              .select("#{label} AS name", "COALESCE(SUM(total_cost), 0) AS total_cost")
                              .group(Arel.sql(label))
                              .order(
                                Arel.sql("COALESCE(SUM(total_cost), 0) DESC"),
                                Arel.sql("MAX(CASE WHEN #{raw_value} IS NULL THEN 1 ELSE 0 END) ASC"),
                                Arel.sql("#{label} DESC")
                              )
  relation = relation.limit(limit) if limit
  relation
end

.daily_costs(days: 30) ⇒ Object



102
103
104
105
106
# File 'app/models/llm_cost_tracker/call.rb', line 102

def daily_costs(days: 30)
  where(tracked_at: days.days.ago..)
    .group_by_period(:day)
    .sum(:total_cost)
end

.group_by_period(period, column: :tracked_at) ⇒ Object



98
99
100
# File 'app/models/llm_cost_tracker/call.rb', line 98

def group_by_period(period, column: :tracked_at)
  group(Arel.sql(period_group_expression(period, column: column)))
end

.group_by_tag(key) ⇒ Object



73
74
75
# File 'app/models/llm_cost_tracker/call.rb', line 73

def group_by_tag(key)
  Ledger::Tags::Sql.join_relation(self, key).group(Ledger::Tags::Sql.value_arel)
end

.latency_by_modelObject



94
# File 'app/models/llm_cost_tracker/call.rb', line 94

def latency_by_model = group(:model).average(:latency_ms).transform_values(&:to_f)

.latency_by_providerObject



96
# File 'app/models/llm_cost_tracker/call.rb', line 96

def latency_by_provider = group(:provider).average(:latency_ms).transform_values(&:to_f)

.total_costObject



65
# File 'app/models/llm_cost_tracker/call.rb', line 65

def total_cost = sum(:total_cost).to_f

.total_tokensObject



67
# File 'app/models/llm_cost_tracker/call.rb', line 67

def total_tokens = sum(:total_tokens).to_i

Instance Method Details

#parsed_tagsObject



154
155
156
157
158
# File 'app/models/llm_cost_tracker/call.rb', line 154

def parsed_tags
  tag_records.to_h do |record|
    [record.key, record.value]
  end
end