Class: CompletionKit::Api::V1::MetricsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



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

def create
  metric = Metric.new(metric_params)
  if metric.save
    render json: metric, status: :created
  else
    render_validation_errors(metric)
  end
end

#destroyObject



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

def destroy
  @metric.destroy!
  head :no_content
end

#indexObject



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

def index
  scope = Metric.includes(:tags)
  scope = filter_by_tags(scope)
  render json: paginate(scope.order(created_at: :desc))
end

#showObject



13
14
15
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 13

def show
  render json: @metric
end

#suggest_variantsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 39

def suggest_variants
  disagreement_count = Calibration.where(metric_id: @metric.id, verdict: "disagree").count
  if disagreement_count.zero?
    render_error("Mark at least one case as Disagree before asking the model to suggest a change.", status: :unprocessable_entity)
    return
  end

  MetricVersion.drafts.where(metric_id: @metric.id, source: "suggestion").destroy_all
  generator = MetricVariantGenerator.new(@metric, count: params[:count].to_i, model: params[:model])
  variants = generator.call
  if variants.empty?
    render_error("The model returned no usable variants. Try again with a different model.", status: :unprocessable_entity)
    return
  end
  versions = generator.persist!(variants)
  render json: versions, status: :created
end

#updateObject



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

def update
  if @metric.update(metric_params)
    render json: @metric
  else
    render_validation_errors(@metric)
  end
end