From 31fed0942528fb22452bdff2a506370efbb18235 Mon Sep 17 00:00:00 2001 From: Leena Gupte Date: Mon, 20 Feb 2017 18:05:34 +0000 Subject: [PATCH] Use a constant to set the default batch size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Made the batch size a constant rather than a attribute to make it easier to read and change later. Through trial and error, I’ve hit upon a batch size of 3000 for one month' data. This size could be larger for 7 days page views, and smaller for one year’s page views. --- .../number_of_views_by_organisation.rb | 4 +++- .../number_of_views_by_organisation_spec.rb | 21 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/models/importers/number_of_views_by_organisation.rb b/app/models/importers/number_of_views_by_organisation.rb index a8c4c19f1..d1ff70d03 100644 --- a/app/models/importers/number_of_views_by_organisation.rb +++ b/app/models/importers/number_of_views_by_organisation.rb @@ -1,10 +1,12 @@ module Importers class NumberOfViewsByOrganisation + BATCH_SIZE = 3000 + def run(slug) organisation = Organisation.find_by(slug: slug) google_analytics_service = GoogleAnalyticsService.new - organisation.content_items.find_in_batches(batch_size: 1) do |content_items| + organisation.content_items.find_in_batches(batch_size: BATCH_SIZE) do |content_items| base_paths = content_items.pluck(:base_path) results = google_analytics_service.page_views(base_paths) diff --git a/spec/models/importers/number_of_views_by_organisation_spec.rb b/spec/models/importers/number_of_views_by_organisation_spec.rb index fce08e52c..0770449d9 100644 --- a/spec/models/importers/number_of_views_by_organisation_spec.rb +++ b/spec/models/importers/number_of_views_by_organisation_spec.rb @@ -19,6 +19,9 @@ }, ] ) + + stub_const("Importers::NumberOfViewsByOrganisation::BATCH_SIZE", 1) + subject.run('the-slug') content_item_one = ContentItem.find_by(base_path: 'the-link/first') @@ -28,10 +31,22 @@ expect(content_item_two.unique_page_views).to eq(2) end - it "perform the requests in batches" do - expect_any_instance_of(GoogleAnalyticsService).to receive(:page_views).twice.and_return([]) + context "performs the request to google api in batches" do + it "makes two requests when the batch size is one" do + expect_any_instance_of(GoogleAnalyticsService).to receive(:page_views).twice.and_return([]) - subject.run('the-slug') + stub_const("Importers::NumberOfViewsByOrganisation::BATCH_SIZE", 1) + + subject.run('the-slug') + end + + it "makes one request when the batch size is two" do + expect_any_instance_of(GoogleAnalyticsService).to receive(:page_views).once.and_return([]) + + stub_const("Importers::NumberOfViewsByOrganisation::BATCH_SIZE", 2) + + subject.run('the-slug') + end end end end