summaryrefslogtreecommitdiff
path: root/app/models/ml/candidate.rb
blob: f24161d598f3d037ac1df373aca6e00718ed9ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

module Ml
  class Candidate < ApplicationRecord
    enum status: { running: 0, scheduled: 1, finished: 2, failed: 3, killed: 4 }

    validates :iid, :experiment, presence: true
    validates :status, inclusion: { in: statuses.keys }

    belongs_to :experiment, class_name: 'Ml::Experiment'
    belongs_to :user
    has_many :metrics, class_name: 'Ml::CandidateMetric'
    has_many :params, class_name: 'Ml::CandidateParam'
    has_many :metadata, class_name: 'Ml::CandidateMetadata'
    has_many :latest_metrics, -> { latest }, class_name: 'Ml::CandidateMetric', inverse_of: :candidate

    attribute :iid, default: -> { SecureRandom.uuid }

    scope :including_metrics_and_params, -> { includes(:latest_metrics, :params) }

    def artifact_root
      "/#{package_name}/#{package_version}/"
    end

    def artifact
      ::Packages::Generic::PackageFinder.new(experiment.project).execute!(package_name, package_version)
    rescue ActiveRecord::RecordNotFound
      nil
    end

    def package_name
      "ml_candidate_#{iid}"
    end

    def package_version
      '-'
    end

    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
  end
end