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

feat(events): Support clickhouse store for events listing #2744

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions app/graphql/resolvers/events_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class EventsResolver < Resolvers::BaseResolver
include AuthenticableApiUser
include RequiredOrganization

MAX_LIMIT = 1000

description 'Query events of an organization'

argument :limit, Integer, required: false
Expand All @@ -13,10 +15,16 @@ class EventsResolver < Resolvers::BaseResolver
type Types::Events::Object.collection_type, null: true

def resolve(page: nil, limit: nil)
Event.where(organization_id: current_organization.id)
.order(timestamp: :desc)
.page(page)
.per(limit)
if current_organization.clickhouse_events_store?
Clickhouse::EventsRaw.where(organization_id: current_organization.id)
.page(page)
.per((limit >= MAX_LIMIT) ? MAX_LIMIT : limit)
else
Event.where(organization_id: current_organization.id)
.order(timestamp: :desc)
.page(page)
.per((limit >= MAX_LIMIT) ? MAX_LIMIT : limit)
end
end
end
end
11 changes: 5 additions & 6 deletions app/graphql/types/events/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ class Object < Types::BaseObject
field :code, String, null: false
field :id, ID, null: false

field :external_customer_id, String, null: true
field :external_subscription_id, String, null: true
field :transaction_id, String, null: true

field :customer_timezone, Types::TimezoneEnum, null: false
field :deleted_at, GraphQL::Types::ISO8601DateTime, null: true
field :received_at, GraphQL::Types::ISO8601DateTime, null: false, method: :created_at
field :received_at, GraphQL::Types::ISO8601DateTime, null: true, method: :created_at
field :timestamp, GraphQL::Types::ISO8601DateTime, null: true

field :api_client, String, null: true
Expand All @@ -23,10 +22,10 @@ class Object < Types::BaseObject
field :billable_metric_name, String, null: true
field :payload, GraphQL::Types::JSON, null: false

field :match_billable_metric, Boolean, null: false
field :match_custom_field, Boolean, null: false
field :match_customer, Boolean, null: false
field :match_subscription, Boolean, null: false
field :match_billable_metric, Boolean, null: true
field :match_custom_field, Boolean, null: true
field :match_customer, Boolean, null: true
field :match_subscription, Boolean, null: true

def payload
{
Expand Down
30 changes: 30 additions & 0 deletions app/models/clickhouse/events_raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
module Clickhouse
class EventsRaw < BaseRecord
self.table_name = 'events_raw'

def created_at
ingested_at
end

def billable_metric
BillableMetric.find_by(code:, organization_id:)
end

def api_client
end

def ip_address
end

def subscription
organization.subscriptions.find_by(external_id: external_subscription_id)
jdenquin marked this conversation as resolved.
Show resolved Hide resolved
end

def customer_timezone
end
jdenquin marked this conversation as resolved.
Show resolved Hide resolved

def organization
Organization.find_by(id: organization_id)
end

private

delegate :customer, to: :subscription
jdenquin marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand All @@ -11,6 +40,7 @@ class EventsRaw < BaseRecord
# Table name: events_raw
#
# code :string not null, primary key
# ingested_at :datetime not null
# precise_total_amount_cents :decimal(40, 15)
# properties :string not null
# timestamp :datetime not null, primary key
Expand Down
6 changes: 4 additions & 2 deletions app/queries/events_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

class EventsQuery < BaseQuery
def call
events = organization.events
events = organization.clickhouse_events_store? ? Clickhouse::EventsRaw : Event
events = events.where(organization_id: organization.id)
events = paginate(events)
events = events.order(created_at: :desc)

events = events.order(created_at: :desc) unless organization.clickouse_events_store?

events = with_code(events) if filters.code
events = with_external_subscription_id(events) if filters.external_subscription_id
Expand Down
1 change: 1 addition & 0 deletions db/clickhouse_migrate/20231024084411_create_events_raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def change
t.string :code, null: false
t.string :properties, map: true, null: false
t.decimal :precise_total_amount_cents, precision: 40, scale: 15
t.datetime :ingested_at, null: false, precision: 3
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def change
t.string :code, null: false
t.string :properties, null: false
t.decimal :precise_total_amount_cents, precision: 40, scale: 15
t.datetime :ingested_at, null: false, precision: 3
end
end
end
3 changes: 2 additions & 1 deletion db/clickhouse_migrate/20231030163703_create_events_raw_mv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def change
toDateTime64(timestamp, 3) as timestamp,
code,
JSONExtract(properties, 'Map(String, String)') as properties,
precise_total_amount_cents
precise_total_amount_cents,
ingested_at
FROM events_raw_queue
SQL

Expand Down
11 changes: 5 additions & 6 deletions schema.graphql

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

64 changes: 15 additions & 49 deletions schema.json

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

Loading