Commit 36aa8479 authored by alinamihaila's avatar alinamihaila

Add helper method for API

parent 9063dea6
...@@ -288,6 +288,29 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF ...@@ -288,6 +288,29 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
end end
``` ```
1. Track event in API using `increment_unique_events(event_name, values)` helper method
In order to be able to track the events usage ping should be enabled and event feature `usage_data_<event_name>` should be enabled.
Arguments:
- `event_name`: event name.
- `values`: values we count unique events
Example usage:
```ruby
get ':id/registry/repositories' do
repositories = ContainerRepositoriesFinder.new(
user: current_user, subject: user_group
).execute
increment_unique_events('i_list_repositories', current_user.id)
present paginate(repositories), with: Entities::ContainerRegistry::Repository, tags: params[:tags], tags_count: params[:tags_count]
end
```
1. Track event using base module `Gitlab::UsageDataCounters::HLLRedisCounter.track_event(entity_id, event_name)`. 1. Track event using base module `Gitlab::UsageDataCounters::HLLRedisCounter.track_event(entity_id, event_name)`.
Arguments: Arguments:
......
...@@ -537,6 +537,19 @@ module API ...@@ -537,6 +537,19 @@ module API
) )
end end
def increment_unique_values(event_name, values)
feature_name = "usage_data_#{event_name}"
raise "Feature #{feature_name} not enabled" unless Feature.enabled?(feature_name)
raise "Usage ping not enabled" unless Gitlab::CurrentSettings.usage_ping_enabled?
raise "values is empty" unless values.present?
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(values, event_name)
rescue => error
Rails.logger.warn( # rubocop:disable Gitlab/RailsLogger
"Redis tracking event failed for event: #{event_name}, message: #{error.message}"
)
end
def with_api_params(&block) def with_api_params(&block)
yield({ api: true, request: request }) yield({ api: true, request: request })
end end
......
...@@ -189,6 +189,54 @@ RSpec.describe API::Helpers do ...@@ -189,6 +189,54 @@ RSpec.describe API::Helpers do
end end
end end
describe '#increment_unique_values' do
let(:value) { "9f302fea-f828-4ca9-aef4-e10bd723c0b3" }
let(:event_name) { 'my_event' }
let(:unknown_event) { 'unknown' }
let(:feature) { "usage_data_#{event_name}" }
context 'with feature enabled' do
before do
stub_feature_flags(feature => true)
end
it 'tracks redis hll event' do
stub_application_setting(usage_ping_enabled: true)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(value, event_name)
subject.increment_unique_values(event_name, value)
end
it 'logs an exception if usage ping is not enabled' do
stub_application_setting(usage_ping_enabled: false)
expect(Rails.logger).to receive(:warn).with("Redis tracking event failed for event: #{event_name}, message: Usage ping not enabled")
subject.increment_unique_values(event_name, value)
end
it 'logs an exception for unknown event' do
stub_application_setting(usage_ping_enabled: true)
expect(Rails.logger).to receive(:warn).with("Redis tracking event failed for event: #{unknown_event}, message: Unknown event #{unknown_event}")
subject.increment_unique_values(unknown_event, value)
end
end
context 'with feature disabled' do
before do
stub_feature_flags(feature => false)
end
it "logs an exception" do
expect(Rails.logger).to receive(:warn).with("Redis tracking event failed for event: #{event_name}, message: Feature #{feature} not enabled")
subject.increment_unique_values(event_name, value)
end
end
end
describe '#order_options_with_tie_breaker' do describe '#order_options_with_tie_breaker' do
subject { Class.new.include(described_class).new.order_options_with_tie_breaker } subject { Class.new.include(described_class).new.order_options_with_tie_breaker }
......
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