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(rounding): Apply rounding to billable metric aggregation results #2775

Merged
merged 1 commit into from
Nov 5, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module BillableMetrics
module Aggregations
class ApplyRoundingService < ::BaseService
def initialize(billable_metric:, units:)
@billable_metric = billable_metric
@units = units

super
end

def call
precision = billable_metric.rounding_precision || 0

result.units = case billable_metric.rounding_function&.to_sym
when :ceil
units.ceil(precision)
when :floor
units.floor(precision)
when :round
units.round(precision)
else
units
end

result
end

private

attr_reader :billable_metric, :units
end
end
end
25 changes: 25 additions & 0 deletions app/services/billable_metrics/aggregations/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ def aggregate(options: {})
if charge.dynamic?
compute_grouped_by_precise_total_amount_cents(options:)
end

result.aggregations.each { apply_rounding(_1) }
else
compute_aggregation(options:)
if charge.dynamic?
compute_precise_total_amount_cents(options:)
end

apply_rounding(result)
end
result
end
Expand Down Expand Up @@ -142,6 +146,27 @@ def find_cached_aggregation(with_from_datetime:, with_to_datetime:, grouped_by:

query.first
end

def apply_rounding(result)
return if billable_metric.rounding_function.blank?
return if event.present? # Rouding does not apply to the in advance billing

result.aggregation = BillableMetrics::Aggregations::ApplyRoundingService
.call(billable_metric:, units: result.aggregation)
.units

if result.full_units_number.present?
result.full_units_number = BillableMetrics::Aggregations::ApplyRoundingService
.call(billable_metric:, units: result.full_units_number)
.units
end

if result.current_usage_units.present?
result.current_usage_units = BillableMetrics::Aggregations::ApplyRoundingService
.call(billable_metric:, units: result.current_usage_units)
.units
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe BillableMetrics::Aggregations::ApplyRoundingService, type: :service do
subject(:rounding_service) { described_class.new(billable_metric:, units:) }

let(:rounding_function) { 'round' }
let(:rounding_precision) { 2 }

let(:billable_metric) do
create(:billable_metric, rounding_precision:, rounding_function:)
end

let(:units) { 123.456 }

describe '#call' do
let(:result) { rounding_service.call }

context 'with round function' do
it 'returns the rounded units' do
expect(result.units).to eq(123.46)
end

context 'without precision' do
let(:rounding_precision) { nil }

it 'applies the rounding to the integer value' do
expect(result.units).to eq(123)
end
end

context 'with negative precision' do
let(:rounding_precision) { -2 }

it 'applies the rounding' do
expect(result.units).to eq(100)
end
end
end

context 'with ceil function' do
let(:rounding_function) { 'ceil' }

it 'returns the rounded units' do
expect(result.units).to eq(123.46)
end

context 'without precision' do
let(:rounding_precision) { nil }

it 'applies the rounding to the integer value' do
expect(result.units).to eq(124)
end
end

context 'with negative precision' do
let(:rounding_precision) { -2 }

it 'applies the rounding' do
expect(result.units).to eq(200)
end
end
end

context 'with floor function' do
let(:rounding_function) { 'floor' }

it 'returns the rounded units' do
expect(result.units).to eq(123.45)
end

context 'without precision' do
let(:rounding_precision) { nil }

it 'applies the rounding to the integer value' do
expect(result.units).to eq(123)
end
end

context 'with negative precision' do
let(:rounding_precision) { -2 }

it 'applies the rounding' do
expect(result.units).to eq(100)
end
end
end

context 'without rounding function' do
let(:rounding_function) { nil }

it 'returns the units' do
expect(result.units).to eq(units)
end
end
end
end
60 changes: 60 additions & 0 deletions spec/services/billable_metrics/aggregations/sum_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,29 @@
end
end

context 'when rounding is configured' do
let(:billable_metric) do
create(
:billable_metric,
organization:,
aggregation_type: "sum_agg",
field_name: "total_count",
rounding_function: "ceil",
rounding_precision: 2
)
end

before do
latest_events.last.update!(properties: {total_count: 12.434})
end

it 'aggregates the events' do
result = sum_service.aggregate(options:)

expect(result.aggregation).to eq(48.44)
end
end

describe ".per_event_aggregation" do
it "aggregates per events" do
result = sum_service.per_event_aggregation
Expand Down Expand Up @@ -829,5 +852,42 @@
end
end
end

context 'when rounding is configured' do
let(:billable_metric) do
create(
:billable_metric,
organization:,
aggregation_type: "sum_agg",
field_name: "total_count",
rounding_function: "ceil",
rounding_precision: 2
)
end

let(:last_event) do
latest_events.last.tap do |e|
e.update!(properties: {total_count: 12.434, agent_name: e.properties['agent_name']})
end
end

before { last_event }

it 'aggregates the events' do
result = sum_service.aggregate(options:)

expect(result.aggregations.count).to eq(4)

result.aggregations.sort_by { |a| a.grouped_by["agent_name"] }.each_with_index do |aggregation, index|
if aggregation.grouped_by["agent_name"] == last_event.properties['agent_name']
expect(aggregation.aggregation).to eq(12.44)
else
expect(aggregation.aggregation).to eq(12)
end
expect(aggregation.count).to eq(1)
expect(aggregation.grouped_by["agent_name"]).to eq(agent_names[index])
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@
expect(result.aggregation).to eq(9.64517)
expect(result.current_usage_units).to eq(29)
end

context 'when rounding is configured' do
let(:billable_metric) do
create(
:billable_metric,
organization:,
aggregation_type: 'sum_agg',
field_name: 'total_count',
recurring: true,
rounding_function: 'ceil',
rounding_precision: 2
)
end

before do
latest_events.last.update!(properties: {total_count: 12.434})
end

it 'aggregates the events' do
result = sum_service.aggregate(options:)

expect(result.aggregation).to eq(9.73)
expect(result.current_usage_units).to eq(29.44)
end
end
end

context 'when current usage context and charge is pay in advance' do
Expand Down