Skip to content

Commit

Permalink
Watchers work faster if order of pages is reversed
Browse files Browse the repository at this point in the history
  • Loading branch information
gousiosg committed May 27, 2014
1 parent 45a397d commit 8c34140
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/ghtorrent/retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ def retrieve_watchers(user, repo)
repo_bound_items(user, repo, :watchers,
["repos/#{user}/#{repo}/stargazers"],
{'repo' => repo, 'owner' => user},
'login')
'login', item = nil, refresh = false, order = :desc)
end

# Retrieve a single watcher for a repository
def retrieve_watcher(user, repo, watcher)
repo_bound_item(user, repo, watcher, :watchers,
["repos/#{user}/#{repo}/stargazers"],
{'repo' => repo, 'owner' => user},
'login')
'login', order = :desc)
end

def retrieve_pull_requests(user, repo, refr = false)
Expand Down Expand Up @@ -552,12 +552,18 @@ def restricted_page_request(url, pages)
end

def repo_bound_items(user, repo, entity, urls, selector, discriminator,
item_id = nil, refresh = false)
item_id = nil, refresh = false, order = :asc)

urls.each do |url|
total_pages = num_pages(ghurl url)

(1..total_pages).each do |page|
page_range = if order == :asc
(1..total_pages)
else
total_pages.downto(1)
end

page_range.each do |page|
items = api_request(ghurl(url, page))

items.each do |x|
Expand Down Expand Up @@ -614,12 +620,13 @@ def repo_bound_items(user, repo, entity, urls, selector, discriminator,
end
end

def repo_bound_item(user, repo, item_id, entity, url, selector, discriminator)
def repo_bound_item(user, repo, item_id, entity, url, selector,
discriminator, order = :asc)
stored_item = repo_bound_instance(entity, selector, discriminator, item_id)

if stored_item.empty?
repo_bound_items(user, repo, entity, url, selector, discriminator,
item_id).first
item_id, false, order).first
else
stored_item.first
end
Expand All @@ -646,15 +653,20 @@ def repo_bound_instance(entity, selector, discriminator, item_id)
end
end

def ghurl(path, page = -1)
def ghurl(path, page = -1, per_page = 100)
if page > 0
if path.include?('?')
path = path + "&page=#{page}"
path = path + "&page=#{page}&per_page=#{per_page}"
else
path = path + "?page=#{page}"
path = path + "?page=#{page}&per_page=#{per_page}"
end
config(:mirror_urlbase) + path
else
if path.include?('?')
path = path + "&per_page=#{per_page}"
else
path = path + "?per_page=#{per_page}"
end
config(:mirror_urlbase) + path
end
end
Expand Down

0 comments on commit 8c34140

Please sign in to comment.