Skip to content

Commit bf2dfa8

Browse files
authored
Merge pull request #4939 from coalest/cache-inventory-calc-on-dashboard
Perf: Cache inventory calculation on dashboard
2 parents 9e4d7cf + 1fb7f1d commit bf2dfa8

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

app/controllers/dashboard_controller.rb

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

55
def index
6-
@org_stats = OrganizationStats.new(current_organization)
6+
@org_stats = OrganizationStats.new(current_organization, inventory)
77
@partners_awaiting_review = current_organization.partners.awaiting_review
88
@outstanding_requests = current_organization
99
.ordered_requests
@@ -12,9 +12,15 @@ def index
1212
.order(:created_at)
1313
.limit(25)
1414

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

1717
# passing nil here filters the announcements that didn't come from an organization
1818
@broadcast_announcements = BroadcastAnnouncement.filter_announcements(nil)
1919
end
20+
21+
private
22+
23+
def inventory
24+
@inventory ||= View::Inventory.new(current_organization.id)
25+
end
2026
end

app/models/organization_stats.rb

Lines changed: 6 additions & 6 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
@@ -23,14 +24,13 @@ def donation_sites_added
2324
donation_sites&.length || 0
2425
end
2526

26-
def locations_with_inventory
27-
return [] unless storage_locations
27+
def num_locations_with_inventory
28+
return 0 unless storage_locations
2829

29-
inventory = View::Inventory.new(current_organization.id)
30-
storage_locations.select { |loc| inventory.quantity_for(storage_location: loc.id).positive? }
30+
storage_locations.count { |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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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 = View::Inventory.new(organization.id))
5+
return [] if organization.blank?
6+
67
items = inventory.all_items.uniq(&:item_id)
78

89
low_inventory_items = []

app/views/dashboard/_getting_started_prompt.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<% partner_criteria_met = org_stats.partners_added > 0 %>
33
<% location_criteria_met = org_stats.storage_locations_added > 0 %>
44
<% donation_criteria_met = org_stats.donation_sites_added > 0 %>
5-
<% inventory_criteria_met = org_stats.locations_with_inventory.length > 0 %>
5+
<% inventory_criteria_met = org_stats.num_locations_with_inventory > 0 %>
66
<% criterias = [ location_criteria_met, partner_criteria_met, donation_criteria_met, inventory_criteria_met ] %>
77
<% current_step = criterias.find_index(false) %>
88

@@ -116,7 +116,7 @@
116116
<% else %>
117117
<i class="fa fa-pie-chart fa-2x" aria-hidden="true"></i>
118118
<% end %>
119-
<h4><%= pluralize(org_stats.locations_with_inventory.length, 'Storage Location') %> with Inventory</h4>
119+
<h4><%= pluralize(org_stats.num_locations_with_inventory, 'Storage Location') %> with Inventory</h4>
120120
</div>
121121

122122
<div class="org-stats-desc">

spec/models/organization_stats_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
#
33
RSpec.describe OrganizationStats, type: :model do
44
let(:current_org) { create(:organization) }
5+
let(:inventory) { View::Inventory.new(current_org.id) }
56

6-
subject { described_class.new(current_org) }
7+
subject { described_class.new(current_org, inventory) }
78

89
describe "partners_added method >" do
910
context "current org is nil >" do
1011
let(:current_org) { nil }
12+
let(:inventory) { nil }
1113

1214
it "should return 0" do
1315
expect(subject.partners_added).to eq(0)
@@ -28,6 +30,7 @@
2830
describe "storage_locations_added method >" do
2931
context "current org is nil >" do
3032
let(:current_org) { nil }
33+
let(:inventory) { nil }
3134

3235
it "should return 0" do
3336
expect(subject.storage_locations_added).to eq(0)
@@ -48,6 +51,7 @@
4851
describe "donation_sites_added method >" do
4952
context "current org is nil >" do
5053
let(:current_org) { nil }
54+
let(:inventory) { nil }
5155

5256
it "should return 0" do
5357
expect(subject.donation_sites_added).to eq(0)
@@ -65,12 +69,13 @@
6569
end
6670
end
6771

68-
describe "locations_with_inventory method >" do
72+
describe "num_locations_with_inventory method >" do
6973
context "current org is nil >" do
7074
let(:current_org) { nil }
75+
let(:inventory) { nil }
7176

7277
it "should return an empty array" do
73-
expect(subject.locations_with_inventory).to eq([])
78+
expect(subject.num_locations_with_inventory).to eq(0)
7479
end
7580
end
7681

@@ -87,7 +92,7 @@
8792
end
8893

8994
it "should return storage location" do
90-
expect(subject.locations_with_inventory).to include(storage_location_1)
95+
expect(subject.num_locations_with_inventory).to eq(1)
9196
end
9297
end
9398

@@ -96,7 +101,7 @@
96101
let(:storage_locations) { [storage_location_1] }
97102

98103
it "should return an empty array" do
99-
expect(subject.locations_with_inventory).to eq([])
104+
expect(subject.num_locations_with_inventory).to eq(0)
100105
end
101106
end
102107
end

0 commit comments

Comments
 (0)