Module: Legion::Extensions::Metering::Runners::Metering

Extended by:
Metering
Included in:
Metering
Defined in:
lib/legion/extensions/metering/runners/metering.rb

Constant Summary collapse

PERIOD_DAYS =
{ 'daily' => 1, 'weekly' => 7, 'monthly' => 30 }.freeze

Instance Method Summary collapse

Instance Method Details

#cleanup_old_records(retention_days: 90) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/legion/extensions/metering/runners/metering.rb', line 90

def cleanup_old_records(retention_days: 90, **)
  return { purged: 0, retention_days: retention_days, cutoff: nil } unless defined?(Legion::Data) && Legion::Data.connection

  cutoff = Time.now.utc - (retention_days * 86_400)
  count = Legion::Data.connection[:metering_records].where { recorded_at < cutoff }.delete
  log.info("[metering] cleanup: purged=#{count} retention_days=#{retention_days} cutoff=#{cutoff}")
  { purged: count, retention_days: retention_days, cutoff: cutoff }
end

#record(worker_id: nil, task_id: nil, provider: nil, model_id: nil, input_tokens: 0, output_tokens: 0, thinking_tokens: 0, input_context_bytes: 0, latency_ms: 0, routing_reason: nil, wall_clock_ms: 0, cpu_time_ms: 0, external_api_calls: 0, cost_usd: 0.0, status: nil, event_type: nil, extension: nil, runner_function: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



12
13
14
15
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
41
42
43
44
# File 'lib/legion/extensions/metering/runners/metering.rb', line 12

def record(worker_id: nil, task_id: nil, provider: nil, model_id: nil, # rubocop:disable Metrics/ParameterLists
           input_tokens: 0, output_tokens: 0, thinking_tokens: 0,
           input_context_bytes: 0, latency_ms: 0, routing_reason: nil,
           wall_clock_ms: 0, cpu_time_ms: 0, external_api_calls: 0,
           cost_usd: 0.0, status: nil, event_type: nil,
           extension: nil, runner_function: nil, **)
  record = {
    worker_id:           worker_id,
    task_id:             task_id,
    provider:            provider,
    model_id:            model_id,
    input_tokens:        input_tokens,
    output_tokens:       output_tokens,
    thinking_tokens:     thinking_tokens,
    total_tokens:        input_tokens + output_tokens + thinking_tokens,
    input_context_bytes: input_context_bytes,
    latency_ms:          latency_ms,
    wall_clock_ms:       wall_clock_ms,
    cpu_time_ms:         cpu_time_ms,
    external_api_calls:  external_api_calls,
    routing_reason:      routing_reason,
    cost_usd:            cost_usd.to_f,
    status:              status,
    event_type:          event_type,
    extension:           extension,
    runner_function:     runner_function,
    recorded_at:         Time.now.utc
  }

  log.debug("[metering] recorded: provider=#{provider} model=#{model_id} " \
            "tokens=#{record[:total_tokens]} latency=#{latency_ms}ms wall_clock=#{wall_clock_ms}ms")
  record
end

#routing_stats(worker_id: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/metering/runners/metering.rb', line 78

def routing_stats(worker_id: nil, **)
  ds = Legion::Data.connection[:metering_records]
  ds = ds.where(worker_id: worker_id) if worker_id

  {
    by_routing_reason:       ds.group_and_count(:routing_reason).all,
    by_provider:             ds.group_and_count(:provider).all,
    by_model:                ds.group_and_count(:model_id).all,
    avg_latency_by_provider: ds.group(:provider).select_append { avg(latency_ms).as(avg_latency) }.all
  }
end

#team_costs(team:, period: 'daily') ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/metering/runners/metering.rb', line 64

def team_costs(team:, period: 'daily', **)
  workers = Legion::Data::Model::DigitalWorker.where(team: team).select_map(:worker_id)
  ds = Legion::Data.connection[:metering_records].where(worker_id: workers)

  {
    team:         team,
    period:       period,
    worker_count: workers.size,
    total_tokens: ds.sum(:total_tokens) || 0,
    total_calls:  ds.count,
    by_worker:    ds.group_and_count(:worker_id).all
  }
end

#worker_costs(worker_id:, period: 'daily') ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/legion/extensions/metering/runners/metering.rb', line 46

def worker_costs(worker_id:, period: 'daily', **)
  ds = Legion::Data.connection[:metering_records].where(worker_id: worker_id)
  ds = apply_period_filter(ds, period)

  {
    worker_id:       worker_id,
    period:          period,
    total_tokens:    ds.sum(:total_tokens) || 0,
    input_tokens:    ds.sum(:input_tokens) || 0,
    output_tokens:   ds.sum(:output_tokens) || 0,
    thinking_tokens: ds.sum(:thinking_tokens) || 0,
    total_calls:     ds.count,
    avg_latency_ms:  ds.avg(:latency_ms)&.round(1) || 0,
    by_provider:     ds.group_and_count(:provider).all,
    by_model:        ds.group_and_count(:model_id).all
  }
end