Class: LlmCostTracker::Storage::ActiveRecordBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/storage/active_record_backend.rb

Constant Summary collapse

VERIFY_TAG =
"llm_cost_tracker_verify"

Class Method Summary collapse

Class Method Details

.prune(cutoff:, batch_size:) ⇒ Object



43
44
45
46
47
# File 'lib/llm_cost_tracker/storage/active_record_backend.rb', line 43

def prune(cutoff:, batch_size:)
  require_relative "../llm_api_call" unless defined?(LlmCostTracker::LlmApiCall)

  ActiveRecordStore.prune(cutoff: cutoff, batch_size: batch_size)
end

.save(event) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/llm_cost_tracker/storage/active_record_backend.rb', line 14

def save(event)
  require_relative "../llm_api_call" unless defined?(LlmCostTracker::LlmApiCall)

  ActiveRecordStore.save(event)
  event
rescue LoadError => e
  raise Error, "ActiveRecord storage requires the active_record gem: #{e.message}"
end

.verifyObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/llm_cost_tracker/storage/active_record_backend.rb', line 23

def verify
  require_relative "../llm_api_call" unless defined?(LlmCostTracker::LlmApiCall)

  unless LlmCostTracker::LlmApiCall.table_exists?
    return [
      VerificationResult.new(
        :error,
        "active_record",
        "llm_api_calls table is missing; run install generator and migrate"
      )
    ]
  end

  [active_record_capture_check]
rescue LoadError => e
  [VerificationResult.new(:error, "active_record", "unavailable: #{e.message}")]
rescue StandardError => e
  [VerificationResult.new(:error, "active_record", "#{e.class}: #{e.message}")]
end