A platform for community discussion. Free, open, simple.
PERF: removes N+1 when loading events list (#34841)
Joffrey JAFFEUX908371191fcd
diff --git a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rbindex 5a5bf64c507..145a51af11e 100644--- a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb+++ b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb@@ -5,7 +5,10 @@ module DiscoursePostEventdef index@events =DiscoursePostEvent::EventFinder.search(current_user, filtered_events_params).includes(- post: :topic,+ :event_dates,+ post: {+ topic: %i[tags category],+ },)# The detailed serializer is currently not used anywhere in the frontend, but available via APIdiff --git a/plugins/discourse-calendar/app/models/discourse_post_event/event.rb b/plugins/discourse-calendar/app/models/discourse_post_event/event.rbindex 371fb9acc8f..25f5a8180ab 100644--- a/plugins/discourse-calendar/app/models/discourse_post_event/event.rb+++ b/plugins/discourse-calendar/app/models/discourse_post_event/event.rb@@ -17,6 +17,7 @@ module DiscoursePostEventbelongs_to :post, foreign_key: :idscope :visible, -> { where(deleted_at: nil) }+ scope :open, -> { where(closed: false) }after_commit :destroy_topic_custom_field, on: %i[destroy]after_commit :create_or_update_event_date, on: %i[create update]@@ -110,21 +111,31 @@ module DiscoursePostEventdef starts_atreturn nil if recurring? && recurrence_until.present? && recurrence_until < Time.current- from_event_dates =- event_dates.pending.order(:starts_at).last&.starts_at ||- event_dates.order(:updated_at, :id).last&.starts_at+ date =+ if association(:event_dates).loaded?+ pending = event_dates.select { |d| d.finished_at.nil? }+ pending.max_by(&:starts_at) || event_dates.max_by { |d| [d.updated_at, d.id] }+ else+ event_dates.where(finished_at: nil).order(:starts_at).last ||+ event_dates.order(:updated_at, :id).last+ end- from_event_dates || original_starts_at+ date&.starts_at || original_starts_atenddef ends_atreturn nil if recurring? && recurrence_until.present? && recurrence_until < Time.current- from_event_dates =- event_dates.pending.order(:starts_at).last&.ends_at ||- event_dates.order(:updated_at, :id).last&.ends_at+ date =+ if association(:event_dates).loaded?+ pending = event_dates.select { |d| d.finished_at.nil? }+ pending.max_by(&:starts_at) || event_dates.max_by { |d| [d.updated_at, d.id] }+ else+ event_dates.where(finished_at: nil).order(:starts_at).last ||+ event_dates.order(:updated_at, :id).last+ end- from_event_dates || original_ends_at+ date&.ends_at || original_ends_atenddef on_going_event_inviteesdiff --git a/plugins/discourse-calendar/app/serializers/discourse_post_event/basic_event_serializer.rb b/plugins/discourse-calendar/app/serializers/discourse_post_event/basic_event_serializer.rbindex 1a53eb6fb73..22d60196a51 100644--- a/plugins/discourse-calendar/app/serializers/discourse_post_event/basic_event_serializer.rb+++ b/plugins/discourse-calendar/app/serializers/discourse_post_event/basic_event_serializer.rb@@ -32,7 +32,12 @@ module DiscoursePostEvent""end),- topic: TopicListItemSerializer.new(object.post.topic, scope:, root: false).as_json,+ topic:+ DiscoursePostEvent::EventTopicSerializer.new(+ object.post.topic,+ scope:,+ root: false,+ ).as_json,}enddiff --git a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_topic_serializer.rb b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_topic_serializer.rbnew file mode 100644index 00000000000..5094433caf3--- /dev/null+++ b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_topic_serializer.rb@@ -0,0 +1,10 @@+# frozen_string_literal: true++module DiscoursePostEvent+ class EventTopicSerializer < ApplicationSerializer+ include TopicTagsMixin++ attributes :id+ attributes :title+ end+enddiff --git a/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rb b/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rbindex 0419ff7f2f3..4553afa3878 100644--- a/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rb+++ b/plugins/discourse-calendar/lib/discourse_post_event/event_finder.rb@@ -21,6 +21,8 @@ module DiscoursePostEventpms = private_messages(user)DiscoursePostEvent::Event+ .visible+ .open.includes(:event_dates, :post, post: :topic).joins(post: :topic).merge(Post.secured(guardian))@@ -28,7 +30,7 @@ module DiscoursePostEvent.joins(latest_event_date_join).select("discourse_post_event_events.*, latest_event_dates.starts_at").where(- "(discourse_post_event_events.recurrence IS NOT NULL) OR (latest_event_dates.starts_at IS NOT NULL)",+ "(discourse_post_event_events.recurrence IS NOT NULL) OR (latest_event_dates.starts_at IS NOT NULL) OR (discourse_post_event_events.original_starts_at IS NOT NULL)",).distinctenddiff --git a/plugins/discourse-calendar/spec/fabricators/event_fabricator.rb b/plugins/discourse-calendar/spec/fabricators/event_fabricator.rbindex 3c9dac3cf9f..83001893187 100644--- a/plugins/discourse-calendar/spec/fabricators/event_fabricator.rb+++ b/plugins/discourse-calendar/spec/fabricators/event_fabricator.rb@@ -1,7 +1,17 @@# frozen_string_literal: trueFabricator(:event, from: "DiscoursePostEvent::Event") do- post { |attrs| attrs[:post] || Fabricate(:post) }+ transient :user++ post do |attrs|+ if attrs[:post]+ attrs[:post]+ else+ user = attrs[:user] || Fabricate(:user, admin: true, refresh_auto_groups: true)+ topic = attrs[:topic] || Fabricate(:topic, user:, category: Fabricate(:category))+ Fabricate(:post, user:, topic:)+ end+ endid { |attrs| attrs[:post].id }diff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rbindex b42c81bf82d..f1a2bc0b91f 100644--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb@@ -10,30 +10,59 @@ module DiscoursePostEventenddescribe "#index" do- fab!(:event_1) { Fabricate(:event, original_starts_at: 1.day.from_now) }-it "should not result in N+1 queries problem when multiple events are returned" do- original_queries = track_sql_queries { get "/discourse-post-event/events.json" }+ # Warmup+ get "/discourse-post-event/events.json"+ # Test with 1 event+ Fabricate(:event, original_starts_at: 1.day.from_now)+ queries_with_1_event = track_sql_queries { get "/discourse-post-event/events.json" }expect(response.status).to eq(200)expect(response.parsed_body["events"].length).to eq(1)- event_2 = Fabricate(:event, original_starts_at: 2.days.from_now)- event_3 = Fabricate(:event, original_starts_at: 3.days.from_now)+ # Test with 3 events+ Fabricate(:event, original_starts_at: 2.days.from_now)+ Fabricate(:event, original_starts_at: 3.days.from_now)+ queries_with_3_events = track_sql_queries { get "/discourse-post-event/events.json" }+ expect(response.status).to eq(200)+ expect(response.parsed_body["events"].length).to eq(3)- new_queries = track_sql_queries { get "/discourse-post-event/events.json" }+ expect(queries_with_3_events.count).to eq(queries_with_1_event.count)+ end++ it "should not show deleted events" do+ active_event1 = Fabricate(:event, original_starts_at: 1.day.from_now)+ active_event2 = Fabricate(:event, original_starts_at: 2.days.from_now)+ deleted_event =+ Fabricate(:event, original_starts_at: 3.days.from_now, deleted_at: 1.hour.ago)++ get "/discourse-post-event/events.json"expect(response.status).to eq(200)- expect(response.parsed_body["events"].length).to eq(3)+ events = response.parsed_body["events"]+ event_ids = events.map { |e| e["id"] }++ expect(event_ids).to match_array([active_event1.id, active_event2.id])+ end++ it "should not show closed events" do+ active_event1 = Fabricate(:event, original_starts_at: 1.day.from_now)+ active_event2 = Fabricate(:event, original_starts_at: 2.days.from_now)+ closed_event = Fabricate(:event, original_starts_at: 3.days.from_now, closed: true)++ get "/discourse-post-event/events.json"++ expect(response.status).to eq(200)+ events = response.parsed_body["events"]+ event_ids = events.map { |e| e["id"] }- # TODO: There is still N+1 query problem here so uncomment this line when it is fixed- # expect(new_queries.count).to eq(original_queries.count)+ expect(event_ids).to match_array([active_event1.id, active_event2.id])endendcontext "with an existing post" dolet(:user) { Fabricate(:user, admin: true) }- let(:topic) { Fabricate(:topic, user: user) }+ let(:topic) { Fabricate(:topic, user: user, category: Fabricate(:category)) }let(:post1) { Fabricate(:post, user: user, topic: topic) }let(:invitee1) { Fabricate(:user) }let(:invitee2) { Fabricate(:user) }