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(group-by): handle group for in pay in advance charges #1620

Merged
merged 2 commits into from
Jan 23, 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
2 changes: 1 addition & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ def recurring_breakdown(fee)
event_store_class: Events::Stores::PostgresStore,
charge: fee.charge,
subscription: fee.subscription,
group: fee.group,
boundaries: {
from_datetime: DateTime.parse(fee.properties['charges_from_datetime']),
to_datetime: DateTime.parse(fee.properties['charges_to_datetime']),
},
filters: { group: fee.group },
).breakdown.breakdown
end

Expand Down
11 changes: 7 additions & 4 deletions app/services/billable_metrics/aggregations/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
module BillableMetrics
module Aggregations
class BaseService < ::BaseService
def initialize(event_store_class:, charge:, subscription:, boundaries:, group: nil, event: nil) # rubocop:disable Metrics/ParameterLists
def initialize(event_store_class:, charge:, subscription:, boundaries:, filters: {})
super(nil)
@event_store_class = event_store_class
@charge = charge
@subscription = subscription
@group = group
@event = event

@filters = filters
@group = filters[:group]
@event = filters[:event]

@boundaries = boundaries

result.aggregator = self
Expand All @@ -27,7 +30,7 @@ def per_event_aggregation

protected

attr_accessor :event_store_class, :charge, :subscription, :group, :event, :boundaries
attr_accessor :event_store_class, :charge, :subscription, :filters, :group, :event, :boundaries

delegate :billable_metric, to: :charge

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def base_scope
from_datetime: subscription.started_at,
to_datetime: subscription.terminated_at,
},
filters: { group: },
filters:,
)
store.aggregation_property = billable_metric.field_name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def latest_value_from_events
code: billable_metric.code,
subscription:,
boundaries: { to_datetime: from_datetime },
filters: { group: },
filters:,
)

event_store.use_from_boundary = false
Expand Down
2 changes: 1 addition & 1 deletion app/services/billable_metrics/breakdown/sum_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def persisted_breakdown
code: billable_metric.code,
subscription:,
boundaries: { to_datetime: from_datetime },
filters: { group: },
filters:,
)

event_store.use_from_boundary = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def persisted_sum
code: billable_metric.code,
subscription:,
boundaries: { to_datetime: from_datetime },
filters: { group: },
filters:,
)

event_store.use_from_boundary = false
Expand All @@ -92,7 +92,7 @@ def recurring_value
code: billable_metric.code,
subscription:,
boundaries: { to_datetime: from_datetime },
filters: { group: },
filters:,
)

event_store.use_from_boundary = false
Expand Down
18 changes: 16 additions & 2 deletions app/services/charges/pay_in_advance_aggregation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ def call
aggregator = BillableMetrics::AggregationFactory.new_instance(
charge:,
subscription:,
group:,
event:,
boundaries: {
from_datetime: boundaries[:charges_from_datetime],
to_datetime: boundaries[:charges_to_datetime],
},
filters: aggregation_filters,
)

aggregator.aggregate(options: aggregation_options)
Expand All @@ -40,5 +39,20 @@ def aggregation_options
free_units_per_total_aggregation: BigDecimal(properties['free_units_per_total_aggregation'] || 0),
}
end

def aggregation_filters
filters = {
group:,
event:,
}

if charge.standard? && charge.properties['grouped_by'].present?
filters[:grouped_by_values] = charge.properties['grouped_by'].index_with do |grouped_by|
event.properties[grouped_by]
end
end

filters
end
end
end
8 changes: 4 additions & 4 deletions app/services/events/stores/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def initialize(code:, subscription:, boundaries:, filters: {})

@group = filters[:group]
@grouped_by = filters[:grouped_by]
@grouped_by_value = filters[:grouped_by_value]
@grouped_by_values = filters[:grouped_by_values]

@aggregation_property = nil
@numeric_property = false
@use_from_boundary = true
end

def grouped_by_value?
grouped_by.present? && grouped_by_value.present?
def grouped_by_values?
grouped_by.present? && grouped_by_values.present?
end

def events(force_from: false)
Expand Down Expand Up @@ -78,7 +78,7 @@ def charges_duration

protected

attr_accessor :code, :subscription, :group, :boundaries, :grouped_by, :grouped_by_value
attr_accessor :code, :subscription, :group, :boundaries, :grouped_by, :grouped_by_values

delegate :customer, to: :subscription

