Skip to content

Commit

Permalink
Use a constant to set the default batch size
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leenagupte committed Mar 1, 2017
1 parent 614c4dc commit 31fed09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/models/importers/number_of_views_by_organisation.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
21 changes: 18 additions & 3 deletions spec/models/importers/number_of_views_by_organisation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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

0 comments on commit 31fed09

Please sign in to comment.