diff --git a/app/models/charge_filter.rb b/app/models/charge_filter.rb index 16f082040af..ec76541828c 100644 --- a/app/models/charge_filter.rb +++ b/app/models/charge_filter.rb @@ -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 diff --git a/db/migrate/20240701083355_create_billable_metrics_grouped_charges.rb b/db/migrate/20240701083355_create_billable_metrics_grouped_charges.rb new file mode 100644 index 00000000000..5964918ae2e --- /dev/null +++ b/db/migrate/20240701083355_create_billable_metrics_grouped_charges.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 5b1f1c3e0f4..1dbf167a3c0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_06_28_083830) do +ActiveRecord::Schema[7.1].define(version: 2024_07_01_083355) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -1215,4 +1215,25 @@ SQL add_index "last_hour_events_mv", ["organization_id"], name: "index_last_hour_events_mv_on_organization_id" + create_view "billable_metrics_grouped_charges", sql_definition: <<-SQL + 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", '{}'::character varying[]) ORDER BY billable_metric_filters.key) FILTER (WHERE (billable_metric_filters.key IS NOT NULL)) AS filters, + CASE + WHEN (charges.charge_model = 0) THEN COALESCE((charge_filters.properties -> 'grouped_by'::text), (charges.properties -> 'grouped_by'::text)) + ELSE NULL::jsonb + END AS grouped_by + FROM ((((billable_metrics + 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; + SQL end diff --git a/db/views/billable_metrics_grouped_charges_v01.sql b/db/views/billable_metrics_grouped_charges_v01.sql new file mode 100644 index 00000000000..9cf1b404bd0 --- /dev/null +++ b/db/views/billable_metrics_grouped_charges_v01.sql @@ -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