Welcome to mirror list, hosted at ThFree Co, Russian Federation.

experiments_helper.rb « ml « projects « helpers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c7b6eb6fbcbc8c230e8732fa376b17ab1bc8f7b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true
module Projects
  module Ml
    module ExperimentsHelper
      require 'json'
      include ActionView::Helpers::NumberHelper

      def experiment_as_data(experiment)
        data = {
          name: experiment.name,
          path: link_to_experiment(experiment.project, experiment)
        }

        Gitlab::Json.generate(data)
      end

      def candidates_table_items(candidates, current_user)
        items = candidates.map do |candidate|
          {
            **candidate.params.to_h { |p| [p.name, p.value] },
            **candidate.latest_metrics.to_h { |m| [m.name, number_with_precision(m.value, precision: 4)] },
            ci_job: job_info(candidate, current_user),
            artifact: link_to_artifact(candidate),
            details: link_to_details(candidate),
            name: candidate.name,
            created_at: candidate.created_at,
            user: user_info(candidate, current_user)
          }
        end

        Gitlab::Json.generate(items)
      end

      def unique_logged_names(candidates, &selector)
        Gitlab::Json.generate(candidates.flat_map(&selector).map(&:name).uniq)
      end

      def experiments_as_data(project, experiments)
        data = experiments.map do |exp|
          {
            name: exp.name,
            path: link_to_experiment(project, exp),
            candidate_count: exp.candidate_count
          }
        end

        Gitlab::Json.generate(data)
      end

      def page_info(paginator)
        {
          has_next_page: paginator.has_next_page?,
          has_previous_page: paginator.has_previous_page?,
          start_cursor: paginator.cursor_for_previous_page,
          end_cursor: paginator.cursor_for_next_page
        }
      end

      def formatted_page_info(page_info)
        Gitlab::Json.generate(page_info)
      end

      def link_to_artifact(candidate)
        artifact = candidate.artifact

        return unless artifact.present?

        project_package_path(candidate.project, artifact)
      end

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

      def job_info(candidate, user)
        return unless candidate.from_ci? && can?(user, :read_build, candidate.ci_build)

        build = candidate.ci_build

        {
          path: project_job_path(build.project, build),
          name: build.name
        }
      end

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

      def user_info(candidate, current_user)
        user =
          if candidate.from_ci?
            candidate.ci_build.user if can?(current_user, :read_build, candidate.ci_build)
          else
            candidate.user
          end

        return unless user.present?

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