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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.7 KiB
Ruby
Raw Normal View History

2022-08-27 11:52:29 +05:30
# frozen_string_literal: true
module Ml
class Candidate < ApplicationRecord
2023-03-17 16:20:25 +05:30
PACKAGE_PREFIX = 'ml_candidate_'
2022-10-11 01:57:18 +05:30
enum status: { running: 0, scheduled: 1, finished: 2, failed: 3, killed: 4 }
2022-08-27 11:52:29 +05:30
validates :iid, :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
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-01-13 00:05:48 +05:30
attribute :iid, default: -> { SecureRandom.uuid }
2023-03-17 16:20:25 +05:30
scope :including_relationships, -> { includes(:latest_metrics, :params, :user) }
delegate :project_id, :project, 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 artifact
2023-03-17 16:20:25 +05:30
artifact_lazy&.itself
end
def artifact_lazy
BatchLoader.for(id).batch do |candidate_ids, loader|
Packages::Package
.joins("INNER JOIN ml_candidates ON packages_packages.name=(concat('#{PACKAGE_PREFIX}', ml_candidates.id))")
.where(ml_candidates: { id: candidate_ids })
.find_each do |package|
loader.call(package.name.delete_prefix(PACKAGE_PREFIX).to_i, package)
end
end
2023-03-04 22:38:38 +05:30
end
def package_name
2023-03-17 16:20:25 +05:30
"#{PACKAGE_PREFIX}#{id}"
2023-03-04 22:38:38 +05:30
end
def package_version
'-'
2023-01-13 00:05:48 +05:30
end
2022-10-11 01:57:18 +05:30
class << self
def with_project_id_and_iid(project_id, iid)
return unless project_id.present? && iid.present?
joins(:experiment).find_by(experiment: { project_id: project_id }, iid: iid)
end
end
2022-08-27 11:52:29 +05:30
end
end