Commit 09f7b555 authored by David Fernandez's avatar David Fernandez

Merge branch '322103-optimize-the-dictionary-view-to-not-repeat-same-columns' into 'master'

Optimize the Dictionary view to not repeat same columns

See merge request gitlab-org/gitlab!55145
parents 0e59f6ae 1ff30fad
---
title: Optimize Metrics Dictionary
merge_request: 55145
author:
type: changed
This diff is collapsed.
...@@ -5,9 +5,6 @@ module Gitlab ...@@ -5,9 +5,6 @@ module Gitlab
module Docs module Docs
# Helper with functions to be used by HAML templates # Helper with functions to be used by HAML templates
module Helper module Helper
HEADER = %w(field value).freeze
SKIP_KEYS = %i(description).freeze
def auto_generated_comment def auto_generated_comment
<<-MARKDOWN.strip_heredoc <<-MARKDOWN.strip_heredoc
--- ---
...@@ -27,37 +24,33 @@ module Gitlab ...@@ -27,37 +24,33 @@ module Gitlab
end end
def render_name(name) def render_name(name)
"## `#{name}`\n" "### `#{name}`"
end end
def render_description(object) def render_description(object)
return 'Missing description' unless object.attributes[:description].present? return 'Missing description' unless object[:description].present?
object.attributes[:description] object[:description]
end end
def render_attribute_row(key, value) def render_yaml_link(yaml_path)
value = Gitlab::Usage::Docs::ValueFormatter.format(key, value) "[YAML definition](#{yaml_path})"
table_row(["`#{key}`", value])
end end
def render_attributes_table(object) def render_status(object)
<<~MARKDOWN "Status: #{format(:status, object[:status])}"
end
#{table_row(HEADER)} def render_owner(object)
#{table_row(HEADER.map { '---' })} "Group: `#{object[:product_group]}`"
#{table_value_rows(object.attributes)}
MARKDOWN
end end
def table_value_rows(attributes) def render_tiers(object)
attributes.reject { |k, _| k.in?(SKIP_KEYS) }.map do |key, value| "Tiers: #{format(:tier, object[:tier])}"
render_attribute_row(key, value)
end.join("\n")
end end
def table_row(array) def format(key, value)
"| #{array.join(' | ')} |" Gitlab::Usage::Docs::ValueFormatter.format(key, value)
end end
end end
end end
......
...@@ -16,13 +16,23 @@ ...@@ -16,13 +16,23 @@
- [`config/metrics`](https://gitlab.com/gitlab-org/gitlab/-/tree/master/config/metrics) - [`config/metrics`](https://gitlab.com/gitlab-org/gitlab/-/tree/master/config/metrics)
- [`ee/config/metrics`](https://gitlab.com/gitlab-org/gitlab/-/tree/master/ee/config/metrics) - [`ee/config/metrics`](https://gitlab.com/gitlab-org/gitlab/-/tree/master/ee/config/metrics)
Each table includes a `milestone`, which corresponds to the GitLab version when the metric Each table includes a `milestone`, which corresponds to the GitLab version when the metric
was released. was released.
## Metrics Definitions
\ \
- metrics_definitions.each do |name, object| - metrics_definitions.each do |name, object|
= render_name(name) = render_name(name)
\
= render_description(object) = render_description(object.attributes)
\
= render_attributes_table(object) = render_yaml_link(object.yaml_path)
\
= render_owner(object.attributes)
\
= render_status(object.attributes)
\
= render_tiers(object.attributes)
\
...@@ -12,12 +12,12 @@ module Gitlab ...@@ -12,12 +12,12 @@ module Gitlab
"**`#{value}`**" "**`#{value}`**"
when :data_source when :data_source
value.to_s.capitalize value.to_s.capitalize
when :product_group, :product_category when :product_group, :product_category, :status
"`#{value}`" "`#{value}`"
when :introduced_by_url when :introduced_by_url
"[Introduced by](#{value})" "[Introduced by](#{value})"
when :distribution, :tier when :distribution, :tier
Array(value).join(', ') Array(value).map { |tier| "`#{tier}`" }.join(', ')
else else
value value
end end
......
...@@ -4,6 +4,7 @@ module Gitlab ...@@ -4,6 +4,7 @@ module Gitlab
module Usage module Usage
class MetricDefinition class MetricDefinition
METRIC_SCHEMA_PATH = Rails.root.join('config', 'metrics', 'schema.json') METRIC_SCHEMA_PATH = Rails.root.join('config', 'metrics', 'schema.json')
BASE_REPO_PATH = 'https://gitlab.com/gitlab-org/gitlab/-/blob/master'
attr_reader :path attr_reader :path
attr_reader :attributes attr_reader :attributes
...@@ -21,6 +22,10 @@ module Gitlab ...@@ -21,6 +22,10 @@ module Gitlab
attributes attributes
end end
def yaml_path
"#{BASE_REPO_PATH}#{path.delete_prefix(Rails.root.to_s)}"
end
def validate! def validate!
unless skip_validation? unless skip_validation?
self.class.schemer.validate(attributes.stringify_keys).each do |error| self.class.schemer.validate(attributes.stringify_keys).each do |error|
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
require 'spec_helper' require 'spec_helper'
CODE_REGEX = %r{<code>(.*)</code>}.freeze
RSpec.describe Gitlab::Usage::Docs::Renderer do RSpec.describe Gitlab::Usage::Docs::Renderer do
describe 'contents' do describe 'contents' do
let(:dictionary_path) { Gitlab::Usage::Docs::Renderer::DICTIONARY_PATH } let(:dictionary_path) { Gitlab::Usage::Docs::Renderer::DICTIONARY_PATH }
...@@ -9,12 +11,12 @@ RSpec.describe Gitlab::Usage::Docs::Renderer do ...@@ -9,12 +11,12 @@ RSpec.describe Gitlab::Usage::Docs::Renderer do
it 'generates dictionary for given items' do it 'generates dictionary for given items' do
generated_dictionary = described_class.new(items).contents generated_dictionary = described_class.new(items).contents
generated_dictionary_keys = RDoc::Markdown generated_dictionary_keys = RDoc::Markdown
.parse(generated_dictionary) .parse(generated_dictionary)
.table_of_contents .table_of_contents
.select { |metric_doc| metric_doc.level == 2 && !metric_doc.text.start_with?('info:') } .select { |metric_doc| metric_doc.level == 3 }
.map(&:text) .map { |item| item.text.match(CODE_REGEX)&.captures&.first }
.map { |text| text.sub('<code>', '').sub('</code>', '') }
expect(generated_dictionary_keys).to match_array(items.keys) expect(generated_dictionary_keys).to match_array(items.keys)
end end
......
...@@ -10,11 +10,11 @@ RSpec.describe Gitlab::Usage::Docs::ValueFormatter do ...@@ -10,11 +10,11 @@ RSpec.describe Gitlab::Usage::Docs::ValueFormatter do
:data_source | 'redis' | 'Redis' :data_source | 'redis' | 'Redis'
:data_source | 'ruby' | 'Ruby' :data_source | 'ruby' | 'Ruby'
:introduced_by_url | 'http://test.com' | '[Introduced by](http://test.com)' :introduced_by_url | 'http://test.com' | '[Introduced by](http://test.com)'
:tier | %w(gold premium) | 'gold, premium' :tier | %w(gold premium) | '`gold`, `premium`'
:distribution | %w(ce ee) | 'ce, ee' :distribution | %w(ce ee) | '`ce`, `ee`'
:key_path | 'key.path' | '**`key.path`**' :key_path | 'key.path' | '**`key.path`**'
:milestone | '13.4' | '13.4' :milestone | '13.4' | '13.4'
:status | 'data_available' | 'data_available' :status | 'data_available' | '`data_available`'
end end
with_them do with_them do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment