From b1c35b84395c630ffd77e6b10aa4fe825ebf65bf Mon Sep 17 00:00:00 2001
From: fdegier <fdegier@gitlab.com>
Date: Mon, 2 Dec 2024 13:19:18 +0100
Subject: [PATCH] Model registry: Add default experiment to model card

Changelog: added
---
 app/graphql/types/ml/model_type.rb         | 4 ++++
 app/presenters/ml/model_presenter.rb       | 4 ++++
 doc/api/graphql/reference/index.md         | 1 +
 spec/graphql/types/ml/model_type_spec.rb   | 6 ++++++
 spec/presenters/ml/model_presenter_spec.rb | 6 ++++++
 5 files changed, 21 insertions(+)

diff --git a/app/graphql/types/ml/model_type.rb b/app/graphql/types/ml/model_type.rb
index eda04383b08b8..cf4f8bd5e72f1 100644
--- a/app/graphql/types/ml/model_type.rb
+++ b/app/graphql/types/ml/model_type.rb
@@ -43,6 +43,10 @@ class ModelType < ::Types::BaseObject
       field :candidates, ::Types::Ml::CandidateType.connection_type, null: true,
         description: 'Version candidates of the model.'
 
+      field :default_experiment_path, ::GraphQL::Types::String,
+        null: true,
+        description: 'Path to default experiment page for the model.'
+
       field :version, ::Types::Ml::ModelVersionType, null: true,
         description: 'Version of the model.',
         resolver: ::Resolvers::Ml::FindModelVersionResolver
diff --git a/app/presenters/ml/model_presenter.rb b/app/presenters/ml/model_presenter.rb
index a4dbb6b535bd7..741e14b4a6cdb 100644
--- a/app/presenters/ml/model_presenter.rb
+++ b/app/presenters/ml/model_presenter.rb
@@ -34,6 +34,10 @@ def path
       project_ml_model_path(model.project, model.id)
     end
 
+    def default_experiment_path
+      project_ml_experiment_path(model.project, model.default_experiment)
+    end
+
     private
 
     def latest_version
diff --git a/doc/api/graphql/reference/index.md b/doc/api/graphql/reference/index.md
index b056b50d16d9d..98cb38b13d2f7 100644
--- a/doc/api/graphql/reference/index.md
+++ b/doc/api/graphql/reference/index.md
@@ -29342,6 +29342,7 @@ Machine learning model in the model registry.
 | <a id="mlmodelcandidatecount"></a>`candidateCount` | [`Int`](#int) | Count of candidates in the model. |
 | <a id="mlmodelcandidates"></a>`candidates` | [`MlCandidateConnection`](#mlcandidateconnection) | Version candidates of the model. (see [Connections](#connections)) |
 | <a id="mlmodelcreatedat"></a>`createdAt` | [`Time!`](#time) | Date of creation. |
+| <a id="mlmodeldefaultexperimentpath"></a>`defaultExperimentPath` | [`String`](#string) | Path to default experiment page for the model. |
 | <a id="mlmodeldescription"></a>`description` | [`String`](#string) | Description of the model. |
 | <a id="mlmodeldescriptionhtml"></a>`descriptionHtml` | [`String`](#string) | GitLab Flavored Markdown rendering of `description`. |
 | <a id="mlmodelid"></a>`id` | [`MlModelID!`](#mlmodelid) | ID of the model. |
diff --git a/spec/graphql/types/ml/model_type_spec.rb b/spec/graphql/types/ml/model_type_spec.rb
index 8447c83a4b788..5ec68575f9648 100644
--- a/spec/graphql/types/ml/model_type_spec.rb
+++ b/spec/graphql/types/ml/model_type_spec.rb
@@ -34,6 +34,7 @@
             }
             description
             descriptionHtml
+            defaultExperimentPath
             name
             versionCount
             candidateCount
@@ -65,6 +66,7 @@
             }
             description
             descriptionHtml
+            defaultExperimentPath
             name
             versionCount
             candidateCount
@@ -113,6 +115,8 @@
       'description' => 'A description',
       'descriptionHtml' =>
         '<p data-sourcepos="1:1-1:13" dir="auto">A description</p>',
+      'defaultExperimentPath' =>
+        "/#{project.full_path}/-/ml/experiments/#{model.default_experiment.id}",
       'latestVersion' => {
         'id' => model_version_id
       },
@@ -143,6 +147,8 @@
       'description' => model_markdown.description,
       'descriptionHtml' =>
         '<p data-sourcepos="1:1-1:17" dir="auto">A <strong data-sourcepos="1:3-1:17">description</strong></p>',
+      'defaultExperimentPath' =>
+        "/#{project_markdown.full_path}/-/ml/experiments/#{model_markdown.default_experiment.id}",
       'latestVersion' => {
         'id' => model_version_id_markdown
       },
diff --git a/spec/presenters/ml/model_presenter_spec.rb b/spec/presenters/ml/model_presenter_spec.rb
index b07f8d79c3a29..a4a2495f67402 100644
--- a/spec/presenters/ml/model_presenter_spec.rb
+++ b/spec/presenters/ml/model_presenter_spec.rb
@@ -99,4 +99,10 @@
 
     it { is_expected.to eq(model1.user) }
   end
+
+  describe '#default_experiment_path' do
+    subject { model1.present.default_experiment_path }
+
+    it { is_expected.to eq("/#{model1.project.full_path}/-/ml/experiments/#{model1.default_experiment.id}") }
+  end
 end
-- 
GitLab