Skip to content

Commit 49ac86c

Browse files
committed
Perf: Cache inventory calculation in DashboardController
1 parent b0ec01e commit 49ac86c

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

app/controllers/dashboard_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ class DashboardController < ApplicationController
33
respond_to :html, :js
44

55
def index
6-
@org_stats = OrganizationStats.new(current_organization)
6+
inventory = View::Inventory.new(current_organization.id)
7+
@org_stats = OrganizationStats.new(current_organization, inventory)
78
@partners_awaiting_review = current_organization.partners.awaiting_review
89
@outstanding_requests = current_organization
910
.ordered_requests
@@ -12,7 +13,7 @@ def index
1213
.order(:created_at)
1314
.limit(25)
1415

15-
@low_inventory_report = LowInventoryQuery.call(current_organization)
16+
@low_inventory_report = LowInventoryQuery.call(current_organization, inventory)
1617

1718
# passing nil here filters the announcements that didn't come from an organization
1819
@broadcast_announcements = BroadcastAnnouncement.filter_announcements(nil)

app/models/organization_stats.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ class OrganizationStats
77
:donation_sites,
88
to: :current_organization, allow_nil: true
99

10-
def initialize(organization)
10+
def initialize(organization, inventory)
1111
@current_organization = organization
12+
@inventory = inventory
1213
end
1314

1415
def partners_added
@@ -26,11 +27,10 @@ def donation_sites_added
2627
def locations_with_inventory
2728
return [] unless storage_locations
2829

29-
inventory = View::Inventory.new(current_organization.id)
3030
storage_locations.select { |loc| inventory.quantity_for(storage_location: loc.id).positive? }
3131
end
3232

3333
private
3434

35-
attr_reader :current_organization
35+
attr_reader :current_organization, :inventory
3636
end

app/queries/low_inventory_query.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class LowInventoryQuery
22
LowInventoryItem = Data.define(:id, :name, :on_hand_minimum_quantity, :on_hand_recommended_quantity, :total_quantity)
33

4-
def self.call(organization)
5-
inventory = View::Inventory.new(organization.id)
4+
def self.call(organization, inventory)
65
items = inventory.all_items.uniq(&:item_id)
76

87
low_inventory_items = []

0 commit comments

Comments
 (0)