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(custom_agg): Improve scenarios to match real use cases #2015

Merged
merged 1 commit into from
May 21, 2024
Merged
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
46 changes: 24 additions & 22 deletions spec/scenarios/billable_metrics/custom_aggregation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,34 @@ def aggregate(event, previous_state, aggregation_properties)
ranges = aggregation_properties['ranges']

result_amount = ranges.reduce(0) do |amount, range|
to = range['to']
to = BigDecimal(to.to_s) if to

# Range was already reached
next amount if range['to'] && previous_units > range['to']
next amount if to && previous_units > to

from = BigDecimal(range['from'].to_s)
certif_amount = BigDecimal(range[certif] ? range[certif].to_s : '0')

if !range['to'] || total_units <= range['to']
if !to || total_units <= to
# Last matching range is reached
units_to_use = if previous_units >= range['from']
units_to_use = if previous_units >= from
# All new units are in the current range
event_units
else
# Takes only the new units in the current range
total_units - range['from'] + 1
total_units - from + 1
end
break amount += certif_amount * units_to_use

else
# Range is not the last one
units_to_use = if previous_units >= range['from']
units_to_use = if previous_units >= from
# All remaining units in the range
range['to'] - previous_units
to - previous_units
else
# All units in the range
range['to'] - range['from'] + 1
to - from + 1
end

amount += certif_amount * units_to_use
Expand Down Expand Up @@ -594,23 +598,20 @@ def aggregate(event, previous_state, aggregation_properties)
amount_property = aggregation_properties['amount']
rate_property = aggregation_properties['rate']
min_amount_property = aggregation_properties['min_amount']

fx_rate = BigDecimal(aggregation_properties['fx_rate'] ? aggregation_properties['fx_rate'].to_s : '1')
event_value = BigDecimal(event.properties['value'].to_s)

total_units = previous_units + 1
result_amount = BigDecimal('0')

if ranges_property != nil
# The aggregation uses a range logic
range = ranges_property.find { |r| r['from'] <= total_units && (r['to'].nil? || total_units < r['to']) }

amount = range['amount']
rate = range['rate']
range = ranges_property.find { |r| BigDecimal(r['from'].to_s) <= total_units && (r['to'].nil? || total_units <= BigDecimal(r['to'].to_s)) }

if amount != nil
result_amount += BigDecimal(amount.to_s)
if range['amount'] != nil
result_amount += BigDecimal(range['amount'].to_s)
else
result_amount += event_value * BigDecimal(rate.to_s) / 100
result_amount += (event_value * BigDecimal(range['rate'].to_s) / 100) * fx_rate
end
elsif amount_property != nil
# The aggregation uses an amount logic
Expand Down Expand Up @@ -691,9 +692,10 @@ def aggregate(event, previous_state, aggregation_properties)
{from: 10_001, to: 15_000, rate: '0.3'},
{from: 15_001, to: 22_000, rate: '0.25'},
{from: 22_001, to: nil, rate: '0.2'},
]
],
fx_rate: 0.88
}
},
}
)
end

Expand Down Expand Up @@ -1086,7 +1088,7 @@ def aggregate(event, previous_state, aggregation_properties)
expect(CachedAggregation.where(organization_id: organization.id).count).to eq(8)

fee = subscription.fees.where(charge:).order(created_at: :desc).first
expect(fee.amount_cents).to eq(40_000)
expect(fee.amount_cents).to eq(35_200)
expect(fee.events_count).to eq(1)
expect(fee.units).to eq(1)
end
Expand Down Expand Up @@ -1125,7 +1127,7 @@ def aggregate(event, previous_state, aggregation_properties)
expect(CachedAggregation.where(organization_id: organization.id).count).to eq(10)

fee = subscription.fees.where(charge:).order(created_at: :desc).first
expect(fee.amount_cents).to eq(300)
expect(fee.amount_cents).to eq(264)
expect(fee.events_count).to eq(1)
expect(fee.units).to eq(1)
end
Expand All @@ -1134,13 +1136,13 @@ def aggregate(event, previous_state, aggregation_properties)
travel_to(DateTime.new(2024, 2, 6, 10)) do
fetch_current_usage(customer:)

expect(json[:customer_usage][:total_amount_cents]).to eq(65_620) # TODO
expect(json[:customer_usage][:total_amount_cents]).to eq(60_772)
expect(json[:customer_usage][:charges_usage].count).to eq(1)

charge_usage = json[:customer_usage][:charges_usage].first
expect(charge_usage[:units]).to eq('8.0')
expect(charge_usage[:events_count]).to eq(8)
expect(charge_usage[:amount_cents]).to eq(65_620) # TODO
expect(charge_usage[:amount_cents]).to eq(60_772)
expect(charge_usage[:filters].count).to eq(9)

gbp_domestic_fps_charge = charge_usage[:filters].find do |f|
Expand Down Expand Up @@ -1190,7 +1192,7 @@ def aggregate(event, previous_state, aggregation_properties)
end
expect(chf_charge[:events_count]).to eq(2)
expect(chf_charge[:units]).to eq('2.0')
expect(chf_charge[:amount_cents]).to eq(40_400)
expect(chf_charge[:amount_cents]).to eq(35_552)
end
end
end
Expand Down
Loading