20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'app/services/llm_cost_tracker/dashboard/overview_stats.rb', line 20
def call(scope: LlmCostTracker::LlmApiCall.all, previous_scope: nil)
current = aggregate(scope)
total_calls = current.calls_count.to_i
total_cost = current.total_cost_sum.to_f
previous = previous_scope && aggregate(previous_scope)
prev_cost = previous&.total_cost_sum.to_f
prev_calls = previous&.calls_count.to_i
OverviewStatsData.new(
total_cost: total_cost,
total_calls: total_calls,
average_cost_per_call: total_calls.positive? ? total_cost / total_calls : 0.0,
average_latency_ms: latency_value(current, scope),
unknown_pricing_count: current.unknown_pricing_count.to_i,
previous_total_cost: previous ? prev_cost : nil,
previous_total_calls: previous ? prev_calls : nil,
cost_delta_percent: previous ? delta_percent(total_cost, prev_cost) : nil,
calls_delta_percent: previous ? delta_percent(total_calls, prev_calls) : nil,
monthly_budget_status: budget_status
)
end
|