Class: CompletionKit::Api::V1::RunsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/completion_kit/api/v1/runs_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 15

def create
  run = Run.new(run_params.except(:metric_ids))
  if run.save
    run.replace_metrics!(params[:metric_ids])
    render json: run.reload, status: :created
  else
    render json: {errors: run.errors}, status: :unprocessable_entity
  end
end

#destroyObject



34
35
36
37
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 34

def destroy
  @run.destroy!
  head :no_content
end

#generateObject



39
40
41
42
43
44
45
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 39

def generate
  if @run.start!
    render json: @run.reload, status: :accepted
  else
    render json: { errors: [@run.failure_summary || @run.errors.full_messages.to_sentence] }, status: :unprocessable_entity
  end
end

#indexObject



7
8
9
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 7

def index
  render json: Run.order(created_at: :desc)
end

#retry_failuresObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 47

def retry_failures
  scope = @run.responses.where(status: "failed")
  scope = scope.where(id: params[:only]) if params[:only].present?

  ActiveRecord::Base.transaction do
    failed_response_ids = scope.pluck(:id)
    CompletionKit::Review.where(response_id: failed_response_ids, status: "failed").update_all(
      status: "pending", attempts: 0,
      error_provider: nil, error_class: nil, error_status: nil, error_message: nil,
      ai_score: nil, ai_feedback: nil
    )
    scope.update_all(
      status: "pending", attempts: 0,
      error_provider: nil, error_class: nil, error_status: nil, error_message: nil,
      response_text: nil
    )
    @run.update!(status: "running")
    failed_response_ids.each { |rid| CompletionKit::GenerateRowJob.perform_later(@run.id, rid) }
  end

  render json: @run.reload, status: :accepted
end

#showObject



11
12
13
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 11

def show
  render json: @run
end

#updateObject



25
26
27
28
29
30
31
32
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 25

def update
  if @run.update(run_params.except(:metric_ids))
    @run.replace_metrics!(params[:metric_ids]) if params.key?(:metric_ids)
    render json: @run.reload
  else
    render json: {errors: @run.errors}, status: :unprocessable_entity
  end
end