Skip to content

Commit

Permalink
Change to counter type sum calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jemassey committed Jun 17, 2016
1 parent f6488e4 commit b90968c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
8 changes: 7 additions & 1 deletion dev-requirements.txt
@@ -1,6 +1,12 @@
wheel>=0.23.0
docopt
netuitive>=0.1.0
configobj
setproctitle
docutils
tox
tox-pyenv
mock
flake8
coverage
coveralls
coveralls
15 changes: 5 additions & 10 deletions libs/statsd/counter.py
Expand Up @@ -32,18 +32,13 @@ def add_value(self, value, ts, rate=None, sign=None):
if rate is None:
rate = 1.0

if sign is None:
self.value += float(value * float(rate))

if sign == '+':
self.signed = True
self.value += float(value * float(rate))

if sign == '-':
self.signed = True
self.value += float(float(-value) * float(rate))

self.samples.append(float(self.value * float(rate)))
self.samples.append(float(value) * float(rate))
elif sign == '-':
self.samples.append(float(-value) * float(rate))
else:
self.samples.append(float(value) * float(rate))

def get_values(self, timestamp):

Expand Down
32 changes: 16 additions & 16 deletions tests/test_aggregation.py
Expand Up @@ -153,16 +153,16 @@ def test_counter_aggregate_test1(self):
# the samples used to build the payload
samples_to_post = e.element.samples[0].__dict__

self.assertListEqual(samples, [10.0, 11.0, 21.0])
self.assertListEqual(samples, [10.0, 1.0, 10.0])
self.assertEqual(prepared_samples, [])
self.assertEqual(samples_to_post, {'avg': 14.0,
self.assertEqual(samples_to_post, {'avg': 21.0/3.0,
'cnt': 3.0,
'max': 21.0,
'max': 10.0,
'metricId': 'statsd.counter',
'min': 10.0,
'sum': 42.0,
'min': 1.0,
'sum': 21.0,
'timestamp': samples_to_post['timestamp'],
'val': 42.0})
'val': 21.0})

def test_counter_aggregate_test2(self):

Expand All @@ -183,16 +183,16 @@ def test_counter_aggregate_test2(self):
# the samples used to build the payload
samples_to_post = e.element.samples[0].__dict__

self.assertListEqual(samples, [-10.0, -9.0, 11.0])
self.assertListEqual(samples, [-10.0, 1.0, 20.0])
self.assertEqual(prepared_samples, [])
self.assertEqual(samples_to_post, {'avg': -2.6666666666666665,
self.assertEqual(samples_to_post, {'avg': 11.0/3.0,
'cnt': 3.0,
'max': 11.0,
'max': 20.0,
'metricId': 'statsd.counter',
'min': -10.0,
'sum': -8.0,
'sum': 11.0,
'timestamp': samples_to_post['timestamp'],
'val': -8.0})
'val': 11.0})

def test_counter_aggregate_test3(self):

Expand All @@ -213,16 +213,16 @@ def test_counter_aggregate_test3(self):
# the samples used to build the payload
samples_to_post = e.element.samples[0].__dict__

self.assertListEqual(samples, [2.0, 7.0, 10.0])
self.assertListEqual(samples, [2.0, 5.0, 3.0])
self.assertEqual(prepared_samples, [])
self.assertEqual(samples_to_post, {'avg': 6.333333333333333,
self.assertEqual(samples_to_post, {'avg': 10.0/3.0,
'cnt': 3.0,
'max': 10.0,
'max': 5.0,
'metricId': 'statsd.counter',
'min': 2.0,
'sum': 19.0,
'sum': 10.0,
'timestamp': samples_to_post['timestamp'],
'val': 19.0})
'val': 10.0})

def test_timer_aggregate(self):
with self.lock:
Expand Down
20 changes: 10 additions & 10 deletions tests/test_poster.py
Expand Up @@ -264,14 +264,14 @@ def test_single_counter_with_rate(self):
'unit': ''}],
'name': 'testelement',
'relations': [],
'samples': [{'avg': 0.010000000000000002,
'samples': [{'avg': 0.1,
'cnt': 1.0,
'max': 0.010000000000000002,
'max': 0.1,
'metricId': 'statsd.counterrate',
'min': 0.010000000000000002,
'sum': 0.010000000000000002,
'min': 0.1,
'sum': 0.1,
'timestamp': self.poster.elements.elements['testelement'].element.samples[0].timestamp,
'val': 0.010000000000000002}],
'val': 0.1}],
'tags': [],
'type': 'SERVER'},
'elementId': 'testelement',
Expand Down Expand Up @@ -584,14 +584,14 @@ def test_mutliple_metrics(self):
'sum': 333.0,
'timestamp': element.samples[0].timestamp,
'val': 333.0},
{'avg': 0.010000000000000002,
{'avg': 0.1,
'cnt': 1.0,
'max': 0.010000000000000002,
'max': 0.1,
'metricId': 'statsd.counterrate',
'min': 0.010000000000000002,
'sum': 0.010000000000000002,
'min': 0.1,
'sum': 0.1,
'timestamp': element.samples[0].timestamp,
'val': 0.010000000000000002},
'val': 0.1},
{'avg': 333.0,
'cnt': 1.0,
'max': 333.0,
Expand Down

0 comments on commit b90968c

Please sign in to comment.