Class: LlmCostTracker::Dashboard::TagBreakdown

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

Overview

Aggregates calls grouped by the distinct values of a single tag key. Invalid keys raise InvalidFilterError so controllers can return HTTP 400.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, key:) ⇒ TagBreakdown

Returns a new instance of TagBreakdown.



21
22
23
24
# File 'app/services/llm_cost_tracker/dashboard/tag_breakdown.rb', line 21

def initialize(scope:, key:)
  @scope = scope
  @key = LlmCostTracker::TagKey.validate!(key, error_class: LlmCostTracker::InvalidFilterError)
end

Class Method Details

.call(key:, scope: LlmCostTracker::LlmApiCall.all) ⇒ Object



16
17
18
# File 'app/services/llm_cost_tracker/dashboard/tag_breakdown.rb', line 16

def call(key:, scope: LlmCostTracker::LlmApiCall.all)
  new(scope: scope, key: key).rows
end

Instance Method Details

#rowsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/llm_cost_tracker/dashboard/tag_breakdown.rb', line 26

def rows
  costs = scope.cost_by_tag(key)
  counts = counts_by_tag

  costs.map do |value, total_cost|
    calls = counts[value].to_i
    total_cost = total_cost.to_f

    TagBreakdownRow.new(
      value: value,
      calls: calls,
      total_cost: total_cost,
      average_cost_per_call: calls.positive? ? total_cost / calls : 0.0
    )
  end
end