Module: LlmCostTracker::Retention

Defined in:
lib/llm_cost_tracker/retention.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
5_000

Class Method Summary collapse

Class Method Details

.prune(older_than:, batch_size: DEFAULT_BATCH_SIZE, now: Time.now.utc) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/llm_cost_tracker/retention.rb', line 8

def prune(older_than:, batch_size: DEFAULT_BATCH_SIZE, now: Time.now.utc)
  batch_size = batch_size.to_i
  raise ArgumentError, "batch_size must be positive: #{batch_size.inspect}" unless batch_size.positive?

  cutoff = resolve_cutoff(older_than, now)
  require_relative "ledger"

  deleted = 0
  loop do
    batch = prune_batch(cutoff, batch_size)
    deleted += batch
    break if batch < batch_size
  end
  deleted
end

.prune_inbox(older_than:, now: Time.now.utc) ⇒ Object



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

def prune_inbox(older_than:, now: Time.now.utc)
  cutoff = resolve_cutoff(older_than, now)
  require_relative "ingestion"
  return 0 unless LlmCostTracker::Ingestion::InboxEntry.table_exists?

  LlmCostTracker::Ingestion::InboxEntry.where(tracked_at: ...cutoff).delete_all
end

.prune_invoice_imports(older_than:, now: Time.now.utc) ⇒ Object



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

def prune_invoice_imports(older_than:, now: Time.now.utc)
  cutoff = resolve_cutoff(older_than, now)
  require_relative "ledger"
  return 0 unless LlmCostTracker::ProviderInvoiceImport.table_exists?

  LlmCostTracker::ProviderInvoiceImport
    .where(state: %w[completed failed])
    .where(finished_at: ...cutoff)
    .delete_all
end