Class: TokenHawk::Storage::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/token_hawk/storage/active_record.rb

Class Method Summary collapse

Class Method Details

.all ⇒ Object



21
22
23
# File 'lib/token_hawk/storage/active_record.rb', line 21

def all
  CallRecord.all.map { |r| to_call(r) }
end

.clear ⇒ Object



32
33
34
# File 'lib/token_hawk/storage/active_record.rb', line 32

def clear
  CallRecord.delete_all
end

.record(call) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/token_hawk/storage/active_record.rb', line 7

def record(call)
  CallRecord.create!(
    domain: call.domain.to_s,
    tags: call.tags,
    vendor: call.vendor,
    model: call.model,
    input_tokens: call.input_tokens,
    output_tokens: call.output_tokens,
    total_cost_cents: call.total_cost_cents,
    latency_ms: call.latency_ms,
    created_at: call.created_at
  )
end

.where(domain: nil, since: nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/token_hawk/storage/active_record.rb', line 25

def where(domain: nil, since: nil)
  scope = CallRecord.all
  scope = scope.for_domain(domain.to_s) if domain
  scope = scope.since(since) if since
  scope.map { |r| to_call(r) }
end