Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_file_fetch_edge_cases(self):
"""
Test some of the edge cases in file_fetch() that should return
None or raise an exception
"""
whisper.create(self.filename, [(1, 60)])
with open(self.filename, 'rb') as fh:
msg = "Invalid time interval: from time '{0}' is after until time '{1}'"
until_time = 0
from_time = int(time.time()) + 100
with AssertRaisesException(
whisper.InvalidTimeInterval(msg.format(from_time, until_time))):
whisper.file_fetch(fh, fromTime=from_time, untilTime=until_time)
# fromTime > now aka metrics from the future
self.assertIsNone(
whisper.file_fetch(fh, fromTime=int(time.time()) + 100,
untilTime=int(time.time()) + 200),
)
# untilTime > oldest time stored in the archive
headers = whisper.info(self.filename)
the_past = int(time.time()) - headers['maxRetention'] - 200
self.assertIsNone(
whisper.file_fetch(fh, fromTime=the_past - 1, untilTime=the_past),
)
# untilTime > now, change untilTime to now
# in a context manager as a tuple. See this for a minimal reproducer:
# http://git.io/cKz30g
with self.assertRaises(IOError):
# check a db that doesnt exist
whisper.fetch("this_db_does_not_exist", 0)
# SECOND MINUTE HOUR DAY
retention = [(1, 60), (60, 60), (3600, 24), (86400, 365)]
whisper.create(self.filename, retention)
# check a db with an invalid time range
now = int(time.time())
past = now - 6000
msg = "Invalid time interval: from time '{0}' is after until time '{1}'"
with AssertRaisesException(whisper.InvalidTimeInterval(msg.format(now, past))):
whisper.fetch(self.filename, now, past)
fetch = whisper.fetch(self.filename, 0)
# check time range
self.assertEqual(fetch[0][1] - fetch[0][0],
retention[-1][0] * retention[-1][1])
# check number of points
self.assertEqual(len(fetch[1]), retention[-1][1])
# check step size
self.assertEqual(fetch[0][2], retention[-1][0])
def file_fetch(fh, fromTime, untilTime, now=None):
header = __readHeader(fh)
if now is None:
now = int(time.time())
if untilTime is None:
untilTime = now
fromTime = int(fromTime)
untilTime = int(untilTime)
# Here we try and be flexible and return as much data as we can.
# If the range of data is from too far in the past or fully in the future, we
# return nothing
if fromTime > untilTime:
raise InvalidTimeInterval("Invalid time interval: from time '%s' is after until time '%s'" % (fromTime, untilTime))
oldestTime = now - header['maxRetention']
# Range is in the future
if fromTime > now:
return None
# Range is beyond retention
if untilTime < oldestTime:
return None
# Range requested is partially beyond retention, adjust
if fromTime < oldestTime:
fromTime = oldestTime
# Range is partially in the future, adjust
if untilTime > now:
untilTime = now
diff = now - fromTime
@coroutine
def fetch(self, fromTime=0, untilTime=None):
header = yield self._build_header()
now = int(time.time())
if untilTime is None:
untilTime = now
fromTime = int(fromTime)
untilTime = int(untilTime)
# Here we try and be flexible and return as much data as we can.
# If the range of data is from too far in the past or fully
# in the future, we return nothing
if (fromTime > untilTime):
raise InvalidTimeInterval("Invalid time interval: from time '%s' is after until time '%s'" % (fromTime, untilTime))
oldestTime = now - header['maxRetention']
# Range is in the future
if fromTime > now:
raise Return(None)
# Range is beyond retention
if untilTime < oldestTime:
raise Return(None)
# Range requested is partially beyond retention, adjust
if fromTime < oldestTime:
fromTime = oldestTime
# Range is partially in the future, adjust
if untilTime > now:
untilTime = now
diff = now - fromTime