Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

add custom timestamp to SeriesHelper #130

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion examples/tutorial_serieshelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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_()
10 changes: 10 additions & 0 deletions influxdb/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -141,6 +144,13 @@ def _json_body_(cls):
"fields": {},
"tags": {},
}
if 'timestamp' in point.__dict__ \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getattr(point, 'timestamp', None) would be more pythonic I suppose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do :

ts = getattr(point, 'timestamp', 0)
if ts:
    json_point['timestamp'] = ts
...

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]
Expand Down