Class: LlmCostTracker::Dashboard::SpendAnomaly

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

Constant Summary collapse

WINDOW_DAYS =
7

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, from:, to:) ⇒ SpendAnomaly

Returns a new instance of SpendAnomaly.



14
15
16
17
18
# File 'app/services/llm_cost_tracker/dashboard/spend_anomaly.rb', line 14

def initialize(scope:, from:, to:)
  @scope = scope
  @from = from.to_date
  @to = to.to_date
end

Class Method Details

.call(from:, to:, scope: LlmCostTracker::Ledger::Call.all) ⇒ Object



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

def call(from:, to:, scope: LlmCostTracker::Ledger::Call.all)
  new(scope: scope, from: from, to: to).alert
end

Instance Method Details

#alertObject



20
21
22
23
24
# File 'app/services/llm_cost_tracker/dashboard/spend_anomaly.rb', line 20

def alert
  return nil if from > (to - WINDOW_DAYS)

  alerts.max_by { |item| [item.fetch(:ratio) || 0.0, item.fetch(:latest_spend)] }
end