From 7e88798778accb0f52e591914d532ab026bf0113 Mon Sep 17 00:00:00 2001 From: Tom Carchrae Date: Mon, 14 Jan 2019 17:56:21 -0800 Subject: [PATCH] big speedup in syncing via caching/reducing queries (#610) --- app/models/article.rb | 2 +- app/models/shared_supplier.rb | 12 +++++++++++- app/models/supplier.rb | 12 +++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index b0852133..97da62d0 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -154,7 +154,7 @@ class Article < ActiveRecord::Base # to get the correspondent shared article def shared_article(supplier = self.supplier) self.order_number.blank? and return nil - @shared_article ||= supplier.shared_supplier.shared_articles.find_by_number(self.order_number) rescue nil + @shared_article ||= supplier.shared_supplier.find_article_by_number(self.order_number) rescue nil end # convert units in foodcoop-size diff --git a/app/models/shared_supplier.rb b/app/models/shared_supplier.rb index 5108a703..a10dd402 100644 --- a/app/models/shared_supplier.rb +++ b/app/models/shared_supplier.rb @@ -1,5 +1,5 @@ class SharedSupplier < ActiveRecord::Base - + # connect to database from sharedLists-Application SharedSupplier.establish_connection(FoodsoftConfig[:shared_lists]) # set correct table_name in external DB @@ -8,6 +8,16 @@ class SharedSupplier < ActiveRecord::Base has_many :suppliers has_many :shared_articles, :foreign_key => :supplier_id + + def find_article_by_number(order_number) + # note that `shared_articles` uses number instead order_number + cached_articles.detect { |a| a.number == order_number } + end + + def cached_articles + @cached_articles ||= shared_articles.all + end + # These set of attributes are used to autofill attributes of new supplier, # when created by import from shared supplier feature. def autofill_attributes diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 708ea1bf..d29bdb87 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -32,11 +32,13 @@ class Supplier < ActiveRecord::Base # also returns an array with new articles, which should be added (depending on shared_sync_method) def sync_all updated_article_pairs, outlisted_articles, new_articles = [], [], [] + existing_articles = Set.new for article in articles.undeleted # try to find the associated shared_article shared_article = article.shared_article(self) if shared_article # article will be updated + existing_articles.add(shared_article.id) unequal_attributes = article.shared_article_changed?(self) unless unequal_attributes.blank? # skip if shared_article has not been changed article.attributes = unequal_attributes @@ -51,11 +53,11 @@ class Supplier < ActiveRecord::Base end # Find any new articles, unless the import is manual unless shared_sync_method == 'import' - for shared_article in shared_supplier.shared_articles - unless articles.undeleted.find_by_order_number(shared_article.number) - new_articles << shared_article.build_new_article(self) - end - end + shared_supplier + .shared_articles + .where(available: true) + .where.not(id: existing_articles) + .each { |shared_article| new_articles << shared_article.build_new_article(self) } end return [updated_article_pairs, outlisted_articles, new_articles] end