From a580fb2108b3ac656d9d0a756b2483c2e951ef2b Mon Sep 17 00:00:00 2001 From: areski Date: Mon, 23 Mar 2015 16:13:40 +0100 Subject: [PATCH 1/2] add custom timestamp to SeriesHelper --- examples/tutorial_serieshelper.py | 43 ++++++++++++++++++++++++++++++- influxdb/helper.py | 8 ++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/examples/tutorial_serieshelper.py b/examples/tutorial_serieshelper.py index d7bd27c9..635739dc 100644 --- a/examples/tutorial_serieshelper.py +++ b/examples/tutorial_serieshelper.py @@ -39,7 +39,7 @@ class Meta: # The following will create *five* (immutable) data points. # Since bulk_size is set to 5, upon the fifth construction call, *all* data # points will be written on the wire via MySeriesHelper.Meta.client. -MySeriesHelper(server_name='us.east-1', some_stat=159, other_stat=10) +MySeriesHelper(server_name='us.east-1', some_stat=88, other_stat=10) MySeriesHelper(server_name='us.east-1', some_stat=158, other_stat=20) MySeriesHelper(server_name='us.east-1', some_stat=157, other_stat=30) MySeriesHelper(server_name='us.east-1', some_stat=156, other_stat=40) @@ -50,3 +50,44 @@ class Meta: # To inspect the JSON which will be written, call _json_body_(): MySeriesHelper._json_body_() + + +# +# Different example allowing to set the timestamp +# +class MySeriesTimestampHelper(SeriesHelper): + # Meta class stores time series helper configuration. + class Meta: + # The client should be an instance of InfluxDBClient. + client = myclient + # The series name must be a string. Add dependent fields/tags in curly brackets. + series_name = 'events.stats.{server_name}' + # Defines all the fields in this time series. + fields = ['some_stat', 'other_stat', 'timestamp'] + # Defines all the tags for the series. + tags = ['server_name'] + # Defines the number of data points to store prior to writing on the wire. + bulk_size = 5 + # autocommit must be set to True when using bulk_size + autocommit = True + + +# The following will create *five* (immutable) data points. +# Since bulk_size is set to 5, upon the fifth construction call, *all* data +# points will be written on the wire via MySeriesHelper.Meta.client. +MySeriesTimestampHelper(server_name='us.east-1', some_stat=88, other_stat=10, + timestamp="2015-03-11T18:00:24.017486904Z") +MySeriesTimestampHelper(server_name='us.east-1', some_stat=158, other_stat=20, + timestamp="2015-03-11T18:00:24.017486994Z") +MySeriesTimestampHelper(server_name='us.east-1', some_stat=157, other_stat=30, + timestamp="2015-03-11T18:00:24.017487080Z") +MySeriesTimestampHelper(server_name='us.east-1', some_stat=156, other_stat=40, + timestamp="2015-03-11T18:00:24.017487305Z") +MySeriesTimestampHelper(server_name='us.east-1', some_stat=155, other_stat=50, + timestamp="2015-03-11T18:00:24.017487512Z") + +# To manually submit data points which are not yet written, call commit: +MySeriesTimestampHelper.commit() + +# To inspect the JSON which will be written, call _json_body_(): +MySeriesTimestampHelper._json_body_() diff --git a/influxdb/helper.py b/influxdb/helper.py index 7e86578b..3ef3ed30 100644 --- a/influxdb/helper.py +++ b/influxdb/helper.py @@ -16,6 +16,9 @@ class SeriesHelper(object): Each subclass can write to its own database. The time series names can also be based on one or more defined fields. + A field "timestamp" can be used to write data points at a specific time, + rather than the default current time. + Annotated example:: class MySeriesHelper(SeriesHelper): @@ -141,6 +144,11 @@ def _json_body_(cls): "fields": {}, "tags": {}, } + if 'timestamp' in point.__dict__ and point.__dict__['timestamp']: + json_point["timestamp"] = point.__dict__['timestamp'] + # remove timestamp from fields + if "timestamp" in cls._fields: + cls._fields.remove("timestamp") for field in cls._fields: json_point['fields'][field] = point.__dict__[field] From 9a5eadc925cd2a8401df05653909b127c8acd91d Mon Sep 17 00:00:00 2001 From: areski Date: Tue, 25 Aug 2015 09:20:30 +0200 Subject: [PATCH 2/2] fix flake8 issue --- influxdb/helper.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/influxdb/helper.py b/influxdb/helper.py index 3ef3ed30..bead441a 100644 --- a/influxdb/helper.py +++ b/influxdb/helper.py @@ -144,7 +144,9 @@ def _json_body_(cls): "fields": {}, "tags": {}, } - if 'timestamp' in point.__dict__ and point.__dict__['timestamp']: + if 'timestamp' in point.__dict__ \ + and point.__dict__['timestamp']: + # keep the timestamp in json_point json_point["timestamp"] = point.__dict__['timestamp'] # remove timestamp from fields if "timestamp" in cls._fields: