Skip to content

Commit d605dca

Browse files
authored
Perf: Reduce number of queries and object allocations in request index page (#4920)
1 parent b91e62c commit d605dca

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

app/controllers/requests_controller.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ def index
99
.during(helpers.selected_range)
1010
.class_filter(filter_params)
1111
@unfulfilled_requests_count = current_organization.requests.where(status: [:pending, :started]).count
12-
@paginated_requests = @requests.page(params[:page])
12+
@paginated_requests = @requests.includes(:partner).page(params[:page])
1313
@calculate_product_totals = RequestsTotalItemsService.new(requests: @requests).calculate
14-
@items = current_organization.items.alphabetized
15-
@partners = current_organization.partners.order(:name)
14+
@items = current_organization.items.alphabetized.select(:id, :name)
15+
@partners = current_organization.partners.alphabetized.select(:id, :name)
1616
@statuses = Request.statuses.transform_keys(&:humanize)
17-
@partner_users = User.where(id: @paginated_requests.pluck(:partner_user_id))
17+
@partner_users = User.where(id: @paginated_requests.map(&:partner_user_id)).select(:id, :name, :email)
1818
@request_types = Request.request_types.transform_keys(&:humanize)
1919
@selected_request_type = filter_params[:by_request_type]
2020
@selected_request_item = filter_params[:by_request_item_id]

app/models/partners/item_request.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ def request_unit_is_supported
3737
end
3838

3939
def name_with_unit(quantity_override = nil)
40-
if item
41-
if Flipper.enabled?(:enable_packs) && request_unit.present?
42-
"#{name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
43-
else
44-
name
45-
end
40+
if Flipper.enabled?(:enable_packs) && request_unit.present?
41+
"#{name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
42+
else
43+
name
4644
end
4745
end
4846
end

app/services/exports/export_request_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def build_row_data(request)
9797
row += Array.new(item_headers.size, 0)
9898

9999
request.item_requests.each do |item_request|
100-
item_name = item_request.name_with_unit(0) || DELETED_ITEMS_COLUMN_HEADER
100+
item_name = item_request.item.present? ? item_request.name_with_unit(0) : DELETED_ITEMS_COLUMN_HEADER
101101
item_column_idx = headers_with_indexes[item_name]
102102
row[item_column_idx] ||= 0
103103
row[item_column_idx] += item_request.quantity.to_i
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class RequestsTotalItemsService
22
def initialize(requests:)
3-
@requests = requests.includes(item_requests: {item: :request_units})
3+
@requests = requests
44
end
55

66
def calculate
7-
return unless requests
8-
97
totals = Hash.new(0)
108
item_requests.each do |item_request|
119
totals[item_request.name_with_unit] += item_request.quantity.to_i
@@ -16,9 +14,7 @@ def calculate
1614

1715
private
1816

19-
attr_accessor :requests
20-
2117
def item_requests
22-
@item_requests ||= requests.flat_map(&:item_requests)
18+
@item_requests ||= Partners::ItemRequest.where(request: @requests)
2319
end
2420
end

spec/services/requests_total_items_service_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,21 @@
7373

7474
it { is_expected.to be_blank }
7575
end
76+
77+
context 'when request item belongs to deleted item' do
78+
let(:item) { create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") }
79+
let!(:requests) do
80+
request = create(:request, :with_item_requests, request_items: [{"item_id" => item.id, "quantity" => 10, "request_unit" => "pack"}])
81+
Request.where(id: request.id)
82+
end
83+
84+
before do
85+
item.destroy
86+
end
87+
88+
it 'returns item with correct quantity calculated' do
89+
expect(subject).to eq({"Diaper" => 10})
90+
end
91+
end
7692
end
7793
end

0 commit comments

Comments
 (0)