Class: AgentC::Costs::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_c/costs/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, run:) ⇒ Data

Returns a new instance of Data.



8
9
10
11
# File 'lib/agent_c/costs/data.rb', line 8

def initialize(project:, run:)
  @project = project
  @run = run
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



6
7
8
# File 'lib/agent_c/costs/data.rb', line 6

def project
  @project
end

#runObject (readonly)

Returns the value of attribute run.



6
7
8
# File 'lib/agent_c/costs/data.rb', line 6

def run
  @run
end

Class Method Details

.calculate(agent_store:, project:, run_id: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/agent_c/costs/data.rb', line 13

def self.calculate(agent_store:, project:, run_id: nil)
  calculator = Report::Calculator.new

  # Calculate project-level cost
  project_messages = agent_store.message
    .joins(:chat)
    .where(chats: { project: project })
    .includes(:model, :chat)
  project_stats = calculator.calculate(project_messages)
  project_cost = project_stats[:total_cost]

  # Calculate run-level cost
  run_cost = 0.0
  if run_id
    run_messages = agent_store.message
      .joins(:chat)
      .where(chats: { project: project, run_id: run_id })
      .includes(:model, :chat)
    run_stats = calculator.calculate(run_messages)
    run_cost = run_stats[:total_cost]
  end

  new(project: project_cost, run: run_cost)
end