From b90968ccc9bbf84edfdf343274e881f1939d2c5a Mon Sep 17 00:00:00 2001 From: jemassey Date: Fri, 17 Jun 2016 09:53:00 -0400 Subject: [PATCH] Change to counter type sum calculation --- dev-requirements.txt | 8 +++++++- libs/statsd/counter.py | 15 +++++---------- tests/test_aggregation.py | 32 ++++++++++++++++---------------- tests/test_poster.py | 20 ++++++++++---------- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 9742403..835f2bb 100644 --- a/dev-requirements.txt +++ b/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 \ No newline at end of file +coveralls diff --git a/libs/statsd/counter.py b/libs/statsd/counter.py index 17e284d..dc5170e 100644 --- a/libs/statsd/counter.py +++ b/libs/statsd/counter.py @@ -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): diff --git a/tests/test_aggregation.py b/tests/test_aggregation.py index 33052f6..3c7675d 100755 --- a/tests/test_aggregation.py +++ b/tests/test_aggregation.py @@ -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): @@ -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): @@ -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: diff --git a/tests/test_poster.py b/tests/test_poster.py index a1b5d43..bd32d11 100755 --- a/tests/test_poster.py +++ b/tests/test_poster.py @@ -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', @@ -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,