Expand Down
10 changes: 7 additions & 3 deletions app/services/events/stores/clickhouse_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def events(force_from: false)
scope = scope.where('events_raw.timestamp <= ?', to_datetime) if to_datetime
scope = scope.where(numeric_condition) if numeric_property

scope = with_grouped_by_value(scope) if grouped_by_value?
scope = with_grouped_by_values(scope) if grouped_by_values?

return scope unless group

Expand Down Expand Up @@ -178,8 +178,12 @@ def group_scope(scope)
scope.where('events_raw.properties[?] = ?', group.parent.key.to_s => group.parent.value.to_s)
end

def with_grouped_by_value(scope)
scope.where('events_raw.properties[?] = ?', grouped_by.to_s, grouped_by_value.to_s)
def with_grouped_by_values(scope)
grouped_by_values.each do |grouped_by, grouped_by_value|
scope = scope.where('events_raw.properties[?] = ?', grouped_by, grouped_by_value)
end

scope
end

def numeric_condition
Expand Down
10 changes: 7 additions & 3 deletions app/services/events/stores/postgres_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def events(force_from: false)
.where(numeric_condition)
end

scope = with_grouped_by_value(scope) if grouped_by_value?
scope = with_grouped_by_values(scope) if grouped_by_values?

return scope unless group

Expand Down Expand Up @@ -128,8 +128,12 @@ def group_scope(scope)
scope.where('events.properties @> ?', { group.parent.key.to_s => group.parent.value }.to_json)
end

def with_grouped_by_value(scope)
scope.where('events.properties @> ?', { grouped_by.to_s => grouped_by_value.to_s }.to_json)
def with_grouped_by_values(scope)
grouped_by_values.each do |grouped_by, grouped_by_value|
scope = scope.where('events.properties @> ?', { grouped_by.to_s => grouped_by_value.to_s }.to_json)
end

scope
end

def sanitized_propery_name
Expand Down
2 changes: 1 addition & 1 deletion app/services/fees/charge_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ def aggregator(group:)
charge:,
current_usage: is_current_usage,
subscription:,
group:,
boundaries: {
from_datetime: boundaries.charges_from_datetime,
to_datetime: boundaries.charges_to_datetime,
charges_duration: boundaries.charges_duration,
},
filters: { group: },
)
end

Expand Down
2 changes: 2 additions & 0 deletions app/services/fees/create_pay_in_advance_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def create_fee(properties:, group: nil)
taxes_amount_cents: 0,
unit_amount_cents:,
precise_unit_amount: result.unit_amount,
grouped_by: (charge.properties['grouped_by'] || []).map { event.properties[_1] },
)

taxes_result = Fees::ApplyTaxesService.call(fee:)
Expand Down Expand Up @@ -168,6 +169,7 @@ def cache_aggregation_result(aggregation_result:, group:)
current_aggregation: aggregation_result.current_aggregation,
max_aggregation: aggregation_result.max_aggregation,
max_aggregation_with_proration: aggregation_result.max_aggregation_with_proration,
grouped_by: (charge.properties['grouped_by'] || []).map { event.properties[_1] },
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
event_store_class:,
charge:,
subscription:,
group:,
event: pay_in_advance_event,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
event: pay_in_advance_event,
},
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
event_store_class:,
charge:,
subscription:,
group:,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: { group: },
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
event_store_class:,
charge:,
subscription:,
group:,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
},
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
event_store_class:,
charge:,
subscription:,
group:,
event: pay_in_advance_event,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
event: pay_in_advance_event,
},
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
event_store_class:,
charge:,
subscription:,
group:,
event: pay_in_advance_event,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
event: pay_in_advance_event,
},
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
event_store_class:,
charge:,
subscription:,
group:,
boundaries: {
from_datetime:,
to_datetime:,
charges_duration:,
},
filters: { group: },
)
end

Expand Down
4 changes: 3 additions & 1 deletion spec/services/billable_metrics/breakdown/sum_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
event_store_class:,
charge:,
subscription:,
group:,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
},
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
event_store_class:,
charge:,
subscription:,
group:,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
},
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
event_store_class:,
charge:,
subscription:,
group:,
event: pay_in_advance_event,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
event: pay_in_advance_event,
},
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
event_store_class:,
charge:,
subscription:,
group:,
event: pay_in_advance_event,
boundaries: {
from_datetime:,
to_datetime:,
},
filters: {
group:,
event: pay_in_advance_event,
},
)
end

Expand Down
Loading
Loading