Class: LlmCostTracker::Ledger::Rollups

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/ledger/rollups.rb,
lib/llm_cost_tracker/ledger/rollups/batch.rb,
lib/llm_cost_tracker/ledger/rollups/upsert_sql.rb

Defined Under Namespace

Classes: Batch, UpsertSql

Class Method Summary collapse

Class Method Details

.decrement!(call_rows) ⇒ Object



36
37
38
39
40
41
# File 'lib/llm_cost_tracker/ledger/rollups.rb', line 36

def decrement!(call_rows)
  totals = period_decrement_totals(call_rows)
  return if totals.empty?

  apply_decrements(totals)
end

.increment!(event) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/llm_cost_tracker/ledger/rollups.rb', line 13

def increment!(event)
  return unless event.total_cost

  Period::Total.upsert_all(
    period_rows(event),
    on_duplicate: Ledger::Rollups::UpsertSql.call(Period::Total),
    record_timestamps: true,
    unique_by: unique_by(Period::Total, %i[period period_start])
  )
end

.increment_many!(events) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/llm_cost_tracker/ledger/rollups.rb', line 24

def increment_many!(events)
  events = Array(events).select(&:total_cost)
  return if events.empty?

  Period::Total.upsert_all(
    Ledger::Rollups::Batch.rows(events),
    on_duplicate: Ledger::Rollups::UpsertSql.call(Period::Total),
    record_timestamps: true,
    unique_by: unique_by(Period::Total, %i[period period_start])
  )
end