Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
non_null = [i for i in avg_zero if i is not None]
self.assertEqual(whisper.aggregate('avg_zero', non_null, avg_zero), 1.25)
# avg_zero without neighborValues
with self.assertRaises(whisper.InvalidAggregationMethod):
whisper.aggregate('avg_zero', non_null)
# absmax with negative max
self.assertEqual(whisper.aggregate('absmax', [-3, -2, 1, 2]), -3)
# absmax with positive max
self.assertEqual(whisper.aggregate('absmax', [-2, -1, 2, 3]), 3)
# absmin with positive min
self.assertEqual(whisper.aggregate('absmin', [-3, -2, 1, 2]), 1)
# absmin with negative min
self.assertEqual(whisper.aggregate('absmin', [-2, -1, 2, 3]), -1)
with AssertRaisesException(
whisper.InvalidAggregationMethod(
'Unrecognized aggregation method derp')):
whisper.aggregate('derp', [12, 2, 3123, 1])
"""
# min of 1-4
self.assertEqual(whisper.aggregate('min', [1, 2, 3, 4]), 1)
# max of 1-4
self.assertEqual(whisper.aggregate('max', [1, 2, 3, 4]), 4)
# last element in the known values
self.assertEqual(whisper.aggregate('last', [3, 2, 5, 4]), 4)
# sum ALL THE VALUES!
self.assertEqual(whisper.aggregate('sum', [10, 2, 3, 4]), 19)
# average of the list elements
self.assertEqual(whisper.aggregate('average', [1, 2, 3, 4]), 2.5)
avg_zero = [1, 2, 3, 4, None, None, None, None]
non_null = [i for i in avg_zero if i is not None]
self.assertEqual(whisper.aggregate('avg_zero', non_null, avg_zero), 1.25)
# avg_zero without neighborValues
with self.assertRaises(whisper.InvalidAggregationMethod):
whisper.aggregate('avg_zero', non_null)
# absmax with negative max
self.assertEqual(whisper.aggregate('absmax', [-3, -2, 1, 2]), -3)
# absmax with positive max
self.assertEqual(whisper.aggregate('absmax', [-2, -1, 2, 3]), 3)
# absmin with positive min
self.assertEqual(whisper.aggregate('absmin', [-3, -2, 1, 2]), 1)
# absmin with negative min
self.assertEqual(whisper.aggregate('absmin', [-2, -1, 2, 3]), -1)
with AssertRaisesException(
whisper.InvalidAggregationMethod(
'Unrecognized aggregation method derp')):
whisper.aggregate('derp', [12, 2, 3123, 1])
def test_setAggregation(self):
"""
Create a db, change aggregation, xFilesFactor, then use info() to validate
"""
original_lock = whisper.LOCK
original_caching = whisper.CACHE_HEADERS
original_autoflush = whisper.AUTOFLUSH
whisper.LOCK = True
whisper.AUTOFLUSH = True
whisper.CACHE_HEADERS = True
# create a new db with a valid configuration
whisper.create(self.filename, self.retention)
with AssertRaisesException(
whisper.InvalidAggregationMethod(
'Unrecognized aggregation method: yummy beer')):
whisper.setAggregationMethod(self.filename, 'yummy beer')
# set setting every AggregationMethod available
for ag in whisper.aggregationMethods:
for xff in 0.0, 0.2, 0.4, 0.7, 0.75, 1.0:
# original xFilesFactor
info0 = whisper.info(self.filename)
# optional xFilesFactor not passed
old_ag = whisper.setAggregationMethod(self.filename, ag)
# should return old aggregationmethod
self.assertEqual(old_ag, info0['aggregationMethod'])
# original value should not change
info1 = whisper.info(self.filename)
def aggregate(aggregationMethod, knownValues):
if aggregationMethod == 'average':
return float(sum(knownValues)) / float(len(knownValues))
elif aggregationMethod == 'sum':
return float(sum(knownValues))
elif aggregationMethod == 'last':
return knownValues[len(knownValues)-1]
elif aggregationMethod == 'max':
return max(knownValues)
elif aggregationMethod == 'min':
return min(knownValues)
else:
raise InvalidAggregationMethod("Unrecognized aggregation method %s" %
aggregationMethod)
"""
with open(path,'r+b') as fh:
if LOCK:
fcntl.flock( fh.fileno(), fcntl.LOCK_EX )
packedMetadata = fh.read(metadataSize)
try:
(aggregationType,maxRetention,xff,archiveCount) = struct.unpack(metadataFormat,packedMetadata)
except:
raise CorruptWhisperFile("Unable to read header", fh.name)
try:
newAggregationType = struct.pack( longFormat, aggregationMethodToType[aggregationMethod] )
except KeyError:
raise InvalidAggregationMethod("Unrecognized aggregation method: %s" %
aggregationMethod)
fh.seek(0)
fh.write(newAggregationType)
if AUTOFLUSH:
fh.flush()
os.fsync(fh.fileno())
if CACHE_HEADERS and fh.name in __headerCache:
del __headerCache[fh.name]
return aggregationTypeToMethod.get(aggregationType, 'average')