Commit db57cd3b authored by Arturo Herrero's avatar Arturo Herrero

Merge branch 'mwaw/usage_ping_sql_metrics_names_generator' into 'master'

Add names suggestions for Product Intelligence metrics [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!55733
parents b01c6bb3 d0e97256
---
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
......
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module NamesSuggestions
class Generator < ::Gitlab::UsageData
FREE_TEXT_METRIC_NAME = "<please fill metric name>"
class << self
def generate(key_path)
uncached_data.deep_stringify_keys.dig(*key_path.split('.'))
end
private
def count(relation, column = nil, batch: true, batch_size: nil, start: nil, finish: nil)
"count_#{parse_target_and_source(column, relation)}"
end
def distinct_count(relation, column = nil, batch: true, batch_size: nil, start: nil, finish: nil)
"count_distinct_#{parse_target_and_source(column, relation)}"
end
def redis_usage_counter
FREE_TEXT_METRIC_NAME
end
def alt_usage_data(*)
FREE_TEXT_METRIC_NAME
end
def redis_usage_data_totals(counter)
counter.fallback_totals.transform_values { |_| FREE_TEXT_METRIC_NAME}
end
def sum(relation, column, *rest)
"sum_#{parse_target_and_source(column, relation)}"
end
def estimate_batch_distinct_count(relation, column = nil, *rest)
"estimate_distinct_#{parse_target_and_source(column, relation)}"
end
def add(*args)
"add_#{args.join('_and_')}"
end
def parse_target_and_source(column, relation)
if column
"#{column}_from_#{relation.table_name}"
else
relation.table_name
end
end
end
end
end
end
end
end
# 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
# frozen_string_literal: true
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
expect(described_class.generate('counts.boards')).to eq 'count_boards'
end
end
context 'for count distinct metrics' do
it 'return correct name' do
expect(described_class.generate('counts.issues_using_zoom_quick_actions')).to eq 'count_distinct_issue_id_from_zoom_meetings'
end
end
context 'for sum metrics' do
it 'return correct name' do
expect(described_class.generate('counts.jira_imports_total_imported_issues_count')).to eq 'sum_imported_issues_count_from_jira_imports'
end
end
context 'for add metrics' do
it 'return correct name' do
expect(described_class.generate('counts.snippets')).to eq 'add_count_snippets_and_count_snippets'
end
end
context 'for redis metrics' do
it 'return correct name' do
expect(described_class.generate('analytics_unique_visits.analytics_unique_visits_for_any_target')).to eq '<please fill metric name>'
end
end
context 'for alt_usage_data metrics' do
it 'return correct name' do
expect(described_class.generate('settings.operating_system')).to eq '<please fill metric name>'
end
end
end
end
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