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