Skip to content

Commit

Permalink
feat(clickhouse): new column for clickhouse events store on org
Browse files Browse the repository at this point in the history
  • Loading branch information
jdenquin committed Oct 16, 2024
1 parent 08d76e4 commit 81b4363
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def validate_email_settings
# api_key :string
# city :string
# clickhouse_aggregation :boolean default(FALSE), not null
# clickhouse_events_store :boolean default(FALSE), not null
# country :string
# custom_aggregation :boolean default(FALSE)
# default_currency :string default("USD"), not null
Expand Down
1 change: 1 addition & 0 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def should_sync_payment?
# amount_cents :bigint not null
# amount_currency :string not null
# payable_type :string default("Invoice"), not null
# provider_payment_data :jsonb
# status :string not null
# created_at :datetime not null
# updated_at :datetime not null
Expand Down
2 changes: 1 addition & 1 deletion app/services/billable_metrics/aggregation_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def supports_clickhouse?
def self.new_instance(charge:, current_usage: false, **attributes)
event_store = Events::Stores::PostgresStore

if !charge.pay_in_advance? && supports_clickhouse? && charge.billable_metric.organization.clickhouse_aggregation?
if !charge.pay_in_advance? && supports_clickhouse? && charge.billable_metric.organization.clickhouse_events_store?
event_store = Events::Stores::ClickhouseStore
end

Expand Down
4 changes: 2 additions & 2 deletions app/services/events/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def call
event.timestamp = Time.zone.at(params[:timestamp] ? params[:timestamp].to_f : timestamp)
event.precise_total_amount_cents = params[:precise_total_amount_cents]

event.save! unless organization.clickhouse_aggregation?
event.save! unless organization.clickhouse_events_store?

result.event = event

produce_kafka_event(event)
Events::PostProcessJob.perform_later(event:) unless organization.clickhouse_aggregation?
Events::PostProcessJob.perform_later(event:) unless organization.clickhouse_events_store?

result
rescue ActiveRecord::RecordInvalid => e
Expand Down
4 changes: 2 additions & 2 deletions app/services/events/pay_in_advance_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def call
# NOTE: Temporary condition to support both Postgres and Clickhouse (via kafka)
if kafka_producer_enabled?
# NOTE: when clickhouse, ignore event coming from postgres (Rest API)
return result if event.id.present? && event.organization.clickhouse_aggregation?
return result if event.id.present? && event.organization.clickhouse_events_store?

# NOTE: without clickhouse, ignore events coming from kafka
return result if event.id.nil? && !event.organization.clickhouse_aggregation?
return result if event.id.nil? && !event.organization.clickhouse_events_store?
end

charges.where(invoiceable: false).find_each do |charge|
Expand Down
2 changes: 1 addition & 1 deletion app/services/invoices/customer_usage_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def add_charge_fees
end

def charge_usage(charge)
return charge_usage_without_cache(charge) if organization.clickhouse_aggregation?
return charge_usage_without_cache(charge) if organization.clickhouse_events_store?

json = Rails.cache.fetch(charge_cache_key(charge), expires_in: charge_cache_expiration) do
fees_result = Fees::ChargeService.new(
Expand Down
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/services/events/pay_in_advance_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
end

context 'when organization is using clickhouse' do
before { organization.update!(clickhouse_aggregation: true) }
before { organization.update!(clickhouse_events_store: true) }

it 'enqueues a job to perform the pay_in_advance aggregation' do
expect { in_advance_service.call }.to have_enqueued_job(Fees::CreatePayInAdvanceJob)
Expand Down

0 comments on commit 81b4363

Please sign in to comment.