Module: RubyLLM::Agents::Tenant::Resettable

Extended by:
ActiveSupport::Concern
Included in:
RubyLLM::Agents::Tenant
Defined in:
app/models/ruby_llm/agents/tenant/resettable.rb

Overview

Handles lazy reset of usage counters when periods roll over, and provides refresh_counters! for reconciliation from the executions table.

Instance Method Summary collapse

Instance Method Details

#ensure_daily_reset!void

This method returns an undefined value.

Resets daily counters if the day has rolled over. Uses a WHERE guard to prevent race conditions with concurrent requests.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/ruby_llm/agents/tenant/resettable.rb', line 17

def ensure_daily_reset!
  return if daily_reset_date == Date.current

  rows = self.class.where(id: id)
    .where("daily_reset_date IS NULL OR daily_reset_date < ?", Date.current)
    .update_all(
      daily_cost_spent: 0,
      daily_tokens_used: 0,
      daily_executions_count: 0,
      daily_error_count: 0,
      daily_reset_date: Date.current
    )

  reload if rows > 0
end

#ensure_monthly_reset!void

This method returns an undefined value.

Resets monthly counters if the month has rolled over. Uses a WHERE guard to prevent race conditions with concurrent requests.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/ruby_llm/agents/tenant/resettable.rb', line 37

def ensure_monthly_reset!
  bom = Date.current.beginning_of_month
  return if monthly_reset_date == bom

  rows = self.class.where(id: id)
    .where("monthly_reset_date IS NULL OR monthly_reset_date < ?", bom)
    .update_all(
      monthly_cost_spent: 0,
      monthly_tokens_used: 0,
      monthly_executions_count: 0,
      monthly_error_count: 0,
      monthly_reset_date: bom
    )

  reload if rows > 0
end

#refresh_counters!void

This method returns an undefined value.

Recalculates all counters from the source-of-truth executions table.

Use when counters have drifted due to manual DB edits, failed writes, or after deleting/updating execution records.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/ruby_llm/agents/tenant/resettable.rb', line 60

def refresh_counters!
  today = Date.current
  bom = today.beginning_of_month

  daily_stats = aggregate_stats(
    executions.where("created_at >= ?", today.beginning_of_day)
  )
  monthly_stats = aggregate_stats(
    executions.where("created_at >= ?", bom.beginning_of_day)
  )

  last_exec = executions.order(created_at: :desc).pick(:created_at, :status)

  update_columns(
    daily_cost_spent: daily_stats[:cost],
    daily_tokens_used: daily_stats[:tokens],
    daily_executions_count: daily_stats[:count],
    daily_error_count: daily_stats[:errors],
    daily_reset_date: today,

    monthly_cost_spent: monthly_stats[:cost],
    monthly_tokens_used: monthly_stats[:tokens],
    monthly_executions_count: monthly_stats[:count],
    monthly_error_count: monthly_stats[:errors],
    monthly_reset_date: bom,

    last_execution_at: last_exec&.first,
    last_execution_status: last_exec&.last
  )

  reload
end