From 0356059e15ad86198a56642a1af251265ae24111 Mon Sep 17 00:00:00 2001 From: csanz91 Date: Sat, 8 Jun 2019 19:05:51 +0200 Subject: [PATCH 1/7] Allow specify a retention policy in SeriesHelper --- influxdb/helper.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/influxdb/helper.py b/influxdb/helper.py index e622526d..5408c3a7 100644 --- a/influxdb/helper.py +++ b/influxdb/helper.py @@ -72,6 +72,8 @@ def __new__(cls, *args, **kwargs): cls._autocommit = getattr(_meta, 'autocommit', False) + cls._retention_policy = getattr(_meta, 'retention_policy', None) + cls._client = getattr(_meta, 'client', None) if cls._autocommit and not cls._client: raise AttributeError( @@ -143,7 +145,7 @@ def commit(cls, client=None): """ if not client: client = cls._client - rtn = client.write_points(cls._json_body_()) + rtn = client.write_points(cls._json_body_(), retention_policy=cls._retention_policy) cls._reset_() return rtn From fa5da8a1751419eb3faa628de931f44d29d9091e Mon Sep 17 00:00:00 2001 From: csanz91 Date: Sat, 8 Jun 2019 19:25:09 +0200 Subject: [PATCH 2/7] Complete the annotation example with the retention policy --- influxdb/helper.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/influxdb/helper.py b/influxdb/helper.py index 5408c3a7..4163434f 100644 --- a/influxdb/helper.py +++ b/influxdb/helper.py @@ -41,6 +41,8 @@ class Meta: # Only applicable if autocommit is True. autocommit = True # If True and no bulk_size, then will set bulk_size to 1. + # Specify the retention policy for the data points + retention_policy = 'your_retention_policy' """ From c314b82989457c70572f2f1b4364efc8e25cf2dd Mon Sep 17 00:00:00 2001 From: Cesar Sanz Date: Sun, 15 Sep 2019 19:40:04 +0200 Subject: [PATCH 3/7] Fix formatting --- influxdb/helper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/influxdb/helper.py b/influxdb/helper.py index 4163434f..55e43b7a 100644 --- a/influxdb/helper.py +++ b/influxdb/helper.py @@ -147,7 +147,8 @@ def commit(cls, client=None): """ if not client: client = cls._client - rtn = client.write_points(cls._json_body_(), retention_policy=cls._retention_policy) + rtn = client.write_points(cls._json_body_(), \ + retention_policy=cls._retention_policy) cls._reset_() return rtn From 001c7b8ae78281e63a4714a0c096685c384e65f0 Mon Sep 17 00:00:00 2001 From: Cesar Sanz Date: Sun, 15 Sep 2019 20:03:46 +0200 Subject: [PATCH 4/7] Fix formatting again --- influxdb/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb/helper.py b/influxdb/helper.py index 55e43b7a..996d32ae 100644 --- a/influxdb/helper.py +++ b/influxdb/helper.py @@ -147,7 +147,7 @@ def commit(cls, client=None): """ if not client: client = cls._client - rtn = client.write_points(cls._json_body_(), \ + rtn = client.write_points(cls._json_body_(), retention_policy=cls._retention_policy) cls._reset_() return rtn From f0c8b6841b5021b119ef2abc252a9038dd86a518 Mon Sep 17 00:00:00 2001 From: csanz91 Date: Sun, 29 Sep 2019 20:09:46 +0200 Subject: [PATCH 5/7] Add helper write with retention policy --- influxdb/tests/helper_test.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/influxdb/tests/helper_test.py b/influxdb/tests/helper_test.py index 6f24e85d..6c9167e0 100644 --- a/influxdb/tests/helper_test.py +++ b/influxdb/tests/helper_test.py @@ -365,3 +365,31 @@ class Meta: .format(WarnBulkSizeNoEffect)) self.assertIn('has no affect', str(w[-1].message), 'Warning message did not contain "has not affect".') + + def testSeriesWithRetentionPolicy(self): + """Test that the data is saved with the specified retention policy.""" + + my_policy = 'my_policy' + + class RetentionPolicySeriesHelper(SeriesHelper): + + class Meta: + client = InfluxDBClient() + series_name = 'events.stats.{server_name}' + fields = ['some_stat', 'time'] + tags = ['server_name', 'other_tag'] + bulk_size = 2 + autocommit = True + retention_policy = my_policy + + fake_write_points = mock.MagicMock() + RetentionPolicySeriesHelper( + server_name='us.east-1', some_stat=159, other_tag='gg') + RetentionPolicySeriesHelper._client.write_points = fake_write_points + RetentionPolicySeriesHelper( + server_name='us.east-1', some_stat=158, other_tag='aa') + + kall = fake_write_points.call_args + args, kwargs = kall + self.assertTrue('retention_policy' in kwargs) + self.assertEqual(kwargs['retention_policy'], my_policy) From 3334cf9af695d6617f4e0cbe99417ce86dd3166c Mon Sep 17 00:00:00 2001 From: csanz91 Date: Sun, 29 Sep 2019 20:11:02 +0200 Subject: [PATCH 6/7] Add helper write without retention policy --- influxdb/tests/helper_test.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/influxdb/tests/helper_test.py b/influxdb/tests/helper_test.py index 6c9167e0..6e186067 100644 --- a/influxdb/tests/helper_test.py +++ b/influxdb/tests/helper_test.py @@ -393,3 +393,28 @@ class Meta: args, kwargs = kall self.assertTrue('retention_policy' in kwargs) self.assertEqual(kwargs['retention_policy'], my_policy) + + def testSeriesWithoutRetentionPolicy(self): + """Test that the data is saved without any retention policy.""" + + class NoRetentionPolicySeriesHelper(SeriesHelper): + + class Meta: + client = InfluxDBClient() + series_name = 'events.stats.{server_name}' + fields = ['some_stat', 'time'] + tags = ['server_name', 'other_tag'] + bulk_size = 2 + autocommit = True + + fake_write_points = mock.MagicMock() + NoRetentionPolicySeriesHelper( + server_name='us.east-1', some_stat=159, other_tag='gg') + NoRetentionPolicySeriesHelper._client.write_points = fake_write_points + NoRetentionPolicySeriesHelper( + server_name='us.east-1', some_stat=158, other_tag='aa') + + kall = fake_write_points.call_args + args, kwargs = kall + self.assertTrue('retention_policy' in kwargs) + self.assertEqual(kwargs['retention_policy'], None) From 2a64a0ea4fbec02777b8900c72be860650f9d5b3 Mon Sep 17 00:00:00 2001 From: csanz91 Date: Sun, 29 Sep 2019 20:42:04 +0200 Subject: [PATCH 7/7] Remove blank line after the docstring. --- influxdb/tests/helper_test.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/influxdb/tests/helper_test.py b/influxdb/tests/helper_test.py index 6e186067..4ee71050 100644 --- a/influxdb/tests/helper_test.py +++ b/influxdb/tests/helper_test.py @@ -368,7 +368,6 @@ class Meta: def testSeriesWithRetentionPolicy(self): """Test that the data is saved with the specified retention policy.""" - my_policy = 'my_policy' class RetentionPolicySeriesHelper(SeriesHelper): @@ -396,7 +395,6 @@ class Meta: def testSeriesWithoutRetentionPolicy(self): """Test that the data is saved without any retention policy.""" - class NoRetentionPolicySeriesHelper(SeriesHelper): class Meta: