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.



23
24
25
26
27
# File 'app/services/llm_cost_tracker/dashboard/spend_anomaly.rb', line 23

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

Class Method Details

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



18
19
20
# File 'app/services/llm_cost_tracker/dashboard/spend_anomaly.rb', line 18

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

Instance Method Details

#alertObject



29
30
31
32
33
# File 'app/services/llm_cost_tracker/dashboard/spend_anomaly.rb', line 29

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

  alerts.max_by { |item| [item.ratio || 0.0, item.latest_spend] }
end