Skip to content

Commit

Permalink
feat(group-by): add grouped_by_value logic to event stores
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Jan 19, 2024
1 parent 6198018 commit c0ff257
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/services/events/stores/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def initialize(code:, subscription:, boundaries:, filters: {})
@use_from_boundary = true
end

def grouped_by_value?
grouped_by.present? && grouped_by_value.present?
end

def events(force_from: false)
raise NotImplementedError
end
Expand Down
6 changes: 6 additions & 0 deletions app/services/events/stores/clickhouse_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def events(force_from: false)
scope = scope.where('events_raw.timestamp <= ?', to_datetime) if to_datetime
scope = scope.where(numeric_condition) if numeric_property

scope = with_grouped_by_value(scope) if grouped_by_value?

return scope unless group

group_scope(scope)
Expand Down Expand Up @@ -176,6 +178,10 @@ def group_scope(scope)
scope.where('events_raw.properties[?] = ?', group.parent.key.to_s => group.parent.value.to_s)
end

def with_grouped_by_value(scope)
scope.where('events_raw.properties[?] = ?', grouped_by.to_s, grouped_by_value.to_s)
end

def numeric_condition
ActiveRecord::Base.sanitize_sql_for_conditions(
[
Expand Down
6 changes: 6 additions & 0 deletions app/services/events/stores/postgres_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def events(force_from: false)
.where(numeric_condition)
end

scope = with_grouped_by_value(scope) if grouped_by_value?

return scope unless group

group_scope(scope)
Expand Down Expand Up @@ -126,6 +128,10 @@ def group_scope(scope)
scope.where('events.properties @> ?', { group.parent.key.to_s => group.parent.value }.to_json)
end

def with_grouped_by_value(scope)
scope.where('events.properties @> ?', { grouped_by.to_s => grouped_by_value.to_s }.to_json)
end

def sanitized_propery_name
ActiveRecord::Base.sanitize_sql_for_conditions(
['events.properties->>?', aggregation_property],
Expand Down
19 changes: 17 additions & 2 deletions spec/services/events/stores/clickhouse_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
code:,
subscription:,
boundaries:,
filters: { group: },
filters: { group:, grouped_by:, grouped_by_value: },
)
end

Expand All @@ -30,13 +30,19 @@
end

let(:group) { nil }
let(:grouped_by) { nil }
let(:grouped_by_value) { nil }

let(:events) do
events = []

5.times do |i|
properties = { billable_metric.field_name => i + 1 }
properties[group.key.to_s] = group.value.to_s if group && i.even?

if i.even?
properties[group.key.to_s] = group.value.to_s if group
properties[grouped_by] = grouped_by_value if grouped_by_value
end

events << Clickhouse::EventsRaw.create!(
transaction_id: SecureRandom.uuid,
Expand Down Expand Up @@ -80,6 +86,15 @@
expect(event_store.events.count).to eq(3)
end
end

context 'with grouped_by_value' do
let(:grouped_by) { 'region' }
let(:grouped_by_value) { 'europe' }

it 'returns a list of events' do
expect(event_store.events.count).to eq(3)
end
end
end

describe '.count' do
Expand Down
23 changes: 18 additions & 5 deletions spec/services/events/stores/postgres_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
code:,
subscription:,
boundaries:,
filters: { group: },
filters: { group:, grouped_by:, grouped_by_value: },
)
end

Expand All @@ -30,12 +30,14 @@
end

let(:group) { nil }
let(:grouped_by) { nil }
let(:grouped_by_value) { nil }

let(:events) do
events = []

5.times do |i|
event = create(
event = build(
:event,
organization_id: organization.id,
external_subscription_id: subscription.external_id,
Expand All @@ -47,11 +49,13 @@
},
)

if group && i.even?
event.properties[group.key] = group.value
event.save!
if i.even?
event.properties[group.key] = group.value if group
event.properties[grouped_by] = grouped_by_value if grouped_by_value
end

event.save!

events << event
end

Expand All @@ -72,6 +76,15 @@
expect(event_store.events.count).to eq(3)
end
end

context 'with grouped_by_value' do
let(:grouped_by) { 'region' }
let(:grouped_by_value) { 'europe' }

it 'returns a list of events' do
expect(event_store.events.count).to eq(3)
end
end
end

describe '.count' do
Expand Down

0 comments on commit c0ff257

Please sign in to comment.