summaryrefslogtreecommitdiff
path: root/app/presenters/ml/candidate_details_presenter.rb
blob: 58ec2aee471064457f522c0e60b647f2513e2428 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

module Ml
  class CandidateDetailsPresenter
    include Rails.application.routes.url_helpers

    def initialize(candidate)
      @candidate = candidate
    end

    def present
      data = {
        candidate: {
          info: {
            iid: candidate.iid,
            eid: candidate.eid,
            path_to_artifact: link_to_artifact,
            experiment_name: candidate.experiment.name,
            path_to_experiment: link_to_experiment,
            path: link_to_details,
            status: candidate.status,
            ci_job: job_info
          },
          params: candidate.params,
          metrics: candidate.latest_metrics,
          metadata: candidate.metadata
        }
      }

      Gitlab::Json.generate(data)
    end

    private

    attr_reader :candidate

    def job_info
      return unless candidate.from_ci?

      build = candidate.ci_build

      {
        path: project_job_path(build.project, build),
        name: build.name,
        **user_info(build.user) || {},
        **mr_info(build.pipeline.merge_request) || {}
      }
    end

    def user_info(user)
      return unless user

      {
        user: {
          path: user_path(user),
          username: user.username
        }
      }
    end

    def mr_info(mr)
      return unless mr

      {
        merge_request: {
          path: project_merge_request_path(mr.project, mr),
          title: mr.title
        }
      }
    end

    def link_to_artifact
      artifact = candidate.artifact

      return unless artifact.present?

      project_package_path(candidate.project, artifact)
    end

    def link_to_details
      project_ml_candidate_path(candidate.project, candidate.iid)
    end

    def link_to_experiment
      project_ml_experiment_path(candidate.project, candidate.experiment.iid)
    end
  end
end