-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event): Add high usage enpoint to API
- Loading branch information
1 parent
815a361
commit f4dd4e0
Showing
10 changed files
with
730 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
module Events | ||
class HighUsageBatchCreateService < BaseService | ||
Result = BaseResult[:transactions] | ||
|
||
MAX_LENGTH = ENV.fetch("LAGO_EVENTS_BATCH_MAX_LENGTH", 100).to_i | ||
|
||
def initialize(organization:, params:, timestamp:) | ||
@organization = organization | ||
@params = params | ||
@timestamp = timestamp | ||
super | ||
end | ||
|
||
def call | ||
return result.not_allowed_failure!(code: "missing_configuration") if ENV["LAGO_KAFKA_BOOTSTRAP_SERVERS"].blank? | ||
return result.not_allowed_failure!(code: "missing_configuration") if ENV["LAGO_KAFKA_RAW_EVENTS_TOPIC"].blank? | ||
|
||
if params.blank? | ||
return result.single_validation_failure!(error_code: "no_events", field: :events) | ||
end | ||
|
||
if params.count > MAX_LENGTH | ||
return result.single_validation_failure!(error_code: "too_many_events", field: :events) | ||
end | ||
|
||
result.transactions = params.map { process_event(_1) } | ||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :organization, :params, :timestamp | ||
|
||
def process_event(event_params) | ||
Karafka.producer.produce_async( | ||
topic: ENV["LAGO_KAFKA_RAW_EVENTS_TOPIC"], | ||
key: "#{organization.id}-#{event_params[:external_subscription_id]}", | ||
payload: { | ||
organization_id: organization.id, | ||
external_subscription_id: event_params[:external_subscription_id], | ||
transaction_id: event_params[:transaction_id], | ||
timestamp: parsed_timestamp(event_params[:timestamp]), | ||
code: event_params[:code], | ||
# NOTE: Default value to 0.0 is required for clickhouse parsing | ||
precise_total_amount_cents: precise_total_amount_cents(event_params[:precise_total_amount_cents]), | ||
properties: event_params[:properties] || {}, | ||
# NOTE: Removes trailing 'Z' to allow clickhouse parsing | ||
ingested_at: Time.current.iso8601[...-1], | ||
source: "http_ruby_high_usage" | ||
}.to_json | ||
) | ||
|
||
{transaction_id: event_params[:transaction_id]} | ||
end | ||
|
||
def precise_total_amount_cents(precise_total_amount_cents) | ||
BigDecimal(precise_total_amount_cents.presence || "0.0").to_s | ||
rescue ArgumentError | ||
"0.0" | ||
end | ||
|
||
def parsed_timestamp(event_timestamp) | ||
Time.zone.at(event_timestamp ? Float(event_timestamp) : timestamp).to_i | ||
rescue ArgumentError | ||
timestamp.to_i | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module Events | ||
class HighUsageCreateService < BaseService | ||
Result = BaseResult[:transaction_id] | ||
|
||
def initialize(organization:, params:, timestamp:) | ||
@organization = organization | ||
@params = params | ||
@timestamp = timestamp | ||
super | ||
end | ||
|
||
def call | ||
return result.not_allowed_failure!(code: "missing_configuration") if ENV["LAGO_KAFKA_BOOTSTRAP_SERVERS"].blank? | ||
return result.not_allowed_failure!(code: "missing_configuration") if ENV["LAGO_KAFKA_RAW_EVENTS_TOPIC"].blank? | ||
|
||
Karafka.producer.produce_async( | ||
topic: ENV["LAGO_KAFKA_RAW_EVENTS_TOPIC"], | ||
key: "#{organization.id}-#{params[:external_subscription_id]}", | ||
payload: { | ||
organization_id: organization.id, | ||
external_subscription_id: params[:external_subscription_id], | ||
transaction_id: params[:transaction_id], | ||
timestamp: params[:timestamp].presence || timestamp.to_i, | ||
code: params[:code], | ||
# NOTE: Default value to 0.0 is required for clickhouse parsing | ||
precise_total_amount_cents:, | ||
properties: params[:properties] || {}, | ||
# NOTE: Removes trailing 'Z' to allow clickhouse parsing | ||
ingested_at: Time.current.iso8601[...-1], | ||
source: "http_ruby_high_usage" | ||
}.to_json | ||
) | ||
|
||
result.transaction_id = params[:transaction_id] | ||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :organization, :params, :timestamp | ||
|
||
def precise_total_amount_cents | ||
BigDecimal(params[:precise_total_amount_cents].presence || "0.0").to_s | ||
rescue ArgumentError | ||
"0.0" | ||
end | ||
end | ||
end |
Oops, something went wrong.