Module: LlmCostTracker::Ledger::CallMetrics
- Included in:
- Call
- Defined in:
- app/models/llm_cost_tracker/ledger/call_metrics.rb
Instance Method Summary collapse
- #average_latency_ms ⇒ Object
- #cost_by_model(limit: nil) ⇒ Object
- #cost_by_provider(limit: nil) ⇒ Object
- #cost_by_tag(key, limit: nil) ⇒ Object
- #group_by_tag(key) ⇒ Object
- #latency_by_model ⇒ Object
- #latency_by_provider ⇒ Object
- #tag_value_expression(key, table_name: quoted_table_name) ⇒ Object
- #total_cost ⇒ Object
- #total_tokens ⇒ Object
Instance Method Details
#average_latency_ms ⇒ Object
38 39 40 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 38 def average_latency_ms average(:latency_ms)&.to_f end |
#cost_by_model(limit: nil) ⇒ Object
16 17 18 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 16 def cost_by_model(limit: nil) cost_by_column(:model, limit: limit) end |
#cost_by_provider(limit: nil) ⇒ Object
20 21 22 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 20 def cost_by_provider(limit: nil) cost_by_column(:provider, limit: limit) end |
#cost_by_tag(key, limit: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 28 def cost_by_tag(key, limit: nil) expression = tag_value_expression(key) label_expression = "COALESCE(NULLIF(#{expression}, ''), #{connection.quote('(untagged)')})" relation = select("#{label_expression} AS name, COALESCE(SUM(total_cost), 0) AS total_cost") .group(Arel.sql(label_expression)) .order(Arel.sql("COALESCE(SUM(total_cost), 0) DESC")) relation = relation.limit(limit) if limit relation end |
#group_by_tag(key) ⇒ Object
24 25 26 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 24 def group_by_tag(key) group(Arel.sql(tag_value_expression(key))) end |
#latency_by_model ⇒ Object
42 43 44 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 42 def latency_by_model group(:model).average(:latency_ms).transform_values(&:to_f) end |
#latency_by_provider ⇒ Object
46 47 48 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 46 def latency_by_provider group(:provider).average(:latency_ms).transform_values(&:to_f) end |
#tag_value_expression(key, table_name: quoted_table_name) ⇒ Object
50 51 52 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 50 def tag_value_expression(key, table_name: quoted_table_name) Ledger::Tags::Sql.value_expression(self, key, table_name: table_name) end |
#total_cost ⇒ Object
8 9 10 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 8 def total_cost sum(:total_cost).to_f end |
#total_tokens ⇒ Object
12 13 14 |
# File 'app/models/llm_cost_tracker/ledger/call_metrics.rb', line 12 def total_tokens sum(:total_tokens).to_i end |