Class: LlmCostTracker::Dashboard::OverviewStats

Inherits:
Object
  • Object
show all
Defined in:
app/services/llm_cost_tracker/dashboard/overview_stats.rb

Class Method Summary collapse

Class Method Details

.call(scope: LlmCostTracker::Ledger::Call.all, previous_scope: nil) ⇒ Object



9
10
11
# File 'app/services/llm_cost_tracker/dashboard/overview_stats.rb', line 9

def call(scope: LlmCostTracker::Ledger::Call.all, previous_scope: nil)
  scope.select(aggregate_selects(previous_scope: previous_scope)).take
end

.monthly_budget_statusObject



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
# File 'app/services/llm_cost_tracker/dashboard/overview_stats.rb', line 13

def monthly_budget_status
  budget = LlmCostTracker.configuration.monthly_budget
  return nil unless budget

  now = Time.now.utc
  month_start = now.beginning_of_month
  month_end = now.end_of_month
  spent = LlmCostTracker::Ledger::Period::Totals.call(%i[monthly], time: now).fetch(:monthly)
  elapsed_seconds = now - month_start
  total_seconds = month_end - month_start
  projected_spent = if spent.zero? || !elapsed_seconds.positive?
                      spent
                    else
                      spent * (total_seconds / elapsed_seconds)
                    end

  {
    budget: budget.to_f,
    spent: spent,
    percent_used: budget.to_f.positive? ? (spent / budget.to_f) * 100.0 : 0.0,
    projected_spent: projected_spent,
    projected_percent_used: budget.to_f.positive? ? (projected_spent / budget.to_f) * 100.0 : 0.0,
    projected_delta: projected_spent - budget.to_f,
    projection_end_label: month_end.strftime("%b %-d")
  }
end