debian-mirror-gitlab/app/models/ml/candidate.rb

86 lines
2.8 KiB
Ruby
Raw Normal View History

2022-08-27 11:52:29 +05:30
# frozen_string_literal: true
module Ml
class Candidate < ApplicationRecord
2023-04-23 21:23:45 +05:30
include Sortable
2023-06-20 00:43:36 +05:30
include AtomicInternalId
include IgnorableColumns
2023-04-23 21:23:45 +05:30
2023-06-20 00:43:36 +05:30
ignore_column :iid, remove_with: '16.0', remove_after: '2023-05-01'
2023-03-17 16:20:25 +05:30
2022-10-11 01:57:18 +05:30
enum status: { running: 0, scheduled: 1, finished: 2, failed: 3, killed: 4 }
2023-06-20 00:43:36 +05:30
validates :eid, :experiment, presence: true
2022-10-11 01:57:18 +05:30
validates :status, inclusion: { in: statuses.keys }
2022-08-27 11:52:29 +05:30
belongs_to :experiment, class_name: 'Ml::Experiment'
belongs_to :user
2023-06-20 00:43:36 +05:30
belongs_to :package, class_name: 'Packages::Package'
belongs_to :project
2022-08-27 11:52:29 +05:30
has_many :metrics, class_name: 'Ml::CandidateMetric'
has_many :params, class_name: 'Ml::CandidateParam'
2023-03-04 22:38:38 +05:30
has_many :metadata, class_name: 'Ml::CandidateMetadata'
2023-01-13 00:05:48 +05:30
has_many :latest_metrics, -> { latest }, class_name: 'Ml::CandidateMetric', inverse_of: :candidate
2022-10-11 01:57:18 +05:30
2023-06-20 00:43:36 +05:30
attribute :eid, default: -> { SecureRandom.uuid }
2023-01-13 00:05:48 +05:30
2023-06-20 00:43:36 +05:30
has_internal_id :internal_id,
scope: :project,
init: AtomicInternalId.project_init(self, :internal_id)
scope :including_relationships, -> { includes(:latest_metrics, :params, :user, :package, :project) }
2023-04-23 21:23:45 +05:30
scope :by_name, ->(name) { where("ml_candidates.name LIKE ?", "%#{sanitize_sql_like(name)}%") } # rubocop:disable GitlabSecurity/SqlInjection
2023-06-20 00:43:36 +05:30
2023-04-23 21:23:45 +05:30
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
2023-03-17 16:20:25 +05:30
2023-06-20 00:43:36 +05:30
alias_attribute :artifact, :package
alias_attribute :iid, :internal_id
delegate :package_name, to: :experiment
2023-01-13 00:05:48 +05:30
def artifact_root
2023-03-04 22:38:38 +05:30
"/#{package_name}/#{package_version}/"
end
def package_version
2023-06-20 00:43:36 +05:30
iid
2023-01-13 00:05:48 +05:30
end
2022-10-11 01:57:18 +05:30
class << self
2023-06-20 00:43:36 +05:30
def with_project_id_and_eid(project_id, eid)
return unless project_id.present? && eid.present?
find_by(project_id: project_id, eid: eid)
end
2022-10-11 01:57:18 +05:30
def with_project_id_and_iid(project_id, iid)
return unless project_id.present? && iid.present?
2023-06-20 00:43:36 +05:30
find_by(project_id: project_id, internal_id: iid)
2022-10-11 01:57:18 +05:30
end
end
2022-08-27 11:52:29 +05:30
end
end