Module: LlmCostTracker::Dashboard::DataQuality
- Defined in:
- app/services/llm_cost_tracker/dashboard/data_quality.rb
Defined Under Namespace
Classes: QuarantinedInbox, StreamingHealthRow, Summary, UnknownPricingRow, UnseenBudgetTags
Class Method Summary collapse
- .call(scope: LlmCostTracker::Call.all) ⇒ Object
- .component_costs(scope) ⇒ Object
- .hidden_output_summary(stats) ⇒ Object
- .quarantined_inbox ⇒ Object
- .service_charge_rows(scope) ⇒ Object
- .streaming_health_rows(scope, total_streaming:) ⇒ Object
- .summary(stats) ⇒ Object
- .unknown_pricing_by_model(scope, total_calls:) ⇒ Object
- .unseen_budget_tags ⇒ Object
- .usage_rows(stats, component_costs: {}) ⇒ Object
Class Method Details
.call(scope: LlmCostTracker::Call.all) ⇒ Object
34 35 36 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 34 def call(scope: LlmCostTracker::Call.all) scope.unscope(:order).select(aggregate_selects(scope)).take end |
.component_costs(scope) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 168 def component_costs(scope) line_item_table = LlmCostTracker::CallLineItem.quoted_table_name rows = LlmCostTracker::CallLineItem .where(unit: "token") .joins(:call) .merge(scope.unscope(:select, :order, :group)) .group("#{line_item_table}.kind", "#{line_item_table}.direction", "#{line_item_table}.cache_state") .pluck(Arel.sql("#{line_item_table}.kind"), Arel.sql("#{line_item_table}.direction"), Arel.sql("#{line_item_table}.cache_state"), Arel.sql("COALESCE(SUM(#{line_item_table}.cost), 0)")) index_costs_by_component(rows) end |
.hidden_output_summary(stats) ⇒ Object
211 212 213 214 215 216 217 218 219 220 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 211 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 |
.quarantined_inbox ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 38 def quarantined_inbox return nil unless Ingestion.async? return nil unless Ingestion::InboxEntry.table_exists? row = Ingestion::InboxEntry .quarantined .select("COUNT(*) AS quarantined_count, COALESCE(SUM(total_cost), 0) AS quarantined_cost") .take QuarantinedInbox.new(count: row.quarantined_count.to_i, total_cost: row.quarantined_cost.to_d) end |
.service_charge_rows(scope) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 116 def service_charge_rows(scope) call_table = LlmCostTracker::Call.quoted_table_name line_item_table = LlmCostTracker::CallLineItem.quoted_table_name relation = LlmCostTracker::CallLineItem .where.not(unit: "token") .joins(:call) .merge(scope.unscope(:select, :order)) relation .group("#{call_table}.provider", "#{line_item_table}.kind", "#{line_item_table}.cost_status") .order(Arel.sql("COALESCE(SUM(#{line_item_table}.cost), 0) DESC"), Arel.sql("COUNT(*) DESC")) .select( "#{call_table}.provider AS provider", "#{line_item_table}.kind AS component", "#{line_item_table}.cost_status AS cost_status", "COUNT(*) AS charges_count", "COALESCE(SUM(#{line_item_table}.quantity), 0) AS quantity", "COALESCE(SUM(#{line_item_table}.cost), 0) AS total_cost" ) .limit(10) end |
.streaming_health_rows(scope, total_streaming:) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 184 def streaming_health_rows(scope, total_streaming:) return [] unless total_streaming.positive? unknown_predicate = unknown_usage_source_predicate(scope) rows = scope.unscope(:select, :order, :group) .where(stream: true) .group(:provider) .order(Arel.sql("COUNT(*) DESC"), :provider) .pluck( :provider, Arel.sql("COUNT(*)"), Arel.sql("SUM(CASE WHEN #{unknown_predicate} THEN 1 ELSE 0 END)") ) rows.map do |provider, streams, unknown| streams_count = streams.to_i unknown_count = unknown.to_i StreamingHealthRow.new( provider: provider, streams: streams_count, with_usage: streams_count - unknown_count, unknown: unknown_count, unknown_share: percentage(unknown_count, streams_count) ) end end |
.summary(stats) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 61 def summary(stats) total = stats.total_calls.to_i unknown_pricing_count = stats.unknown_pricing_count.to_i untagged_calls_count = stats.untagged_calls_count.to_i missing_latency_count = stats.missing_latency_count.to_i streaming_count = stats.streaming_count.to_i streaming_missing_usage = stats.streaming_missing_usage_count.to_i missing_provider_response_id_count = stats.missing_provider_response_id_count.to_i calls_with_pricing = total - unknown_pricing_count tagged_calls = total - untagged_calls_count calls_with_latency = total - missing_latency_count streams_with_usage = streaming_count - streaming_missing_usage calls_with_provider_response_id = total - missing_provider_response_id_count Summary.new( total, unknown_pricing_count, untagged_calls_count, missing_latency_count, streaming_count, streaming_missing_usage, missing_provider_response_id_count, calls_with_pricing, tagged_calls, calls_with_latency, streams_with_usage, calls_with_provider_response_id, percentage(unknown_pricing_count, total), percentage(untagged_calls_count, total), percentage(missing_latency_count, total), percentage(streaming_count, total), percentage(streaming_missing_usage, streaming_count), percentage(calls_with_pricing, total), percentage(tagged_calls, total), percentage(calls_with_latency, total), percentage(streams_with_usage, streaming_count), percentage(calls_with_provider_response_id, total) ) end |
.unknown_pricing_by_model(scope, total_calls:) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 101 def unknown_pricing_by_model(scope, total_calls:) scope.unknown_pricing .group(:provider, :model) .order(Arel.sql("COUNT(*) DESC")) .select("provider, model, COUNT(*) AS calls") .limit(10) .map do |row| calls = row.calls.to_i UnknownPricingRow.new(provider: row.provider, model: row.model, calls: calls, share_percent: percentage(calls, total_calls)) end end |
.unseen_budget_tags ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 49 def budgeted = Budget::PerTag.configured return nil if budgeted.empty? return nil unless Budget::PerTag.columns? return nil unless LlmCostTracker::CallTag.exists? unseen = budgeted.keys.reject { |key| LlmCostTracker::CallTag.exists?(key: key) } return nil if unseen.empty? UnseenBudgetTags.new(keys: unseen) end |
.usage_rows(stats, component_costs: {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'app/services/llm_cost_tracker/dashboard/data_quality.rb', line 138 def usage_rows(stats, component_costs: {}) billable_tokens = stats.billable_tokens.to_f rows = Usage::Catalog.token_priced.map do |component| token_value = stats[component.token_key].to_i { price_key: component.key, token_key: component.token_key, cost_key: component.cost_key, token_value: token_value, cost_value: component_costs[component.key], share_percent: percentage(token_value, billable_tokens), 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 |