Class: Legion::Data::Models::LLM::MessageInferenceMetric

Inherits:
Object
  • Object
show all
Includes:
ModelHelpers
Defined in:
lib/legion/data/models/llm/message_inference_metric.rb

Class Method Summary collapse

Methods included from ModelHelpers

#before_create

Class Method Details

.finance_usage_by_cost_center_model_day(cost_center: nil, model_key: nil, from: nil, to: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/data/models/llm/message_inference_metric.rb', line 16

def finance_usage_by_cost_center_model_day(cost_center: nil, model_key: nil, from: nil, to: nil)
  usage_day = Sequel.function(:date, :recorded_at)
  scope = dataset
  scope = scope.where(cost_center: cost_center) unless cost_center.nil?
  scope = scope.where(model_key: model_key) unless model_key.nil?
  scope = scope.where { recorded_at >= from } unless from.nil?
  scope = scope.where { recorded_at < to } unless to.nil?

  scope
    .select(
      :cost_center,
      :model_key,
      usage_day.as(:usage_day),
      Sequel.function(:sum, :input_tokens).as(:input_tokens),
      Sequel.function(:sum, :output_tokens).as(:output_tokens),
      Sequel.function(:sum, :thinking_tokens).as(:thinking_tokens),
      Sequel.function(:sum, :total_tokens).as(:total_tokens),
      Sequel.function(:sum, :cost_usd).as(:cost_usd),
      Sequel.function(:sum, :latency_ms).as(:latency_ms),
      Sequel.function(:sum, :wall_clock_ms).as(:wall_clock_ms)
    )
    .group(:cost_center, :model_key, usage_day)
    .order(:cost_center, :model_key, usage_day)
    .map(&:values)
end