Skip to content

Commit c2492eb

Browse files
author
aviau
committed
SeriesHelper documentation improvements
1 parent 3b468ff commit c2492eb

File tree

4 files changed

+81
-49
lines changed

4 files changed

+81
-49
lines changed

docs/source/api-documentation.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ These clients are initiated in the same way as the
5454
.. autoclass:: influxdb.DataFrameClient
5555
:members:
5656
:undoc-members:
57+
58+
-----------------------
59+
:class:`SeriesHelper`
60+
-----------------------
61+
62+
63+
.. currentmodule:: influxdb.SeriesHelper
64+
.. autoclass:: influxdb.SeriesHelper
65+
:members:
66+
:undoc-members:

docs/source/examples.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ Tutorials - pandas
1919

2020
.. literalinclude:: ../../examples/tutorial_pandas.py
2121
:language: python
22-
:linenos:
22+
23+
Tutorials - SeriesHelper
24+
========================
25+
26+
.. literalinclude:: ../../examples/tutorial_serieshelper.py
27+
:language: python

examples/tutorial_serieshelper.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from influxdb import InfluxDBClient
2+
from influxdb import SeriesHelper
3+
4+
5+
class MySeriesHelper(SeriesHelper):
6+
class Meta:
7+
# Meta class stores time series helper configuration.
8+
client = InfluxDBClient()
9+
# The client should be an instance of InfluxDBClient.
10+
series_name = 'events.stats.{server_name}'
11+
# The series name must be a string. Add dependent field names in curly brackets.
12+
fields = ['time', 'server_name']
13+
# Defines all the fields in this time series.
14+
bulk_size = 5
15+
# Defines the number of data points to store prior to writing on the wire.
16+
17+
# The following will create *five* (immutable) data points.
18+
# Since bulk_size is set to 5, upon the fifth construction call, *all* data
19+
# points will be written on the wire via MySeriesHelper.Meta.client.
20+
MySeriesHelper(server_name='us.east-1', time=159)
21+
MySeriesHelper(server_name='us.east-1', time=158)
22+
MySeriesHelper(server_name='us.east-1', time=157)
23+
MySeriesHelper(server_name='us.east-1', time=156)
24+
MySeriesHelper(server_name='us.east-1', time=155)
25+
26+
# To manually submit data points which are not yet written, call commit:
27+
MySeriesHelper.commit()
28+
29+
# To inspect the JSON which will be written, call _json_body_():
30+
MySeriesHelper._json_body_()

influxdb/helper.py

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,41 @@
1010

1111
class SeriesHelper(object):
1212

13-
'''
13+
"""
1414
Subclassing this helper eases writing data points in bulk.
1515
All data points are immutable, insuring they do not get overwritten.
1616
Each subclass can write to its own database.
1717
The time series names can also be based on one or more defined fields.
1818
19-
Annotated example:
20-
```
21-
class MySeriesHelper(SeriesHelper):
22-
class Meta:
23-
# Meta class stores time series helper configuration.
24-
series_name = 'events.stats.{server_name}'
25-
# Series name must be a string, curly brackets for dynamic use.
26-
fields = ['time', 'server_name']
27-
# Defines all the fields in this time series.
28-
### Following attributes are optional. ###
29-
client = TestSeriesHelper.client
30-
# Client should be an instance of InfluxDBClient.
31-
:warning: Only used if autocommit is True.
32-
bulk_size = 5
33-
# Defines the number of data points to write simultaneously.
34-
:warning: Only applicable if autocommit is True.
35-
autocommit = True
36-
# If True and no bulk_size, then will set bulk_size to 1.
37-
38-
# The following will create *five* (immutable) data points.
39-
# Since bulk_size is set to 5, upon the fifth construction call, all data
40-
# points will be written on the wire via MySeriesHelper.Meta.client.
41-
MySeriesHelper(server_name='us.east-1', time=159)
42-
MySeriesHelper(server_name='us.east-1', time=158)
43-
MySeriesHelper(server_name='us.east-1', time=157)
44-
MySeriesHelper(server_name='us.east-1', time=156)
45-
MySeriesHelper(server_name='us.east-1', time=155)
46-
47-
# If autocommit None or False, one must call commit to write datapoints.
48-
# To manually submit data points which are not yet written, call commit:
49-
MySeriesHelper.commit()
50-
51-
# To inspect the JSON which will be written, call _json_body_():
52-
MySeriesHelper._json_body_()
53-
```
54-
'''
19+
Annotated example::
20+
21+
class MySeriesHelper(SeriesHelper):
22+
class Meta:
23+
# Meta class stores time series helper configuration.
24+
series_name = 'events.stats.{server_name}'
25+
# Series name must be a string, curly brackets for dynamic use.
26+
fields = ['time', 'server_name']
27+
# Defines all the fields in this time series.
28+
### Following attributes are optional. ###
29+
client = TestSeriesHelper.client
30+
# Client should be an instance of InfluxDBClient.
31+
:warning: Only used if autocommit is True.
32+
bulk_size = 5
33+
# Defines the number of data points to write simultaneously.
34+
# Only applicable if autocommit is True.
35+
autocommit = True
36+
# If True and no bulk_size, then will set bulk_size to 1.
37+
38+
"""
5539
__initialized__ = False
5640

5741
def __new__(cls, *args, **kwargs):
58-
'''
42+
"""
5943
Initializes class attributes for subsequent constructor calls.
44+
6045
:note: *args and **kwargs are not explicitly used in this function,
6146
but needed for Python 2 compatibility.
62-
'''
47+
"""
6348
if not cls.__initialized__:
6449
cls.__initialized__ = True
6550
try:
@@ -107,11 +92,12 @@ def __new__(cls, *args, **kwargs):
10792
return super(SeriesHelper, cls).__new__(cls)
10893

10994
def __init__(self, **kw):
110-
'''
95+
"""
11196
Constructor call creates a new data point. All fields must be present.
97+
11298
:note: Data points written when `bulk_size` is reached per Helper.
11399
:warning: Data points are *immutable* (`namedtuples`).
114-
'''
100+
"""
115101
cls = self.__class__
116102

117103
if sorted(cls._fields) != sorted(kw.keys()):
@@ -127,12 +113,13 @@ def __init__(self, **kw):
127113

128114
@classmethod
129115
def commit(cls, client=None):
130-
'''
116+
"""
131117
Commit everything from datapoints via the client.
118+
132119
:param client: InfluxDBClient instance for writing points to InfluxDB.
133120
:attention: any provided client will supersede the class client.
134-
:return result of client.write_points.
135-
'''
121+
:return: result of client.write_points.
122+
"""
136123
if not client:
137124
client = cls._client
138125
rtn = client.write_points(cls._json_body_())
@@ -141,9 +128,9 @@ def commit(cls, client=None):
141128

142129
@classmethod
143130
def _json_body_(cls):
144-
'''
131+
"""
145132
:return: JSON body of these datapoints.
146-
'''
133+
"""
147134
json = []
148135
for series_name, data in six.iteritems(cls._datapoints):
149136
json.append({'name': series_name,
@@ -155,7 +142,7 @@ def _json_body_(cls):
155142

156143
@classmethod
157144
def _reset_(cls):
158-
'''
145+
"""
159146
Reset data storage.
160-
'''
147+
"""
161148
cls._datapoints = defaultdict(list)

0 commit comments

Comments
 (0)