Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(events): Fix clickhouse events id #2747

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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