Commit 43575f71 authored by alinamihaila's avatar alinamihaila

Rename totals to weekly monthly

Add eligible_for_totals? check
parent 15295698
......@@ -56,7 +56,7 @@ module Gitlab
end
def categories
known_events.map { |event| event[:category] }.uniq
@categories ||= known_events.map { |event| event[:category] }.uniq
end
# @param category [String] the category name
......@@ -67,14 +67,16 @@ module Gitlab
def unique_events_data
categories.each_with_object({}) do |category, category_results|
events = events_for_category(category)
events_names = events_for_category(category)
event_results = events.each_with_object({}) do |event, hash|
event_results = events_names.each_with_object({}) do |event, hash|
hash[event] = unique_events(event_names: event, start_date: 7.days.ago.to_date, end_date: Date.current)
end
event_results["#{category}_total_unique_counts_for_week"] = unique_events(event_names: events, start_date: 7.days.ago.to_date, end_date: Date.current)
event_results["#{category}_total_unique_counts_for_month"] = unique_events(event_names: events, start_date: 4.weeks.ago.to_date, end_date: Date.current)
if eligible_for_totals?(events_names)
event_results["#{category}_total_unique_counts_weekly"] = unique_events(event_names: events_names, start_date: 7.days.ago.to_date, end_date: Date.current)
event_results["#{category}_total_unique_counts_monthly"] = unique_events(event_names: events_names, start_date: 4.weeks.ago.to_date, end_date: Date.current)
end
category_results["#{category}"] = event_results
end
......@@ -82,6 +84,15 @@ module Gitlab
private
# Allow to add totals for events that are in the same redis slot, category and have the same aggregation level
# and if there are more than 1 event
def eligible_for_totals?(events_names)
return false if events_names.size <= 1
events = events_for(events_names)
events_in_same_slot?(events) && events_in_same_category?(events) && events_same_aggregation?(events)
end
def keys_for_aggregation(aggregation, events:, start_date:, end_date:)
if aggregation.to_sym == :daily
daily_redis_keys(events: events, start_date: start_date, end_date: end_date)
......@@ -99,8 +110,11 @@ module Gitlab
end
def events_in_same_slot?(events)
# if we check one event then redis_slot is only one to check
return true if events.size == 1
slot = events.first[:redis_slot]
events.all? { |event| event[:redis_slot] == slot }
events.all? { |event| event[:redis_slot].present? && event[:redis_slot] == slot }
end
def events_in_same_category?(events)
......
......@@ -54,6 +54,12 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
end
end
describe '.categories' do
it 'gets all unique category names' do
expect(described_class.categories).to contain_exactly(global_category, analytics_category, productivity_category, compliance_category)
end
end
describe '.track_event' do
it "raise error if metrics don't have same aggregation" do
expect { described_class.track_event(entity1, different_aggregation, Date.current) } .to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownAggregation)
......@@ -189,41 +195,44 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
end
end
describe '.categories' do
it 'gets all unique category names' do
expect(described_class.categories).to contain_exactly(global_category, analytics_category, productivity_category, compliance_category)
end
end
describe 'unique_events_data' do
let(:categories) { described_class.categories }
before do
categories.each do |category|
events = described_class.events_for_category(category)
events.each do |event|
allow(described_class).to receive(:unique_events).with(event_names: event, start_date: 7.days.ago.to_date, end_date: Date.current).and_return(123)
end
allow(described_class).to receive(:unique_events).with(event_names: events, start_date: 7.days.ago.to_date, end_date: Date.current).and_return(543)
allow(described_class).to receive(:unique_events).with(event_names: events, start_date: 4.weeks.ago.to_date, end_date: Date.current).and_return(987)
end
let(:known_events) do
[
{ name: 'event1_slot', redis_slot: "slot", category: 'category1', aggregation: "weekly" },
{ name: 'event2_slot', redis_slot: "slot", category: 'category1', aggregation: "weekly" },
{ name: 'event3', category: 'category2', aggregation: "weekly" },
{ name: 'event4', category: 'category2', aggregation: "weekly" }
].map(&:with_indifferent_access)
end
it 'returns the number of unique events' do
results = categories.each_with_object({}) do |category, category_results|
events = described_class.events_for_category(category)
event_results = events.each_with_object({}) do |event, hash|
hash[event] = 123
end
event_results["#{category}_total_unique_counts_for_week"] = 543
event_results["#{category}_total_unique_counts_for_month"] = 987
category_results["#{category}"] = event_results
end
before do
allow(described_class).to receive(:known_events).and_return(known_events)
allow(described_class).to receive(:categories).and_return(%w(category1 category2))
described_class.track_event(entity1, 'event1_slot', 2.days.ago)
described_class.track_event(entity2, 'event2_slot', 2.days.ago)
described_class.track_event(entity3, 'event2_slot', 2.weeks.ago)
# events in different slots
described_class.track_event(entity2, 'event3', 2.days.ago)
described_class.track_event(entity2, 'event4', 2.days.ago)
end
it 'returns the number of unique events for all known events' do
results = {
'category1' => {
'event1_slot' => 1,
'event2_slot' => 1,
'category1_total_unique_counts_weekly' => 2,
'category1_total_unique_counts_monthly' => 3
},
'category2' => {
'event3' => 1,
'event4' => 1
}
}
expect(subject.unique_events_data).to eq(results)
end
......
......@@ -1151,7 +1151,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
expect(subject[:redis_hll_counters].keys).to match_array(categories)
categories.each do |category|
keys = ::Gitlab::UsageDataCounters::HLLRedisCounter.events_for_category(category) + ["#{category}_total_unique_counts_for_week", "#{category}_total_unique_counts_for_month"]
keys = ::Gitlab::UsageDataCounters::HLLRedisCounter.events_for_category(category) + ["#{category}_total_unique_counts_weekly", "#{category}_total_unique_counts_monthly"]
expect(subject[:redis_hll_counters][category].keys).to match_array(keys)
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