3
3
Helper class for InfluxDB
4
4
"""
5
5
from collections import namedtuple , defaultdict
6
+ from warnings import warn
7
+
6
8
import six
7
9
10
+
8
11
class SeriesHelper (object ):
9
12
'''
10
13
Subclassing this helper eases writing data points in bulk.
11
14
All data points are immutable, insuring they do not get overwritten.
12
15
Each subclass can write to its own database.
13
16
The time series names can also be based on one or more defined fields.
14
-
17
+
15
18
Annotated example:
16
19
```
17
20
class MySeriesHelper(SeriesHelper):
18
21
class Meta:
19
22
# Meta class stores time series helper configuration.
20
- client = TestSeriesHelper.client
21
- # The client should be an instance of InfluxDBClient.
22
23
series_name = 'events.stats.{server_name}'
23
24
# The series name must be a string. Add dependent field names in curly brackets.
24
25
fields = ['time', 'server_name']
25
26
# Defines all the fields in this time series.
27
+ ### Following attributes are optional. ###
28
+ client = TestSeriesHelper.client
29
+ # The client should be an instance of InfluxDBClient. Only used if autocommit is True.
26
30
bulk_size = 5
27
- # Defines the number of data points to store prior to writing on the wire.
31
+ # Defines the number of data points to store prior to writing on the wire. Defaults to 1.
28
32
autocommit = True
29
- # Sets autocommit: must be set to True for bulk_size to have any affect.
30
-
33
+ # Sets autocommit: must be set to True for bulk_size to have any affect. Defaults to False.
34
+
31
35
# The following will create *five* (immutable) data points.
32
36
# Since bulk_size is set to 5, upon the fifth construction call, *all* data
33
37
# points will be written on the wire via MySeriesHelper.Meta.client.
@@ -36,11 +40,11 @@ class Meta:
36
40
MySeriesHelper(server_name='us.east-1', time=157)
37
41
MySeriesHelper(server_name='us.east-1', time=156)
38
42
MySeriesHelper(server_name='us.east-1', time=155)
39
-
43
+
40
44
# If autocommit unset (or set to False), one must call commit to write datapoints.
41
45
# To manually submit data points which are not yet written, call commit:
42
46
MySeriesHelper.commit()
43
-
47
+
44
48
# To inspect the JSON which will be written, call _json_body_():
45
49
MySeriesHelper._json_body_()
46
50
```
@@ -50,22 +54,38 @@ class Meta:
50
54
def __new__ (cls , * args , ** kwargs ):
51
55
'''
52
56
Initializes class attributes for subsequent constructor calls.
57
+ :note: *args and **kwargs are not explicitly used in this function,
58
+ but needed for Python 2 compatibility.
53
59
'''
54
60
if not cls .__initialized__ :
55
61
cls .__initialized__ = True
56
62
try :
57
63
_meta = getattr (cls , 'Meta' )
58
64
except AttributeError :
59
65
raise AttributeError ('Missing Meta class in {}.' .format (cls .__name__ ))
60
-
61
- for attr in ['series_name' , 'fields' , 'client' ]:
66
+
67
+ for attr in ['series_name' , 'fields' ]:
62
68
try :
63
69
setattr (cls , '_' + attr , getattr (_meta , attr ))
64
70
except AttributeError :
65
71
raise AttributeError ('Missing {} in {} Meta class.' .format (attr , cls .__name__ ))
66
-
72
+
67
73
cls ._autocommit = getattr (_meta , 'autocommit' , False )
68
- cls ._bulk_size = getattr (_meta , 'bulk_size' , 1 )
74
+
75
+ cls ._client = getattr (_meta , 'client' , None )
76
+ if cls ._autocommit and not cls ._client :
77
+ raise AttributeError ('In {}, autocommit is set to True, but no client is set.' .format (cls .__name__ ))
78
+
79
+ try :
80
+ cls ._bulk_size = getattr (_meta , 'bulk_size' )
81
+ if cls ._bulk_size < 1 and cls ._autocommit :
82
+ warn ('Definition of bulk_size in {} forced to 1, was less than 1.' .format (cls .__name__ ))
83
+ cls ._bulk_size = 1
84
+ except AttributeError :
85
+ cls ._bulk_size = - 1
86
+ else :
87
+ if not cls ._autocommit :
88
+ warn ('Definition of bulk_size in {} has no affect because autocommit is false.' .format (cls .__name__ ))
69
89
70
90
cls ._datapoints = defaultdict (list )
71
91
cls ._type = namedtuple (cls .__name__ , cls ._fields )
@@ -76,7 +96,7 @@ def __init__(self, **kw):
76
96
'''
77
97
Constructor call creates a new data point. All fields must be present.
78
98
:note: Data points written when `bulk_size` is reached per Helper.
79
- :warning: Data points are *immutable* (`namedtuples`).
99
+ :warning: Data points are *immutable* (`namedtuples`).
80
100
'''
81
101
cls = self .__class__
82
102
@@ -89,12 +109,16 @@ def __init__(self, **kw):
89
109
cls .commit ()
90
110
91
111
@classmethod
92
- def commit (cls ):
112
+ def commit (cls , client = None ):
93
113
'''
94
114
Commit everything from datapoints via the client.
115
+ :param client: InfluxDBClient instance for writing points to InfluxDB.
116
+ :attention: if a client is provided, it will supersede the one defined in the Helper.
95
117
:return result of client.write_points.
96
118
'''
97
- rtn = cls ._client .write_points (cls ._json_body_ ())
119
+ if not client :
120
+ client = cls ._client
121
+ rtn = client .write_points (cls ._json_body_ ())
98
122
cls ._reset_ ()
99
123
return rtn
100
124
0 commit comments