diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index 0df4bca336b..a31238dce57 100644 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -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] ) diff --git a/app/graphql/resolvers/events_resolver.rb b/app/graphql/resolvers/events_resolver.rb index 80d74220885..ded82e9be84 100644 --- a/app/graphql/resolvers/events_resolver.rb +++ b/app/graphql/resolvers/events_resolver.rb @@ -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 diff --git a/app/graphql/types/events/object.rb b/app/graphql/types/events/object.rb index 4e492c7c7a2..3d51e2c7e5f 100644 --- a/app/graphql/types/events/object.rb +++ b/app/graphql/types/events/object.rb @@ -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, diff --git a/app/models/clickhouse/events_raw.rb b/app/models/clickhouse/events_raw.rb index 7ee1f070d99..ca229a240cd 100644 --- a/app/models/clickhouse/events_raw.rb +++ b/app/models/clickhouse/events_raw.rb @@ -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 @@ -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 diff --git a/app/queries/events_query.rb b/app/queries/events_query.rb index cd0631fc543..20c0d0806f0 100644 --- a/app/queries/events_query.rb +++ b/app/queries/events_query.rb @@ -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