diff options
Diffstat (limited to 'app/models/ml/candidate.rb')
-rw-r--r-- | app/models/ml/candidate.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/app/models/ml/candidate.rb b/app/models/ml/candidate.rb index 3ea46a8b703..f973b00c568 100644 --- a/app/models/ml/candidate.rb +++ b/app/models/ml/candidate.rb @@ -2,6 +2,8 @@ module Ml class Candidate < ApplicationRecord + include Sortable + PACKAGE_PREFIX = 'ml_candidate_' enum status: { running: 0, scheduled: 1, finished: 2, failed: 3, killed: 4 } @@ -19,6 +21,30 @@ module Ml attribute :iid, default: -> { SecureRandom.uuid } scope :including_relationships, -> { includes(:latest_metrics, :params, :user) } + scope :by_name, ->(name) { where("ml_candidates.name LIKE ?", "%#{sanitize_sql_like(name)}%") } # rubocop:disable GitlabSecurity/SqlInjection + scope :order_by_metric, ->(metric, direction) do + subquery = Ml::CandidateMetric.latest.where(name: metric) + column_expression = Arel::Table.new('latest')[:value] + metric_order_expression = direction.to_sym == :desc ? column_expression.desc : column_expression.asc + + joins("INNER JOIN (#{subquery.to_sql}) latest ON latest.candidate_id = ml_candidates.id") + .select("ml_candidates.*", "latest.value as metric_value") + .order( + Gitlab::Pagination::Keyset::Order.build( + [ + Gitlab::Pagination::Keyset::ColumnOrderDefinition.new( + attribute_name: 'metric_value', + order_expression: metric_order_expression, + nullable: :nulls_last, + distinct: false + ), + Gitlab::Pagination::Keyset::ColumnOrderDefinition.new( + attribute_name: 'id', + order_expression: arel_table[:id].desc + ) + ]) + ) + end delegate :project_id, :project, to: :experiment |