Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/finders/projects/ml/candidate_finder.rb')
-rw-r--r--app/finders/projects/ml/candidate_finder.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/app/finders/projects/ml/candidate_finder.rb b/app/finders/projects/ml/candidate_finder.rb
new file mode 100644
index 00000000000..a543abc2c99
--- /dev/null
+++ b/app/finders/projects/ml/candidate_finder.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+module Projects
+ module Ml
+ class CandidateFinder
+ VALID_ORDER_BY_TYPES = %w[column metric].freeze
+ VALID_ORDER_BY_COLUMNS = %w[name created_at id].freeze
+ VALID_SORT = %w[asc desc].freeze
+
+ def initialize(experiment, params = {})
+ @experiment = experiment
+ @params = params
+ end
+
+ def execute
+ candidates = @experiment.candidates.including_relationships
+
+ candidates = by_name(candidates)
+ order(candidates)
+ end
+
+ private
+
+ def by_name(candidates)
+ return candidates unless @params[:name].present?
+
+ candidates.by_name(@params[:name])
+ end
+
+ def order(candidates)
+ return candidates.order_by_metric(metric_order_by, sort) if order_by_metric?
+
+ candidates.order_by("#{column_order_by}_#{sort}").with_order_id_desc
+ end
+
+ def order_by_metric?
+ order_by_type == 'metric'
+ end
+
+ def order_by_type
+ valid_or_default(@params[:order_by_type], VALID_ORDER_BY_TYPES, 'column')
+ end
+
+ def column_order_by
+ valid_or_default(@params[:order_by], VALID_ORDER_BY_COLUMNS, 'created_at')
+ end
+
+ def metric_order_by
+ @params[:order_by] || ''
+ end
+
+ def sort
+ valid_or_default(@params[:sort]&.downcase, VALID_SORT, 'desc')
+ end
+
+ def valid_or_default(value, valid_values, default)
+ return value if valid_values.include?(value)
+
+ default
+ end
+ end
+ end
+end