10
10
11
11
class SeriesHelper (object ):
12
12
13
- '''
13
+ """
14
14
Subclassing this helper eases writing data points in bulk.
15
15
All data points are immutable, insuring they do not get overwritten.
16
16
Each subclass can write to its own database.
17
17
The time series names can also be based on one or more defined fields.
18
18
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
+ """
55
39
__initialized__ = False
56
40
57
41
def __new__ (cls , * args , ** kwargs ):
58
- '''
42
+ """
59
43
Initializes class attributes for subsequent constructor calls.
44
+
60
45
:note: *args and **kwargs are not explicitly used in this function,
61
46
but needed for Python 2 compatibility.
62
- '''
47
+ """
63
48
if not cls .__initialized__ :
64
49
cls .__initialized__ = True
65
50
try :
@@ -107,11 +92,12 @@ def __new__(cls, *args, **kwargs):
107
92
return super (SeriesHelper , cls ).__new__ (cls )
108
93
109
94
def __init__ (self , ** kw ):
110
- '''
95
+ """
111
96
Constructor call creates a new data point. All fields must be present.
97
+
112
98
:note: Data points written when `bulk_size` is reached per Helper.
113
99
:warning: Data points are *immutable* (`namedtuples`).
114
- '''
100
+ """
115
101
cls = self .__class__
116
102
117
103
if sorted (cls ._fields ) != sorted (kw .keys ()):
@@ -127,12 +113,13 @@ def __init__(self, **kw):
127
113
128
114
@classmethod
129
115
def commit (cls , client = None ):
130
- '''
116
+ """
131
117
Commit everything from datapoints via the client.
118
+
132
119
:param client: InfluxDBClient instance for writing points to InfluxDB.
133
120
: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
+ """
136
123
if not client :
137
124
client = cls ._client
138
125
rtn = client .write_points (cls ._json_body_ ())
@@ -141,9 +128,9 @@ def commit(cls, client=None):
141
128
142
129
@classmethod
143
130
def _json_body_ (cls ):
144
- '''
131
+ """
145
132
:return: JSON body of these datapoints.
146
- '''
133
+ """
147
134
json = []
148
135
for series_name , data in six .iteritems (cls ._datapoints ):
149
136
json .append ({'name' : series_name ,
@@ -155,7 +142,7 @@ def _json_body_(cls):
155
142
156
143
@classmethod
157
144
def _reset_ (cls ):
158
- '''
145
+ """
159
146
Reset data storage.
160
- '''
147
+ """
161
148
cls ._datapoints = defaultdict (list )
0 commit comments