Skip to content

Commit

Permalink
feat(rounding): Add rounding fields to billable_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Oct 31, 2024
1 parent 296e988 commit ab5ca20
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
38 changes: 22 additions & 16 deletions app/models/billable_metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BillableMetric < ApplicationRecord
has_many :coupon_targets
has_many :coupons, through: :coupon_targets
has_many :groups, dependent: :delete_all
has_many :filters, -> { order(:key) }, dependent: :delete_all, class_name: 'BillableMetricFilter'
has_many :filters, -> { order(:key) }, dependent: :delete_all, class_name: "BillableMetricFilter"

AGGREGATION_TYPES = {
count_agg: 0,
Expand All @@ -27,9 +27,12 @@ class BillableMetric < ApplicationRecord
}.freeze
AGGREGATION_TYPES_PAYABLE_IN_ADVANCE = %i[count_agg sum_agg unique_count_agg custom_agg].freeze

WEIGHTED_INTERVAL = {seconds: 'seconds'}.freeze
ROUNDING_FUNCTIONS = {round: "round", ceil: "ceil", floor: "floor"}.freeze

WEIGHTED_INTERVAL = {seconds: "seconds"}.freeze

enum aggregation_type: AGGREGATION_TYPES
enum rounding_function: ROUNDING_FUNCTIONS
enum weighted_interval: WEIGHTED_INTERVAL

validate :validate_recurring
Expand All @@ -44,6 +47,7 @@ class BillableMetric < ApplicationRecord
inclusion: {in: WEIGHTED_INTERVAL.values},
if: :weighted_sum_agg?
validates :custom_aggregator, presence: true, if: :custom_agg?
validates :rounding_function, inclusion: {in: ROUNDING_FUNCTIONS.values}, allow_nil: true

default_scope -> { kept }

Expand Down Expand Up @@ -81,20 +85,22 @@ def validate_recurring
#
# Table name: billable_metrics
#
# id :uuid not null, primary key
# aggregation_type :integer not null
# code :string not null
# custom_aggregator :text
# deleted_at :datetime
# description :string
# field_name :string
# name :string not null
# properties :jsonb
# recurring :boolean default(FALSE), not null
# weighted_interval :enum
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :uuid not null
# id :uuid not null, primary key
# aggregation_type :integer not null
# code :string not null
# custom_aggregator :text
# deleted_at :datetime
# description :string
# field_name :string
# name :string not null
# properties :jsonb
# recurring :boolean default(FALSE), not null
# rounding_function :enum
# rounding_precision :integer
# weighted_interval :enum
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :uuid not null
#
# Indexes
#
Expand Down
10 changes: 5 additions & 5 deletions db/migrate/20240314165306_migrate_groups_to_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
class MigrateGroupsToFilters < ActiveRecord::Migration[7.0]
disable_ddl_transaction!

class BillableMetric < ApplicationRecord
has_many :groups
has_many :filters, -> { order(:key) }, dependent: :delete_all, class_name: 'BillableMetricFilter'
end

class BillableMetricFilter < ApplicationRecord
belongs_to :billable_metric
end
Expand All @@ -26,11 +31,6 @@ class Charge < ApplicationRecord
has_many :filter_values, through: :filters, class_name: 'ChargeFilterValue', source: :values
end

class BillableMetric < ApplicationRecord
has_many :groups
has_many :filters, -> { order(:key) }, dependent: :delete_all, class_name: 'BillableMetricFilter'
end

class ChargeFilter < ApplicationRecord
belongs_to :charge
has_many :values, class_name: 'ChargeFilterValue', dependent: :destroy
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20240603095841_refresh_cached_aggregations.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# frozen_string_literal: true

class RefreshCachedAggregations < ActiveRecord::Migration[7.0]
class BillableMetric < ApplicationRecord
enum aggregation_type: {
count_agg: 0,
sum_agg: 1,
max_agg: 2,
unique_count_agg: 3,
weighted_sum_agg: 5,
latest_agg: 6,
custom_agg: 7
}
end

class Subscription < ApplicationRecord
belongs_to :plan
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class AddRoundingOptionsToBillableMetrics < ActiveRecord::Migration[7.1]
def change
create_enum :billable_metric_rounding_function, %w[round floor ceil]

safety_assured do
change_table :billable_metrics, bulk: true do |t|
t.enum :rounding_function, enum_type: "billable_metric_rounding_function"
t.integer :rounding_precision
end
end
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

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

0 comments on commit ab5ca20

Please sign in to comment.