diff --git a/app/models/reporting_event.rb b/app/models/reporting_event.rb index f083e32a1..04b319c10 100644 --- a/app/models/reporting_event.rb +++ b/app/models/reporting_event.rb @@ -3,8 +3,11 @@ # Table name: reporting_events # # id :bigint not null, primary key +# channel_type :string +# conversation_created_at :datetime # event_end_time :datetime # event_start_time :datetime +# from_state :string # name :string # value :float # value_in_business_hours :float @@ -13,17 +16,22 @@ # account_id :integer # conversation_id :integer # inbox_id :integer +# team_id :bigint # user_id :integer # # Indexes # -# index_reporting_events_on_account_id (account_id) -# index_reporting_events_on_conversation_id (conversation_id) -# index_reporting_events_on_created_at (created_at) -# index_reporting_events_on_inbox_id (inbox_id) -# index_reporting_events_on_name (name) -# index_reporting_events_on_user_id (user_id) -# reporting_events__account_id__name__created_at (account_id,name,created_at) +# idx_reporting_events_on_account_channel_name_created (account_id,channel_type,name,created_at) +# idx_reporting_events_on_account_conv_created_at_name (account_id,conversation_created_at,name) +# idx_reporting_events_on_account_from_state_name_created (account_id,from_state,name,created_at) +# idx_reporting_events_on_account_team_name_created (account_id,team_id,name,created_at) +# index_reporting_events_on_account_id (account_id) +# index_reporting_events_on_conversation_id (conversation_id) +# index_reporting_events_on_created_at (created_at) +# index_reporting_events_on_inbox_id (inbox_id) +# index_reporting_events_on_name (name) +# index_reporting_events_on_user_id (user_id) +# reporting_events__account_id__name__created_at (account_id,name,created_at) # class ReportingEvent < ApplicationRecord @@ -35,6 +43,7 @@ class ReportingEvent < ApplicationRecord belongs_to :user, optional: true belongs_to :inbox, optional: true belongs_to :conversation, optional: true + belongs_to :team, optional: true # Scopes for filtering scope :filter_by_date_range, lambda { |range| @@ -52,4 +61,12 @@ class ReportingEvent < ApplicationRecord scope :filter_by_name, lambda { |name| where(name: name) if name.present? } + + scope :filter_by_team_id, lambda { |team_id| + where(team_id: team_id) if team_id.present? + } + + scope :filter_by_channel_type, lambda { |channel_type| + where(channel_type: channel_type) if channel_type.present? + } end diff --git a/db/migrate/20260121121101_add_fields_to_reporting_events.rb b/db/migrate/20260121121101_add_fields_to_reporting_events.rb new file mode 100644 index 000000000..1863050bf --- /dev/null +++ b/db/migrate/20260121121101_add_fields_to_reporting_events.rb @@ -0,0 +1,19 @@ +class AddFieldsToReportingEvents < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + + def change + add_column :reporting_events, :conversation_created_at, :datetime + add_column :reporting_events, :from_state, :string + add_column :reporting_events, :team_id, :bigint + add_column :reporting_events, :channel_type, :string + + add_index :reporting_events, [:account_id, :conversation_created_at, :name], + algorithm: :concurrently, name: 'idx_reporting_events_on_account_conv_created_at_name' + add_index :reporting_events, [:account_id, :from_state, :name, :created_at], + algorithm: :concurrently, name: 'idx_reporting_events_on_account_from_state_name_created' + add_index :reporting_events, [:account_id, :team_id, :name, :created_at], + algorithm: :concurrently, name: 'idx_reporting_events_on_account_team_name_created' + add_index :reporting_events, [:account_id, :channel_type, :name, :created_at], + algorithm: :concurrently, name: 'idx_reporting_events_on_account_channel_name_created' + end +end diff --git a/db/schema.rb b/db/schema.rb index 148e7769c..5170a6f28 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: 2026_01_20_121402) do +ActiveRecord::Schema[7.1].define(version: 2026_01_21_121101) do # These extensions should be enabled to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -1114,7 +1114,15 @@ ActiveRecord::Schema[7.1].define(version: 2026_01_20_121402) do t.float "value_in_business_hours" t.datetime "event_start_time", precision: nil t.datetime "event_end_time", precision: nil + t.datetime "conversation_created_at" + t.string "from_state" + t.bigint "team_id" + t.string "channel_type" + t.index ["account_id", "channel_type", "name", "created_at"], name: "idx_reporting_events_on_account_channel_name_created" + t.index ["account_id", "conversation_created_at", "name"], name: "idx_reporting_events_on_account_conv_created_at_name" + t.index ["account_id", "from_state", "name", "created_at"], name: "idx_reporting_events_on_account_from_state_name_created" t.index ["account_id", "name", "created_at"], name: "reporting_events__account_id__name__created_at" + t.index ["account_id", "team_id", "name", "created_at"], name: "idx_reporting_events_on_account_team_name_created" t.index ["account_id"], name: "index_reporting_events_on_account_id" t.index ["conversation_id"], name: "index_reporting_events_on_conversation_id" t.index ["created_at"], name: "index_reporting_events_on_created_at"