Class: LlmCostTracker::Pricing::Backfill

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/pricing/backfill.rb

Defined Under Namespace

Classes: Result, RollupEvent

Constant Summary collapse

DEFAULT_BATCH_SIZE =
500

Class Method Summary collapse

Class Method Details

.call(scope: default_scope, batch_size: DEFAULT_BATCH_SIZE) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/llm_cost_tracker/pricing/backfill.rb', line 18

def call(scope: default_scope, batch_size: DEFAULT_BATCH_SIZE)
  examined = 0
  recomputed = 0

  scope.includes(:line_items).find_in_batches(batch_size: batch_size) do |batch|
    rollup_events = []
    LlmCostTracker::Call.transaction do
      batch.each do |call|
        examined += 1
        outcome = recompute_for(call)
        next unless outcome

        persist!(call, outcome)
        rollup_events << rollup_event_for(call, outcome)
        recomputed += 1
      end
      Ledger::Rollups.increment_many!(rollup_events) if rollup_events.any?
    end
  end

  Result.new(examined: examined, recomputed: recomputed, still_unknown: examined - recomputed)
end

.default_scopeObject



41
42
43
# File 'lib/llm_cost_tracker/pricing/backfill.rb', line 41

def default_scope
  LlmCostTracker::Call.where(total_cost: nil)
end