Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: scrapinghub.hustorage.frontier.Frontier.count_slot #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions scrapinghub/hubstorage/frontier.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ def delete(self, frontier, slot, ids):

def delete_slot(self, frontier, slot):
self.apidelete((frontier, 's', slot))

def count_slot(self, frontier, slot):
total = {
'count': 0,
'scanned': 0,
}
start = None
while True:
ret = list(self.apiget(
(frontier, 's', slot, 'q/count'),
params={'start': start}
))
total['count'] += ret[0]['count']
total['scanned'] += ret[0]['scanned']
start = ret[0].get('nextstart')
if not start:
break
return total
9 changes: 9 additions & 0 deletions tests/hubstorage/test_frontier.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_add_read(hsproject):
def test_add_multiple_chunks(hsproject):
frontier = hsproject.frontier
old_count = frontier.newcount
initial_count = frontier.count_slot(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT)

batch_size = 50
fps1 = [{'fp': '/index_%s.html' % fp} for fp in range(0, batch_size)]
Expand All @@ -50,6 +51,10 @@ def test_add_multiple_chunks(hsproject):

assert frontier.newcount == 150 + old_count

# test count_slot
count = frontier.count_slot(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT)
assert count['count'] == initial_count['count'] + 150

# insert repeated fingerprints
fps4 = [{'fp': '/index_%s.html' % fp} for fp in range(0, batch_size)]
frontier.add(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT, fps3)
Expand All @@ -69,6 +74,10 @@ def test_add_multiple_chunks(hsproject):
ids = [batch['id'] for batch in batches]
frontier.delete(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT, ids)

# test count_slot again
count = frontier.count_slot(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT)
assert count['count'] - initial_count['count'] == 150 - 100

# get remaining 50
batches = list(frontier.read(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT))
urls = [_get_urls(batch) for batch in batches]
Expand Down