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
- .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
33 34 35 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 33 def daily_total(time: Time.now.utc) period_totals(%i[daily], time: time).fetch(:daily) end |
.increment!(event) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 16 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
29 30 31 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 29 def monthly_total(time: Time.now.utc) period_totals(%i[monthly], time: time).fetch(:monthly) end |
.period_totals(periods, time: Time.now.utc) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 37 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
12 13 14 |
# File 'lib/llm_cost_tracker/storage/active_record_rollups.rb', line 12 def reset! remove_instance_variable(:@period_totals_enabled) if instance_variable_defined?(:@period_totals_enabled) end |