debian-mirror-gitlab/app/helpers/projects/ml/experiments_helper.rb

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

101 lines
2.7 KiB
Ruby
Raw Normal View History

2023-01-13 00:05:48 +05:30
# frozen_string_literal: true
module Projects
module Ml
module ExperimentsHelper
require 'json'
include ActionView::Helpers::NumberHelper
2023-04-23 21:23:45 +05:30
def show_candidate_view_model(candidate)
data = {
candidate: {
params: candidate.params,
metrics: candidate.latest_metrics,
info: {
iid: candidate.iid,
path_to_artifact: link_to_artifact(candidate),
experiment_name: candidate.experiment.name,
path_to_experiment: link_to_experiment(candidate.project, candidate.experiment),
status: candidate.status
},
metadata: candidate.metadata
}
}
Gitlab::Json.generate(data)
end
2023-01-13 00:05:48 +05:30
def candidates_table_items(candidates)
items = candidates.map do |candidate|
{
**candidate.params.to_h { |p| [p.name, p.value] },
2023-03-04 22:38:38 +05:30
**candidate.latest_metrics.to_h { |m| [m.name, number_with_precision(m.value, precision: 4)] },
artifact: link_to_artifact(candidate),
2023-03-17 16:20:25 +05:30
details: link_to_details(candidate),
name: candidate.name,
created_at: candidate.created_at,
user: user_info(candidate)
2023-01-13 00:05:48 +05:30
}
end
Gitlab::Json.generate(items)
end
def unique_logged_names(candidates, &selector)
Gitlab::Json.generate(candidates.flat_map(&selector).map(&:name).uniq)
end
2023-03-04 22:38:38 +05:30
2023-04-23 21:23:45 +05:30
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
2023-03-04 22:38:38 +05:30
Gitlab::Json.generate(data)
end
2023-04-23 21:23:45 +05:30
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
2023-03-04 22:38:38 +05:30
def link_to_artifact(candidate)
artifact = candidate.artifact
return unless artifact.present?
2023-03-17 16:20:25 +05:30
project_package_path(candidate.project, artifact)
2023-03-04 22:38:38 +05:30
end
def link_to_details(candidate)
2023-03-17 16:20:25 +05:30
project_ml_candidate_path(candidate.project, candidate.iid)
2023-03-04 22:38:38 +05:30
end
2023-04-23 21:23:45 +05:30
def link_to_experiment(project, experiment)
project_ml_experiment_path(project, experiment.iid)
2023-03-04 22:38:38 +05:30
end
2023-03-17 16:20:25 +05:30
def user_info(candidate)
user = candidate.user
return unless user.present?
{
username: user.username,
path: user_path(user)
}
end
2023-01-13 00:05:48 +05:30
end
end
end