Module: Legion::Extensions::Llm::Ledger::Runners::UsageReporter

Extended by:
UsageReporter
Included in:
UsageReporter
Defined in:
lib/legion/extensions/llm/ledger/runners/usage_reporter.rb

Instance Method Summary collapse

Instance Method Details

#budget_check(budget_id:, budget_usd:, threshold: 0.8, period: 'month') ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/llm/ledger/runners/usage_reporter.rb', line 36

def budget_check(budget_id:, budget_usd:, threshold: 0.8, period: 'month')
  dataset = ::Legion::Data::DB[:metering_records].where(budget_id: budget_id)
  dataset = apply_time_window(dataset, nil, nil, period)
  spent = dataset.sum(:cost_usd).to_f

  {
    budget_id:         budget_id,
    budget_usd:        budget_usd.to_f,
    spent_usd:         spent,
    remaining_usd:     [budget_usd.to_f - spent, 0.0].max,
    exceeded:          spent > budget_usd.to_f,
    threshold_reached: spent >= (budget_usd.to_f * threshold.to_f)
  }
end

#summary(since: nil, until_: nil, period: nil, group_by: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/llm/ledger/runners/usage_reporter.rb', line 11

def summary(since: nil, until_: nil, period: nil, group_by: nil)
  dataset = ::Legion::Data::DB[:metering_records]
  dataset = apply_time_window(dataset, since, until_, period)
  dataset = dataset.group_and_count(group_by.to_sym) if group_by
  dataset.select_append(
    Sequel.function(:SUM, :input_tokens).as(:total_input_tokens),
    Sequel.function(:SUM, :output_tokens).as(:total_output_tokens),
    Sequel.function(:SUM, :total_tokens).as(:grand_total_tokens),
    Sequel.function(:SUM, :cost_usd).as(:total_cost_usd),
    Sequel.function(:AVG, :latency_ms).as(:avg_latency_ms),
    Sequel.function(:COUNT, Sequel.lit('*')).as(:request_count)
  ).all
end

#top_consumers(limit: 10, group_by: 'node_id', since: nil, until_: nil, period: 'day') ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/extensions/llm/ledger/runners/usage_reporter.rb', line 51

def top_consumers(limit: 10, group_by: 'node_id', since: nil, until_: nil, period: 'day')
  col = group_by.to_sym
  dataset = ::Legion::Data::DB[:metering_records]
  dataset = apply_time_window(dataset, since, until_, period)
  dataset.select(
    col,
    Sequel.function(:SUM, :total_tokens).as(:total_tokens),
    Sequel.function(:SUM, :cost_usd).as(:cost_usd),
    Sequel.function(:COUNT, Sequel.lit('*')).as(:request_count)
  ).group(col)
         .order(Sequel.desc(:cost_usd))
         .limit(limit)
         .all
end

#worker_usage(worker_id:, since: nil, until_: nil, period: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/llm/ledger/runners/usage_reporter.rb', line 25

def worker_usage(worker_id:, since: nil, until_: nil, period: nil)
  dataset = ::Legion::Data::DB[:metering_records].where(worker_id: worker_id)
  dataset = apply_time_window(dataset, since, until_, period)
  dataset.select(
    :provider, :model_id, :request_type,
    Sequel.function(:SUM, :total_tokens).as(:total_tokens),
    Sequel.function(:SUM, :cost_usd).as(:cost_usd),
    Sequel.function(:COUNT, Sequel.lit('*')).as(:count)
  ).group(:provider, :model_id, :request_type).all
end