Class: LlmCostTracker::ReportData

Inherits:
Data
  • Object
show all
Defined in:
lib/llm_cost_tracker/report_data.rb,
lib/llm_cost_tracker/report_data.rb

Constant Summary collapse

DEFAULT_DAYS =
30
TOP_LIMIT =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#average_latency_msObject (readonly)

Returns the value of attribute average_latency_ms

Returns:

  • (Object)

    the current value of average_latency_ms



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def average_latency_ms
  @average_latency_ms
end

#cost_by_modelObject (readonly)

Returns the value of attribute cost_by_model

Returns:

  • (Object)

    the current value of cost_by_model



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def cost_by_model
  @cost_by_model
end

#cost_by_providerObject (readonly)

Returns the value of attribute cost_by_provider

Returns:

  • (Object)

    the current value of cost_by_provider



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def cost_by_provider
  @cost_by_provider
end

#cost_by_tagsObject (readonly)

Returns the value of attribute cost_by_tags

Returns:

  • (Object)

    the current value of cost_by_tags



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def cost_by_tags
  @cost_by_tags
end

#daysObject (readonly)

Returns the value of attribute days

Returns:

  • (Object)

    the current value of days



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def days
  @days
end

#from_timeObject (readonly)

Returns the value of attribute from_time

Returns:

  • (Object)

    the current value of from_time



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def from_time
  @from_time
end

#requests_countObject (readonly)

Returns the value of attribute requests_count

Returns:

  • (Object)

    the current value of requests_count



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def requests_count
  @requests_count
end

#to_timeObject (readonly)

Returns the value of attribute to_time

Returns:

  • (Object)

    the current value of to_time



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def to_time
  @to_time
end

#top_callsObject (readonly)

Returns the value of attribute top_calls

Returns:

  • (Object)

    the current value of top_calls



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def top_calls
  @top_calls
end

#total_costObject (readonly)

Returns the value of attribute total_cost

Returns:

  • (Object)

    the current value of total_cost



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def total_cost
  @total_cost
end

#unknown_pricing_countObject (readonly)

Returns the value of attribute unknown_pricing_count

Returns:

  • (Object)

    the current value of unknown_pricing_count



8
9
10
# File 'lib/llm_cost_tracker/report_data.rb', line 8

def unknown_pricing_count
  @unknown_pricing_count
end

Class Method Details

.build(days: DEFAULT_DAYS, now: Time.now.utc, tag_breakdowns: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/llm_cost_tracker/report_data.rb', line 26

def self.build(days: DEFAULT_DAYS, now: Time.now.utc, tag_breakdowns: nil)
  require_relative "llm_api_call" unless defined?(LlmCostTracker::LlmApiCall)

  days = normalized_days(days)
  from = now - days.days
  scope = LlmApiCall.where(tracked_at: from..now)
  tag_breakdowns ||= LlmCostTracker.configuration.report_tag_breakdowns || []

  new(
    days: days,
    from_time: from,
    to_time: now,
    total_cost: scope.sum(:total_cost).to_f,
    requests_count: scope.count,
    average_latency_ms: average_latency_ms(scope),
    unknown_pricing_count: scope.where(total_cost: nil).count,
    cost_by_provider: cost_by(scope, :provider),
    cost_by_model: cost_by(scope, :model),
    cost_by_tags: cost_by_tags(scope, tag_breakdowns),
    top_calls: top_calls(scope)
  )
end