Commit f49a8dbf authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak

Enhance metric definition generator

Include name suggestion in metric definition
parent f99218fb
---
name: product_intelligence_metrics_names_suggestions
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/55733
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/323460
milestone: '13.10'
type: development
group: group::product intelligence
default_enabled: false
---
# See Usage Ping metrics dictionary docs https://docs.gitlab.com/ee/development/usage_ping/metrics_dictionary.html
key_path: <%= key_path %>
key_path: <%= key_path %><%= metric_name_suggestion %>
description:
product_section:
product_stage:
......
......@@ -18,6 +18,9 @@ module Gitlab
Directory.new('license', 'none', 'string')
].freeze
TOP_LEVEL_DIR = 'config'
TOP_LEVEL_DIR_EE = 'ee'
VALID_INPUT_DIRS = (TIME_FRAME_DIRS.flat_map { |d| [d.name, d.time_frame] } - %w(none)).freeze
source_root File.expand_path('../../../generator_templates/usage_metric_definition', __dir__)
......@@ -56,9 +59,15 @@ module Gitlab
private
def metric_name_suggestion
return unless Feature.enabled?(:product_intelligence_metrics_names_suggestions, default_enabled: :yaml)
"\nname: #{Usage::Metrics::NamesSuggestions::Generator.generate(key_path)}"
end
def file_path
path = File.join('config', 'metrics', directory&.name, "#{file_name}.yml")
path = File.join('ee', path) if ee?
path = File.join(TOP_LEVEL_DIR, 'metrics', directory&.name, "#{file_name}.yml")
path = File.join(TOP_LEVEL_DIR_EE, path) if ee?
path
end
......
......@@ -40,7 +40,6 @@ module Gitlab
"add_#{args.join('_and_')}"
end
def parse_target_and_source(column, relation)
if column
"#{column}_from_#{relation.table_name}"
......
# frozen_string_literal: true
require 'spec_helper'
require 'rails/generators/testing/behaviour'
RSpec.describe Gitlab::UsageMetricDefinitionGenerator do
let(:temp_dir) { Dir.mktmpdir }
before do
stub_const("#{described_class}::TOP_LEVEL_DIR", temp_dir)
end
context 'with product_intelligence_metrics_names_suggestions feature ON' do
it 'adds name key to metric definition' do
stub_feature_flags(product_intelligence_metrics_names_suggestions: true)
expect(::Gitlab::Usage::Metrics::NamesSuggestions::Generator).to receive(:generate).and_return('some name')
described_class.new(['counts_weekly.test_metric'], { 'dir' => '7d' }).invoke_all
metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first
expect(YAML.safe_load(File.read(metric_definition_path))).to include("name" => "some name")
end
end
context 'with product_intelligence_metrics_names_suggestions feature OFF' do
it 'adds name key to metric definition' do
stub_feature_flags(product_intelligence_metrics_names_suggestions: false)
expect(::Gitlab::Usage::Metrics::NamesSuggestions::Generator).not_to receive(:generate)
described_class.new(['counts_weekly.test_metric'], { 'dir' => '7d' }).invoke_all
metric_definition_path = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_test_metric.yml')).first
expect(YAML.safe_load(File.read(metric_definition_path)).keys).not_to include(:name)
end
end
end
......@@ -3,6 +3,12 @@
require 'spec_helper'
RSpec.describe Gitlab::Usage::Metrics::NamesSuggestions::Generator do
include UsageDataHelpers
before do
stub_usage_data_connections
end
describe '#generate' do
context 'for count metrics' do
it 'return correct name' 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