Module: LlmCostTracker::Pricing::Backfill

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



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

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
        calculation = recompute_for(call)
        next unless calculation

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

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

.default_scopeObject



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

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