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..bead441a 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,13 @@ def _json_body_(cls): "fields": {}, "tags": {}, } + 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: + cls._fields.remove("timestamp") for field in cls._fields: json_point['fields'][field] = point.__dict__[field]
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: