From d95e2ac56cedd1c95159a24454497abeafcac600 Mon Sep 17 00:00:00 2001 From: Nicolas Florentin Date: Thu, 28 Sep 2023 15:57:30 +0200 Subject: [PATCH] improvement : performance of members#show and reservations#index --- CHANGELOG.md | 1 + app/controllers/api/reservations_controller.rb | 2 ++ app/views/api/members/show.json.jbuilder | 7 ++++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf24fa613..3fd7063d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix a bug: public availabilities (no user) was buggy (server error) - Fix a bug: unable to generate statistic - Fix a bug: rss/projects was failing with project without image +- improvement : performance of members#show and reservations#index - [TODO DEPLOY] `rails fablab:openlab:bulk_export` - [TODO DEPLOY] `rails fablab:openlab:bulk_update` diff --git a/app/controllers/api/reservations_controller.rb b/app/controllers/api/reservations_controller.rb index 260fec568..495b95c9b 100644 --- a/app/controllers/api/reservations_controller.rb +++ b/app/controllers/api/reservations_controller.rb @@ -16,8 +16,10 @@ class API::ReservationsController < API::APIController where_clause[:reservable_id] = params[:reservable_id] if params[:reservable_id] @reservations = Reservation.where(where_clause) + .preload(:reservable, :booking_users, :tickets, { statistic_profile: { user: :profile }, slots_reservations: :slot }) elsif params[:reservable_id] && params[:reservable_type] && (current_user.admin? || current_user.manager?) @reservations = Reservation.where(params.permit(:reservable_id, :reservable_type)) + .preload(:reservable, :booking_users, :tickets, { statistic_profile: { user: :profile }, slots_reservations: :slot }) else @reservations = [] end diff --git a/app/views/api/members/show.json.jbuilder b/app/views/api/members/show.json.jbuilder index 0e7a44135..4313c1e38 100644 --- a/app/views/api/members/show.json.jbuilder +++ b/app/views/api/members/show.json.jbuilder @@ -10,7 +10,8 @@ json.trainings @member.trainings do |t| json.id t.id json.name t.name end -json.training_reservations @member.reservations.where(reservable_type: 'Training').map(&:slots_reservations).flatten do |sr| +reservations = @member.reservations.where(reservable_type: 'Training').preload(slots_reservations: [:slot, reservation: :reservable]) +json.training_reservations reservations.select { |r| r.reservable_type == "Training" }.map(&:slots_reservations).flatten do |sr| json.id sr.id json.start_at sr.slot.start_at json.end_at sr.slot.end_at @@ -19,7 +20,7 @@ json.training_reservations @member.reservations.where(reservable_type: 'Training json.is_valid @member.statistic_profile.training_ids.include?(sr.reservation.reservable_id) json.canceled_at sr.canceled_at end -json.machine_reservations @member.reservations.where(reservable_type: 'Machine').map(&:slots_reservations).flatten do |sr| +json.machine_reservations reservations.select { |r| r.reservable_type == "Machine" }.map(&:slots_reservations).flatten do |sr| json.id sr.id json.start_at sr.slot.start_at json.end_at sr.slot.end_at @@ -27,7 +28,7 @@ json.machine_reservations @member.reservations.where(reservable_type: 'Machine') json.reservable_type 'Machine' json.canceled_at sr.canceled_at end -json.space_reservations @member.reservations.where(reservable_type: 'Space').map(&:slots_reservations).flatten do |sr| +json.space_reservations reservations.select { |r| r.reservable_type == "Space" }.map(&:slots_reservations).flatten do |sr| json.id sr.id json.start_at sr.slot.start_at json.end_at sr.slot.end_at