Skip to content

Commit

Permalink
fix(events): Fix clickhouse events id (#2747)
Browse files Browse the repository at this point in the history
- add `subscription_id`  and `customer_id` for events on CH
- add a default `lago_id`
  • Loading branch information
jdenquin authored Oct 28, 2024
1 parent cfdd0ee commit cc2fa59
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def batch
end

def show
event = Event.find_by(
event_scope = current_organization.clickhouse_events_store? ? Clickhouse::EventsRaw : Event
event = event_scope.find_by(
organization: current_organization,
transaction_id: params[:id]
)
Expand Down
3 changes: 2 additions & 1 deletion app/graphql/resolvers/events_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class EventsResolver < Resolvers::BaseResolver
def resolve(page: nil, limit: nil)
if current_organization.clickhouse_events_store?
Clickhouse::EventsRaw.where(organization_id: current_organization.id)
.order(ingested_at: :desc)
.page(page)
.per((limit >= MAX_LIMIT) ? MAX_LIMIT : limit)
else
Event.where(organization_id: current_organization.id)
.order(timestamp: :desc)
.order(created_at: :desc)
.page(page)
.per((limit >= MAX_LIMIT) ? MAX_LIMIT : limit)
end
Expand Down
1 change: 0 additions & 1 deletion app/graphql/types/events/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def payload
{
event: {
transaction_id: object.transaction_id,
external_customer_id: object.external_customer_id,
external_subscription_id: object.external_subscription_id,
code: object.code,
timestamp: object.timestamp.to_i,
Expand Down
11 changes: 10 additions & 1 deletion app/models/clickhouse/events_raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
module Clickhouse
class EventsRaw < BaseRecord
self.table_name = 'events_raw'
self.primary_key = nil

def id
"#{organization_id}-#{external_subscription_id}-#{transaction_id}-#{ingested_at.to_i}"
end

def created_at
ingested_at
Expand All @@ -27,13 +32,17 @@ def subscription
.first
end

def subscription_id
subscription&.id
end

def organization
Organization.find_by(id: organization_id)
end

private

delegate :customer, to: :subscription, allow_nil: true
delegate :customer, :customer_id, to: :subscription, allow_nil: true
end
end

Expand Down
1 change: 1 addition & 0 deletions app/queries/events_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def call
events = paginate(events)

events = events.order(created_at: :desc) unless organization.clickhouse_events_store?
events = events.order(ingested_at: :desc) if organization.clickhouse_events_store?

events = with_code(events) if filters.code
events = with_external_subscription_id(events) if filters.external_subscription_id
Expand Down

0 comments on commit cc2fa59

Please sign in to comment.