Skip to content

Commit

Permalink
minor changes and add test for fetch with archiveToSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Galera committed Jul 5, 2017
1 parent 6be7c00 commit bcf0ffa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
21 changes: 21 additions & 0 deletions test_whisper.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,27 @@ def test_setAggregation(self):
whisper.AUTOFLUSH = original_autoflush
whisper.CACHE_HEADERS = original_caching

def test_fetch_with_archive_to_select(self):
"""
fetch info from database providing the archive to select
"""

# SECOND MINUTE HOUR DAY
retention = [(1, 60), (60, 60), (3600, 24), (86400, 365)]
whisper.create(self.filename, retention)

archives = ["1s","1m","1h","1d"]

for i in range(len(archives)):
fetch = whisper.fetch(self.filename, 0, archiveToSelect=archives[i])
self.assertEqual(fetch[0][2], retention[i][0])

# check time range
self.assertEqual(fetch[0][1] - fetch[0][0],
retention[-1][0] * retention[-1][1])
with AssertRaisesException(ValueError("Invalid granularity: 2")):
fetch = whisper.fetch(self.filename, 0, archiveToSelect="2s")


class TestgetUnitString(unittest.TestCase):
def test_function(self):
Expand Down
24 changes: 11 additions & 13 deletions whisper.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,13 +885,6 @@ def fetch(path, fromTime, untilTime=None, now=None, archiveToSelect=None):
Returns None if no data can be returned
"""

#Parse granularity
if archiveToSelect:
retentionStr = str(archiveToSelect)+":1s"
archiveToSelect = parseRetentionDef(retentionStr)[0]


with open(path, 'rb') as fh:
return file_fetch(fh, fromTime, untilTime, now, archiveToSelect)

Expand Down Expand Up @@ -929,18 +922,23 @@ def file_fetch(fh, fromTime, untilTime, now=None, archiveToSelect=None):

diff = now - fromTime

#Parse granularity if requested
if archiveToSelect:
retentionStr = str(archiveToSelect)+":1"
archiveToSelect = parseRetentionDef(retentionStr)[0]

for archive in header['archives']:
if not archiveToSelect and archive['retention'] >= diff:
break
if archiveToSelect and archive['secondsPerPoint'] == archiveToSelect:
break
elif archiveToSelect:
if archiveToSelect:
if archive['secondsPerPoint'] == archiveToSelect:
break
archive = None
else:
if archive['retention'] >= diff:
break

if archiveToSelect and not archive:
raise ValueError("Invalid granularity: %s" %(archiveToSelect))


return __archive_fetch(fh, archive, fromTime, untilTime)

def __archive_fetch(fh, archive, fromTime, untilTime):
Expand Down

0 comments on commit bcf0ffa

Please sign in to comment.