Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #167 from librato/generator-fixin
Browse files Browse the repository at this point in the history
Change pagination generator to be recursive
  • Loading branch information
mbeale authored Aug 18, 2017
2 parents cd73875 + 176a7d9 commit 3f3521a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
39 changes: 13 additions & 26 deletions librato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,19 @@ def add_tags(self, d):

# Return all items for a "list" request
def _get_paginated_results(self, entity, klass, **query_props):
offset = query_props.get('offset', 0)
while True:
if offset > 0:
query_props['offset'] = offset
resp = self._mexe(entity, query_props=query_props)

results = self._parse(resp, entity, klass)
for result in results:
resp = self._mexe(entity, query_props=query_props)

results = self._parse(resp, entity, klass)
for result in results:
yield result

length = resp.get('query', {}).get('length', 0)
offset = query_props.get('offset', 0) + length
total = resp.get('query', {}).get('total', length)
if offset < total and length > 0:
query_props.update({'offset': offset})
for result in self._get_paginated_results(entity, klass, **query_props):
yield result
length = resp.get('query', {}).get('length', 0)
offset += length
total = resp.get('query', {}).get('total', length)
if offset >= total or length == 0:
break

#
# Metrics
Expand All @@ -269,19 +268,7 @@ def list_metrics(self, **query_props):
return self._parse(resp, "metrics", Metric)

def list_all_metrics(self, **query_props):
"""List all avaliable metrics"""
if 'length' not in query_props:
query_props['length'] = 100
if 'offset' not in query_props:
query_props['offset'] = 0
page_size = query_props['length']
while True:
metric_list = self.list_metrics(**query_props)
for m in metric_list:
yield m
query_props['offset'] += page_size
if len(metric_list) < page_size:
break
return self._get_paginated_results("metrics", Metric, **query_props)

def submit(self, name, value, type="gauge", **query_props):
if 'tags' in query_props:
Expand Down
39 changes: 28 additions & 11 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
# Mock the server
librato.HTTPSConnection = MockConnect

fake_metric = {
"name": "3333",
"display_name": "test name",
"type": "gauge",
"attributes": {
"created_by_ua": "fake",
},
"description": "a description",
"period": 60,
"source_lag": 60
}


class TestLibrato(unittest.TestCase):
def setUp(self):
Expand All @@ -23,26 +35,31 @@ def test_list_metrics_when_there_are_no_metrics(self):
assert len(metrics) == 0

def test_list_all_metrics(self):
def mock_list(**args):
offset = args['offset']
length = args['length']
def mock_list(entity, query_props=None):
length = query_props['length']
offset = query_props['offset']
# I don't care what the metrics are
# this is about testing the logic and the calls
result = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
return result[offset:length + offset]
result = [fake_metric for x in range(12)]
return {
"query":
{
"offset": offset,
"length": length,
"found": 12,
"total": 12
},
"metrics": result[offset:length + offset]
}

expected_call_list = [({'length': 5, 'offset': 0},),
({'length': 5, 'offset': 5},),
({'length': 5, 'offset': 10},)]
with patch.object(
self.conn,
'list_metrics',
'_mexe',
) as list_prop:
list_prop.side_effect = mock_list
metrics = list(self.conn.list_all_metrics(length=5))
metrics = list(self.conn.list_all_metrics(length=5, offset=0))
assert len(metrics) == 12
assert list_prop.call_count == 3
assert list_prop.call_args_list == expected_call_list

def test_list_metrics_adding_gauge(self):
""" Notice that the api forces you to send a value even when you are
Expand Down

0 comments on commit 3f3521a

Please sign in to comment.