Commit 95542f96 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '205578-gen-pkg-guest-events-list' into 'master'

Generate guest package events list

See merge request gitlab-org/gitlab!48313
parents cd76e9bf fac6c0a5
---
- i_package_composer_guest_delete
- i_package_composer_guest_pull
- i_package_composer_guest_push
- i_package_conan_guest_delete
- i_package_conan_guest_pull
- i_package_conan_guest_push
- i_package_container_guest_delete
- i_package_container_guest_pull
- i_package_container_guest_push
- i_package_debian_guest_delete
- i_package_debian_guest_pull
- i_package_debian_guest_push
- i_package_generic_guest_delete
- i_package_generic_guest_pull
- i_package_generic_guest_push
- i_package_golang_guest_delete
- i_package_golang_guest_pull
- i_package_golang_guest_push
- i_package_maven_guest_delete
- i_package_maven_guest_pull
- i_package_maven_guest_push
- i_package_npm_guest_delete
- i_package_npm_guest_pull
- i_package_npm_guest_push
- i_package_nuget_guest_delete
- i_package_nuget_guest_pull
- i_package_nuget_guest_push
- i_package_pypi_guest_delete
- i_package_pypi_guest_pull
- i_package_pypi_guest_push
- i_package_tag_guest_delete
- i_package_tag_guest_pull
- i_package_tag_guest_push
# frozen_string_literal: true
module Gitlab
module UsageDataCounters
class GuestPackageEventCounter < BaseCounter
KNOWN_EVENTS_PATH = File.expand_path('counter_events/guest_package_events.yml', __dir__)
KNOWN_EVENTS = YAML.safe_load(File.read(KNOWN_EVENTS_PATH)).freeze
PREFIX = 'package_guest'
end
end
end
......@@ -5,11 +5,29 @@ namespace :gitlab do
namespace :packages do
namespace :events do
task generate: :environment do
Rake::Task["gitlab:packages:events:generate_guest"].invoke
Rake::Task["gitlab:packages:events:generate_unique"].invoke
rescue => e
logger.error("Error building events list: #{e}")
end
task generate_guest: :environment do
logger = Logger.new(STDOUT)
logger.info('Building list of package events...')
path = File.join(File.dirname(::Gitlab::UsageDataCounters::HLLRedisCounter::KNOWN_EVENTS_PATH), 'package_events.yml')
path = Gitlab::UsageDataCounters::GuestPackageEventCounter::KNOWN_EVENTS_PATH
File.open(path, "w") { |file| file << guest_events_list.to_yaml }
logger.info("Events file `#{path}` generated successfully")
rescue => e
logger.error("Error building events list: #{e}")
end
task generate_unique: :environment do
logger = Logger.new(STDOUT)
logger.info('Building list of package events...')
path = File.join(File.dirname(Gitlab::UsageDataCounters::HLLRedisCounter::KNOWN_EVENTS_PATH), 'package_events.yml')
File.open(path, "w") { |file| file << generate_unique_events_list.to_yaml }
logger.info("Events file `#{path}` generated successfully")
......@@ -17,14 +35,16 @@ namespace :gitlab do
logger.error("Error building events list: #{e}")
end
private
def event_pairs
::Packages::Event.event_types.keys.product(::Packages::Event::EVENT_SCOPES.keys)
Packages::Event.event_types.keys.product(Packages::Event::EVENT_SCOPES.keys)
end
def generate_unique_events_list
events = event_pairs.each_with_object([]) do |(event_type, event_scope), events|
::Packages::Event.originator_types.keys.excluding('guest').each do |originator|
if name = ::Packages::Event.allowed_event_name(event_scope, event_type, originator)
Packages::Event.originator_types.keys.excluding('guest').each do |originator|
if name = Packages::Event.allowed_event_name(event_scope, event_type, originator)
events << {
"name" => name,
"category" => "#{event_scope}_packages",
......@@ -38,6 +58,12 @@ namespace :gitlab do
events.sort_by { |event| event["name"] }
end
def guest_events_list
event_pairs.map do |event_type, event_scope|
Packages::Event.allowed_event_name(event_scope, event_type, "guest")
end.compact.sort
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::UsageDataCounters::GuestPackageEventCounter, :clean_gitlab_redis_shared_state do
shared_examples_for 'usage counter with totals' do |counter|
it 'increments counter and returns total count' do
expect(described_class.read(counter)).to eq(0)
2.times { described_class.count(counter) }
expect(described_class.read(counter)).to eq(2)
end
end
it 'includes the right events' do
expect(described_class::KNOWN_EVENTS.size).to eq 33
end
described_class::KNOWN_EVENTS.each do |event|
it_behaves_like 'usage counter with totals', event
end
describe '.fetch_supported_event' do
subject { described_class.fetch_supported_event(event_name) }
let(:event_name) { 'package_guest_i_package_composer_guest_push' }
it { is_expected.to eq 'i_package_composer_guest_push' }
end
end
......@@ -7,24 +7,26 @@ RSpec.describe 'gitlab:packages:events namespace rake task' do
Rake.application.rake_require 'tasks/gitlab/packages/events'
end
describe 'generate' do
subject do
file = double('file')
yml_file = nil
subject do
file = double('file')
yml_file = nil
allow(file).to receive(:<<) { |contents| yml_file = contents }
allow(File).to receive(:open).and_yield(file)
allow(file).to receive(:<<) { |contents| yml_file = contents }
allow(File).to receive(:open).and_yield(file)
run_rake_task('gitlab:packages:events:generate')
run_rake_task("gitlab:packages:events:#{task}")
YAML.safe_load(yml_file)
end
YAML.safe_load(yml_file)
end
describe 'generate_unique' do
let(:task) { 'generate_unique' }
it 'excludes guest events' do
expect(subject.find { |event| event['name'].include?("guest") }).to be_nil
end
::Packages::Event::EVENT_SCOPES.keys.each do |event_scope|
Packages::Event::EVENT_SCOPES.keys.each do |event_scope|
it "includes includes `#{event_scope}` scope" do
expect(subject.find { |event| event['name'].include?(event_scope) }).not_to be_nil
end
......@@ -35,4 +37,19 @@ RSpec.describe 'gitlab:packages:events namespace rake task' do
expect(subject.find { |event| event['name'].include?("list_package") }).to be_nil
end
end
describe 'generate_guest' do
let(:task) { 'generate_guest' }
Packages::Event::EVENT_SCOPES.keys.each do |event_scope|
it "includes includes `#{event_scope}` scope" do
expect(subject.find { |event| event.include?(event_scope) }).not_to be_nil
end
end
it 'excludes some event types' do
expect(subject.find { |event| event.include?("search_package") }).to be_nil
expect(subject.find { |event| event.include?("list_package") }).to be_nil
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