Class: Tracebook::InteractionsController

Inherits:
ApplicationController show all
Includes:
Pagy::Method
Defined in:
app/controllers/tracebook/interactions_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_reviewObject



37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/tracebook/interactions_controller.rb', line 37

def bulk_review
  ids = Array(params[:interaction_ids])
  state = params.require(:review_state).to_s
  unless Interaction.review_states.key?(state)
    redirect_to interactions_path, alert: "Invalid review state: #{state}"
    return
  end

  Interaction.where(id: ids).update_all(review_state: Interaction.review_states.fetch(state))
  redirect_to interactions_path, notice: "Updated #{ids.size} interactions"
end

#filter_paramsObject (private)



55
56
57
# File 'app/controllers/tracebook/interactions_controller.rb', line 55

def filter_params
  params.fetch(:filters, {}).permit(:provider, :model, :project, :status, :review_state, :tag, :from, :to)
end

#indexObject



10
11
12
13
14
15
16
17
18
# File 'app/controllers/tracebook/interactions_controller.rb', line 10

def index
  @filters = filter_params
  scope = Interaction.filtered(@filters)
  @kpis = kpis_for(scope)
  @pagy, @interactions = pagy(scope.order(created_at: :desc), limit: Tracebook.config.per_page)
  @providers = Interaction.distinct.order(:provider).pluck(:provider)
  @models = Interaction.distinct.order(:model).pluck(:model)
  @projects = Interaction.distinct.order(:project).pluck(:project).compact
end

#kpis_for(scope) ⇒ Object (private)



59
60
61
62
63
64
65
66
67
# File 'app/controllers/tracebook/interactions_controller.rb', line 59

def kpis_for(scope)
  {
    total: scope.count,
    success: scope.status_success.count,
    cost_cents: scope.sum(:cost_total_cents),
    input_tokens: scope.sum(:input_tokens),
    output_tokens: scope.sum(:output_tokens)
  }
end

#reviewObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/tracebook/interactions_controller.rb', line 23

def review
  state = params.require(:review_state).to_s
  unless Interaction.review_states.key?(state)
    redirect_to interaction_path(@interaction), alert: "Invalid review state: #{state}"
    return
  end

  if @interaction.update(review_state: state)
    redirect_to interaction_path(@interaction), notice: "Review updated"
  else
    render :show, status: :unprocessable_entity
  end
end

#set_interactionObject (private)



51
52
53
# File 'app/controllers/tracebook/interactions_controller.rb', line 51

def set_interaction
  @interaction = Interaction.find(params[:id])
end

#showObject



20
21
# File 'app/controllers/tracebook/interactions_controller.rb', line 20

def show
end