Skip to content

Commit

Permalink
feat(event): Add view to allow caching of charge/filters for billable…
Browse files Browse the repository at this point in the history
…_metrics (#2231)

This PR is part of the real-time aggregation epic

This PR adds a new `billable_metrics_grouped_charges` database view. It
selects the charges for each organization/billable metric code/plan
tupple. It will be used to route events received on the ingest service
to the right bucket in order to perform a pre-computation and allow
future "real time aggregation"
  • Loading branch information
vincent-pochet authored and ancorcruz committed Jul 7, 2024
1 parent d46e5d4 commit 1e91af7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/charge_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ChargeFilter < ApplicationRecord
include Discard::Model
self.discard_column = :deleted_at

belongs_to :charge, -> { with_discarded }
belongs_to :charge, -> { with_discarded }, touch: true

has_many :values, class_name: 'ChargeFilterValue', dependent: :destroy
has_many :billable_metric_filters, through: :values
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class CreateBillableMetricsGroupedCharges < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
create_view :billable_metrics_grouped_charges
end
end
21 changes: 21 additions & 0 deletions db/schema.rb

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

42 changes: 42 additions & 0 deletions db/views/billable_metrics_grouped_charges_v01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
select
billable_metrics.organization_id,
billable_metrics.code,
billable_metrics.aggregation_type,
billable_metrics.field_name,
charges.plan_id,
charges.id as charge_id,
charge_filters.id as charge_filter_id,
json_object_agg(
billable_metric_filters.key,
coalesce(charge_filter_values.values, '{}')
order by billable_metric_filters.key asc
) FILTER (WHERE billable_metric_filters.key IS NOT NULL) AS filters,
(
case when charges.charge_model = 0 -- Standard
then
coalesce(charge_filters.properties->'grouped_by', charges.properties->'grouped_by')
else
null
end
) AS grouped_by

from billable_metrics
inner join charges on charges.billable_metric_id = billable_metrics.id
left join charge_filters on charge_filters.charge_id = charges.id
left join charge_filter_values on charge_filter_values.charge_filter_id = charge_filters.id
left join billable_metric_filters on charge_filter_values.billable_metric_filter_id = billable_metric_filters.id
where
billable_metrics.deleted_at is null
and charges.deleted_at is null
and charges.pay_in_advance = false
and charge_filters.deleted_at is null
and charge_filter_values.deleted_at is null
and billable_metric_filters.deleted_at is null
group by
billable_metrics.organization_id,
billable_metrics.code,
billable_metrics.aggregation_type,
billable_metrics.field_name,
charges.plan_id,
charges.id,
charge_filters.id

0 comments on commit 1e91af7

Please sign in to comment.