Class: LlmCostTracker::Dashboard::TopModels

Inherits:
Object
  • Object
show all
Defined in:
app/services/llm_cost_tracker/dashboard/top_models.rb

Constant Summary collapse

DEFAULT_LIMIT =
5
SORT_OPTIONS =
{
  "cost"     => "total_cost_sum DESC",
  "calls"    => "calls_count DESC",
  "avg_cost" => "total_cost_sum / calls_count DESC",
  "latency"  => "average_latency DESC NULLS LAST"
}.freeze
DEFAULT_SORT =
"cost"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, limit:, sort: DEFAULT_SORT) ⇒ TopModels

Returns a new instance of TopModels.



32
33
34
35
36
# File 'app/services/llm_cost_tracker/dashboard/top_models.rb', line 32

def initialize(scope:, limit:, sort: DEFAULT_SORT)
  @scope = scope
  @limit = limit
  @sort = SORT_OPTIONS.key?(sort.to_s) ? sort.to_s : DEFAULT_SORT
end

Class Method Details

.call(scope: LlmCostTracker::LlmApiCall.all, limit: DEFAULT_LIMIT, sort: DEFAULT_SORT) ⇒ Object



27
28
29
# File 'app/services/llm_cost_tracker/dashboard/top_models.rb', line 27

def call(scope: LlmCostTracker::LlmApiCall.all, limit: DEFAULT_LIMIT, sort: DEFAULT_SORT)
  new(scope: scope, limit: limit, sort: sort).rows
end

Instance Method Details

#rowsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/llm_cost_tracker/dashboard/top_models.rb', line 38

def rows
  grouped_rows.map do |row|
    calls = row.calls_count.to_i
    total_cost = row.total_cost_sum.to_f

    TopModel.new(
      provider: row.provider,
      model: row.model,
      calls: calls,
      total_cost: total_cost,
      average_cost_per_call: calls.positive? ? total_cost / calls : 0.0,
      input_tokens: row.input_tokens_sum.to_i,
      output_tokens: row.output_tokens_sum.to_i,
      average_latency_ms: average_latency(row)
    )
  end
end