Class: LlmCostTracker::Storage::ActiveRecordRollups
- Inherits:
-
Object
- Object
- LlmCostTracker::Storage::ActiveRecordRollups
- Defined in:
- lib/llm_cost_tracker/storage/active_record_rollups.rb
Constant Summary collapse
- PERIODS =
{ monthly: "month", daily: "day" }.freeze
Class Method Summary collapse
- .daily_total(time: Time.now.utc) ⇒ Object
- .decrement!(call_rows) ⇒ Object
- .increment!(event) ⇒ Object
- .monthly_total(time: Time.now.utc) ⇒ Object
- .period_totals(periods, time: Time.now.utc) ⇒ Object
- .reset! ⇒ Object
Class Method Details
.daily_total(time: Time.now.utc) ⇒ Object
44 45 46 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 44 def daily_total(time: Time.now.utc) period_totals(%i[daily], time: time).fetch(:daily) end |
.decrement!(call_rows) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 31 def decrement!(call_rows) return unless period_totals_enabled? totals = period_decrement_totals(call_rows) return if totals.empty? apply_decrements(totals) end |
.increment!(event) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 18 def increment!(event) return unless event.cost&.total_cost return unless period_totals_enabled? model = period_total_model model.upsert_all( period_rows(event), on_duplicate: total_upsert_sql(model), record_timestamps: true, unique_by: unique_by(model, %i[period period_start]) ) end |
.monthly_total(time: Time.now.utc) ⇒ Object
40 41 42 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 40 def monthly_total(time: Time.now.utc) period_totals(%i[monthly], time: time).fetch(:monthly) end |
.period_totals(periods, time: Time.now.utc) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 48 def period_totals(periods, time: Time.now.utc) periods = periods.map(&:to_sym).select { |period| PERIODS.key?(period) } return {} if periods.empty? if period_totals_enabled? rollup_period_totals(periods, time) else periods.to_h { |period| [period, fallback_period_total(period, time)] } end end |
.reset! ⇒ Object
14 15 16 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 14 def reset! remove_instance_variable(:@period_totals_enabled) if instance_variable_defined?(:@period_totals_enabled) end |