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_diff_invalid(self):
testdb = "test-%s" % self.filename
self.addCleanup(self._remove, testdb)
whisper.create(testdb, [(120, 10)])
whisper.create(self.filename, self.retention)
# Merging 2 archives with different retentions should fail
with open(testdb, 'rb') as fh_1:
with open(self.filename, 'rb+') as fh_2:
with AssertRaisesException(
NotImplementedError(
'test-db.wsp and db.wsp archive configurations are '
'unalike. Resize the input before diffing')):
whisper.file_diff(fh_1, fh_2)
def test_create_and_info(self):
"""
Create a db and use info() to validate
"""
# check if invalid configuration fails successfully
for retention in (0, []):
with AssertRaisesException(
whisper.InvalidConfiguration(
'You must specify at least one archive configuration!')):
whisper.create(self.filename, retention)
# create a new db with a valid configuration
whisper.create(self.filename, self.retention)
# Ensure another file can't be created when one exists already
with AssertRaisesException(
whisper.InvalidConfiguration(
'File {0} already exists!'.format(self.filename))):
whisper.create(self.filename, self.retention)
info = whisper.info(self.filename)
# check header information
self.assertEqual(info['maxRetention'],
max([a[0] * a[1] for a in self.retention]))
self.assertEqual(info['aggregationMethod'], 'average')
self.assertEqual(info['xFilesFactor'], 0.5)
# check archive information
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),
)
def test_normal(self):
whisper.create(self.filename, [(1, 60), (60, 60)])
whisper.CACHE_HEADERS = True
whisper.info(self.filename)
whisper.info(self.filename)
whisper.CACHE_HEADERS = False
def test_diff(self):
testdb = "test-%s" % self.filename
now = int(time.time())
self.addCleanup(self._remove, testdb)
whisper.create(testdb, self.retention)
whisper.create(self.filename, self.retention)
whisper.update(testdb, 1.0, now)
whisper.update(self.filename, 2.0, now)
results = whisper.diff(testdb, self.filename)
expected = [(0, [(int(now), 1.0, 2.0)], 1), (1, [], 0)]
self.assertEqual(results, expected)
def test_diff_with_empty(self):
testdb = "test-%s" % self.filename
now = time.time()
self.addCleanup(self._remove, testdb)
whisper.create(testdb, self.retention)
whisper.create(self.filename, self.retention)
whisper.update(testdb, 1.0, now)
whisper.update(self.filename, 2.0, now)
# Purposefully insert nulls to strip out
previous = now - self.retention[0][0]
whisper.update(testdb, float('NaN'), previous)
results = whisper.diff(testdb, self.filename, ignore_empty=True)
self.assertEqual(
results,
[(0, [(int(now), 1.0, 2.0)], 1), (1, [], 0)],
)
results_empties = whisper.diff(testdb, self.filename, ignore_empty=False)
expected = [(0, [(int(previous), float('NaN'), None), (int(now), 1.0, 2.0)], 2), (1, [], 0)]
sys.exit(0)
if len(args) < 2:
option_parser.print_help()
sys.exit(1)
path = args[0]
archives = [whisper.parseRetentionDef(retentionDef)
for retentionDef in args[1:]]
if os.path.exists(path) and options.overwrite:
print('Overwriting existing file: %s' % path)
os.unlink(path)
try:
whisper.create(path, archives, xFilesFactor=options.xFilesFactor,
aggregationMethod=options.aggregationMethod, sparse=options.sparse,
useFallocate=options.fallocate)
except whisper.WhisperException as exc:
raise SystemExit('[ERROR] %s' % str(exc))
size = os.stat(path).st_size
print('Created: %s (%d bytes)' % (path, size))
def _create(self):
"""Create the Whisper file on disk"""
if not os.path.exists(settings.SALMON_WHISPER_DB_PATH):
os.makedirs(settings.SALMON_WHISPER_DB_PATH)
archives = [whisper.parseRetentionDef(retentionDef)
for retentionDef in settings.ARCHIVES.split(",")]
whisper.create(self.path, archives,
xFilesFactor=settings.XFILEFACTOR,
aggregationMethod=settings.AGGREGATION_METHOD)
def handle(self):
points = 0
for metric in self.redis.smembers(METRICS):
values = self.redis.zrange(metric, 0, -1)
points += len(values)
f = target_to_path(self.path, metric)
d = os.path.dirname(f)
if d not in self.dirs:
if not os.path.isdir(d):
os.makedirs(d)
self.dirs.add(d)
if not os.path.exists(f):
whisper.create(f, [(10, 1000)]) # [FIXME] hardcoded values
whisper.update_many(f, [struct.unpack('!ff', a) for a in values])
if len(values):
self.redis.zrem(metric, *values)
self.metric(METRIC_POINTS, points)
def create(self, metric, retentions, xfilesfactor, aggregation_method):
path = self.getFilesystemPath(metric)
directory = dirname(path)
try:
if not exists(directory):
os.makedirs(directory)
except OSError as e:
log.err("%s" % e)
whisper.create(path, retentions, xfilesfactor, aggregation_method,
self.sparse_create, self.fallocate_create)