Module: LlmCostTracker::Ledger::Store

Defined in:
lib/llm_cost_tracker/ledger/store.rb

Class Method Summary collapse

Class Method Details

.insert(events) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/llm_cost_tracker/ledger/store.rb', line 14

def insert(events)
  events = Array(events)
  return if events.empty?

  persist_records(events)
  Ledger::Rollups.increment_safely!(events)
end

.persist_records(events) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/llm_cost_tracker/ledger/store.rb', line 22

def persist_records(events)
  events = Array(events)
  return if events.empty?

  LlmCostTracker::Call.transaction do
    rows = events.map { |event| attributes_for(event) }
    call_ids = insert_calls_returning_ids(rows, events)
    insert_line_items(events, call_ids)
    insert_call_tags(events, call_ids)
  end
end