Commit 2b5e3a6a authored by Matthias Käppler's avatar Matthias Käppler

Merge branch...

Merge branch '331285-add-support-for-creating-multiple-files-in-bulk-to-metric-definitions-generator' into 'master'

Add support for creating multiple files for metric definition generator

See merge request gitlab-org/gitlab!73602
parents 702b46ae 3867ab22
......@@ -197,18 +197,28 @@ tier:
The GitLab codebase provides a dedicated [generator](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/generators/gitlab/usage_metric_definition_generator.rb) to create new metric definitions.
For uniqueness, the generated file includes a timestamp prefix, in ISO 8601 format.
For uniqueness, the generated files include a timestamp prefix in ISO 8601 format.
The generator takes the key path argument and 2 options and creates the metric YAML definition in corresponding location:
The generator takes a list of key paths and 2 options as arguments. It creates metric YAML definitions in the corresponding location:
- `--ee`, `--no-ee` Indicates if metric is for EE.
- `--dir=DIR` indicates the metric directory. It must be one of: `counts_7d`, `7d`, `counts_28d`, `28d`, `counts_all`, `all`, `settings`, `license`.
- `--dir=DIR` Indicates the metric directory. It must be one of: `counts_7d`, `7d`, `counts_28d`, `28d`, `counts_all`, `all`, `settings`, `license`.
**Single metric example**
```shell
bundle exec rails generate gitlab:usage_metric_definition counts.issues --dir=7d
create config/metrics/counts_7d/issues.yml
```
**Multiple metrics example**
```shell
bundle exec rails generate gitlab:usage_metric_definition counts.issues counts.users --dir=7d
create config/metrics/counts_7d/issues.yml
create config/metrics/counts_7d/users.yml
```
NOTE:
To create a metric definition used in EE, add the `--ee` flag.
......
---
key_path: <%= key_path %><%= metric_name_suggestion %>
key_path: <%= args.second %><%= metric_name_suggestion(args.second) %>
description:
product_section:
product_stage:
......
......@@ -30,18 +30,20 @@ module Gitlab
source_root File.expand_path('../../../generator_templates/usage_metric_definition', __dir__)
desc 'Generates a metric definition yml file'
desc 'Generates metric definitions yml files'
class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if metric is for ee'
class_option :dir,
type: :string, desc: "Indicates the metric location. It must be one of: #{VALID_INPUT_DIRS.join(', ')}"
argument :key_path, type: :string, desc: 'Unique JSON key path for the metric'
argument :key_paths, type: :array, desc: 'Unique JSON key paths for the metrics'
def create_metric_file
validate!
template "metric_definition.yml", file_path
key_paths.each do |key_path|
template "metric_definition.yml", file_path(key_path), key_path
end
end
def time_frame
......@@ -66,12 +68,12 @@ module Gitlab
private
def metric_name_suggestion
def metric_name_suggestion(key_path)
"\nname: \"#{Usage::Metrics::NamesSuggestions::Generator.generate(key_path)}\""
end
def file_path
path = File.join(TOP_LEVEL_DIR, 'metrics', directory&.name, "#{file_name}.yml")
def file_path(key_path)
path = File.join(TOP_LEVEL_DIR, 'metrics', directory&.name, "#{file_name(key_path)}.yml")
path = File.join(TOP_LEVEL_DIR_EE, path) if ee?
path
end
......@@ -79,7 +81,10 @@ module Gitlab
def validate!
raise "--dir option is required" unless input_dir.present?
raise "Invalid dir #{input_dir}, allowed options are #{VALID_INPUT_DIRS.join(', ')}" unless directory.present?
raise "Metric definition with key path '#{key_path}' already exists" if metric_definition_exists?
key_paths.each do |key_path|
raise "Metric definition with key path '#{key_path}' already exists" if metric_definition_exists?(key_path)
end
end
def ee?
......@@ -93,15 +98,15 @@ module Gitlab
# Example of file name
#
# 20210201124931_g_project_management_issue_title_changed_weekly.yml
def file_name
"#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_#{metric_name}"
def file_name(key_path)
"#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_#{metric_name(key_path)}"
end
def directory
@directory ||= TIME_FRAME_DIRS.find { |d| d.match?(input_dir) }
end
def metric_name
def metric_name(key_path)
key_path.split('.').last
end
......@@ -109,7 +114,7 @@ module Gitlab
@definitions ||= Gitlab::Usage::MetricDefinition.definitions(skip_validation: true)
end
def metric_definition_exists?
def metric_definition_exists?(key_path)
metric_definitions[key_path].present?
end
end
......
......@@ -99,4 +99,15 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator, :silence_stdout do
expect(YAML.safe_load(File.read(metric_definition_path))).to include("name" => "some name")
end
end
context 'with multiple file names' do
let(:key_paths) { ['counts_weekly.test_metric', 'counts_weekly.test1_metric'] }
it 'creates multiple files' do
described_class.new(key_paths, { 'dir' => dir }).invoke_all
files = Dir.glob(File.join(temp_dir, 'metrics/counts_7d/*_metric.yml'))
expect(files.count).to eq(2)
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