Class: LlmCostTracker::Reconciliation::Diff
- Inherits:
-
Object
- Object
- LlmCostTracker::Reconciliation::Diff
- Defined in:
- lib/llm_cost_tracker/reconciliation/diff.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- SCOPE_KEYS =
%i[provider_project_id provider_api_key_id provider_workspace_id].freeze
- ATTRIBUTION_KEYS =
(SCOPE_KEYS + [:model]).freeze
- COST_ROW_TYPE =
"cost"- PERIOD_ONLY_BASIS =
"period_only"- BASIS_DIMENSION =
{ "project" => :provider_project_id, "api_key" => :provider_api_key_id, "workspace" => :provider_workspace_id, "model" => :model }.freeze
- DEFAULT_DRILLDOWN_LIMIT =
100
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(source:, period_start:, period_end:, provider:, scope: {}, currency: nil, drilldown_limit: DEFAULT_DRILLDOWN_LIMIT) ⇒ Diff
constructor
A new instance of Diff.
Constructor Details
#initialize(source:, period_start:, period_end:, provider:, scope: {}, currency: nil, drilldown_limit: DEFAULT_DRILLDOWN_LIMIT) ⇒ Diff
Returns a new instance of Diff.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/llm_cost_tracker/reconciliation/diff.rb', line 25 def initialize(source:, period_start:, period_end:, provider:, scope: {}, currency: nil, drilldown_limit: DEFAULT_DRILLDOWN_LIMIT) @source = source.to_s @provider = provider.to_s @period_start = parse_date(period_start) @period_end = parse_date(period_end) @scope = (scope || {}).to_h.transform_keys { |key| key.to_s.to_sym }.slice(*SCOPE_KEYS) @currency = (currency || Ledger::Rollups::DEFAULT_CURRENCY).to_s.upcase @drilldown_limit = drilldown_limit raise ArgumentError, "source must be present" if @source.empty? raise ArgumentError, "provider must be present" if @provider.empty? raise ArgumentError, "period_end must be on or after period_start" if @period_end < @period_start end |
Instance Method Details
#call ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/llm_cost_tracker/reconciliation/diff.rb', line 39 def call provider_total = scoped_cost_invoices_in_window .sum(:billed_amount) .then { |sum| BigDecimal(sum.to_s) } local_index = local_attribution_index_distinct invoice_basis_values = invoice_basis_values_distinct_sql local_total, local_total_source = sum_local_total unmatched_providers = unmatched_provider_rows_from_sql(local_index) unmatched_locals = unmatched_local_calls_in(invoice_basis_values) non_cost_rows = non_cost_invoices_to_rows(scoped_non_cost_invoices_for_drilldown) DiffResult.new( source: source, provider: provider, period_start: period_start, period_end: period_end, currency: currency, scope: scope, provider_total: provider_total, local_total: local_total, local_total_source: local_total_source, delta_amount: local_total - provider_total, delta_percent: percent_for(local_total, provider_total), unmatched_provider_rows: cap_by_amount(unmatched_providers, :billed_amount), unmatched_provider_rows_total: unmatched_provider_rows_total_count(local_index), unmatched_local_calls: cap_by_amount(unmatched_locals, :total_cost), unmatched_local_calls_total: unmatched_local_calls_total_count(invoice_basis_values), non_cost_rows: cap_by_amount(non_cost_rows, :billed_amount), non_cost_rows_total: scoped_non_cost_invoices_relation.count ) end |