Class: LlmCostTracker::Dashboard::DataQuality

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

Class Method Summary collapse

Class Method Details

.call(scope: LlmCostTracker::Ledger::Call.all) ⇒ Object



10
11
12
13
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 10

def call(scope: LlmCostTracker::Ledger::Call.all)
  model = scope.klass
  scope.unscope(:order).select(aggregate_selects(scope, model:)).take
end

.hidden_output_summary(stats) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 60

def hidden_output_summary(stats)
  output_tokens = stats.output_tokens.to_i
  return unless output_tokens.positive?

  {
    hidden_output_tokens: stats.hidden_output_tokens.to_i,
    output_tokens: output_tokens,
    share_percent: stats.hidden_output_share.to_f
  }
end

.unknown_pricing_by_model(scope) ⇒ Object



15
16
17
18
19
20
21
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 15

def unknown_pricing_by_model(scope)
  scope.unknown_pricing
       .group(:model)
       .order(Arel.sql("COUNT(*) DESC"))
       .select("model, COUNT(*) AS calls")
       .limit(10)
end

.usage_rows(stats) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 23

def usage_rows(stats)
  billable_tokens = stats.billable_tokens.to_f

  rows = Pricing::COMPONENTS.map do |component|
    token_key = component.token_key
    cost_key = component.cost_key
    token_value = stats[token_key].to_i
    share_percent = if billable_tokens.positive?
                      (token_value.to_f / billable_tokens) * 100.0
                    else
                      0.0
                    end

    {
      price_key: component.price_key,
      token_key: token_key,
      cost_key: cost_key,
      token_value: token_value,
      cost_value: stats[cost_key],
      share_percent: share_percent,
      share_basis: nil
    }
  end

  rows + [
    {
      price_key: nil,
      token_key: :hidden_output_tokens,
      cost_key: nil,
      token_value: stats.hidden_output_tokens.to_i,
      cost_value: nil,
      share_percent: stats.hidden_output_share.to_f,
      share_basis: :output
    }
  ]
end