Commit fac6c0a5 authored by ggelatti's avatar ggelatti

Adds guest package event rake tasks

- Adds rake task to generate guest package events
- Adds class to track guest package events
- Specs
parent 935ed217
---
- 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 ...@@ -5,11 +5,29 @@ namespace :gitlab do
namespace :packages do namespace :packages do
namespace :events do namespace :events do
task generate: :environment 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 = Logger.new(STDOUT)
logger.info('Building list of package events...') 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 } File.open(path, "w") { |file| file << generate_unique_events_list.to_yaml }
logger.info("Events file `#{path}` generated successfully") logger.info("Events file `#{path}` generated successfully")
...@@ -17,14 +35,16 @@ namespace :gitlab do ...@@ -17,14 +35,16 @@ namespace :gitlab do
logger.error("Error building events list: #{e}") logger.error("Error building events list: #{e}")
end end
private
def event_pairs 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 end
def generate_unique_events_list def generate_unique_events_list
events = event_pairs.each_with_object([]) do |(event_type, event_scope), events| events = event_pairs.each_with_object([]) do |(event_type, event_scope), events|
::Packages::Event.originator_types.keys.excluding('guest').each do |originator| Packages::Event.originator_types.keys.excluding('guest').each do |originator|
if name = ::Packages::Event.allowed_event_name(event_scope, event_type, originator) if name = Packages::Event.allowed_event_name(event_scope, event_type, originator)
events << { events << {
"name" => name, "name" => name,
"category" => "#{event_scope}_packages", "category" => "#{event_scope}_packages",
...@@ -38,6 +58,12 @@ namespace :gitlab do ...@@ -38,6 +58,12 @@ namespace :gitlab do
events.sort_by { |event| event["name"] } events.sort_by { |event| event["name"] }
end 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 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 ...@@ -7,24 +7,26 @@ RSpec.describe 'gitlab:packages:events namespace rake task' do
Rake.application.rake_require 'tasks/gitlab/packages/events' Rake.application.rake_require 'tasks/gitlab/packages/events'
end end
describe 'generate' do subject do
subject do file = double('file')
file = double('file') yml_file = nil
yml_file = nil
allow(file).to receive(:<<) { |contents| yml_file = contents } allow(file).to receive(:<<) { |contents| yml_file = contents }
allow(File).to receive(:open).and_yield(file) 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) YAML.safe_load(yml_file)
end end
describe 'generate_unique' do
let(:task) { 'generate_unique' }
it 'excludes guest events' do it 'excludes guest events' do
expect(subject.find { |event| event['name'].include?("guest") }).to be_nil expect(subject.find { |event| event['name'].include?("guest") }).to be_nil
end 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 it "includes includes `#{event_scope}` scope" do
expect(subject.find { |event| event['name'].include?(event_scope) }).not_to be_nil expect(subject.find { |event| event['name'].include?(event_scope) }).not_to be_nil
end end
...@@ -35,4 +37,19 @@ RSpec.describe 'gitlab:packages:events namespace rake task' do ...@@ -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 expect(subject.find { |event| event['name'].include?("list_package") }).to be_nil
end end
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 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