11
11
12
12
from pytz import UTC
13
13
from dateutil .parser import parse
14
- from six import iteritems , binary_type , text_type , integer_types , PY2
14
+ from six import binary_type , text_type , integer_types , PY2
15
15
16
16
EPOCH = UTC .localize (datetime .utcfromtimestamp (0 ))
17
17
@@ -30,15 +30,20 @@ def _convert_timestamp(timestamp, precision=None):
30
30
ns = (timestamp - EPOCH ).total_seconds () * 1e9
31
31
if precision is None or precision == 'n' :
32
32
return ns
33
- elif precision == 'u' :
33
+
34
+ if precision == 'u' :
34
35
return ns / 1e3
35
- elif precision == 'ms' :
36
+
37
+ if precision == 'ms' :
36
38
return ns / 1e6
37
- elif precision == 's' :
39
+
40
+ if precision == 's' :
38
41
return ns / 1e9
39
- elif precision == 'm' :
42
+
43
+ if precision == 'm' :
40
44
return ns / 1e9 / 60
41
- elif precision == 'h' :
45
+
46
+ if precision == 'h' :
42
47
return ns / 1e9 / 3600
43
48
44
49
raise ValueError (timestamp )
@@ -95,9 +100,11 @@ def _escape_value(value):
95
100
96
101
if isinstance (value , text_type ) and value != '' :
97
102
return quote_ident (value )
98
- elif isinstance (value , integer_types ) and not isinstance (value , bool ):
103
+
104
+ if isinstance (value , integer_types ) and not isinstance (value , bool ):
99
105
return str (value ) + 'i'
100
- elif _is_float (value ):
106
+
107
+ if _is_float (value ):
101
108
return repr (value )
102
109
103
110
return str (value )
@@ -107,15 +114,60 @@ def _get_unicode(data, force=False):
107
114
"""Try to return a text aka unicode object from the given data."""
108
115
if isinstance (data , binary_type ):
109
116
return data .decode ('utf-8' )
110
- elif data is None :
117
+
118
+ if data is None :
111
119
return ''
112
- elif force :
120
+
121
+ if force :
113
122
if PY2 :
114
123
return unicode (data )
115
- else :
116
- return str (data )
117
- else :
118
- return data
124
+ return str (data )
125
+
126
+ return data
127
+
128
+
129
+ def make_line (measurement , tags = None , fields = None , time = None , precision = None ):
130
+ """Extract the actual point from a given measurement line."""
131
+ tags = tags or {}
132
+ fields = fields or {}
133
+
134
+ line = _escape_tag (_get_unicode (measurement ))
135
+
136
+ # tags should be sorted client-side to take load off server
137
+ tag_list = []
138
+ for tag_key in sorted (tags .keys ()):
139
+ key = _escape_tag (tag_key )
140
+ value = _escape_tag (tags [tag_key ])
141
+
142
+ if key != '' and value != '' :
143
+ tag_list .append (
144
+ "{key}={value}" .format (key = key , value = value )
145
+ )
146
+
147
+ if tag_list :
148
+ line += ',' + ',' .join (tag_list )
149
+
150
+ field_list = []
151
+ for field_key in sorted (fields .keys ()):
152
+ key = _escape_tag (field_key )
153
+ value = _escape_value (fields [field_key ])
154
+
155
+ if key != '' and value != '' :
156
+ field_list .append ("{key}={value}" .format (
157
+ key = key ,
158
+ value = value
159
+ ))
160
+
161
+ if field_list :
162
+ line += ' ' + ',' .join (field_list )
163
+
164
+ if time is not None :
165
+ timestamp = _get_unicode (str (int (
166
+ _convert_timestamp (time , precision )
167
+ )))
168
+ line += ' ' + timestamp
169
+
170
+ return line
119
171
120
172
121
173
def make_lines (data , precision = None ):
@@ -127,48 +179,19 @@ def make_lines(data, precision=None):
127
179
lines = []
128
180
static_tags = data .get ('tags' )
129
181
for point in data ['points' ]:
130
- elements = []
131
-
132
- # add measurement name
133
- measurement = _escape_tag (_get_unicode (
134
- point .get ('measurement' , data .get ('measurement' ))))
135
- key_values = [measurement ]
136
-
137
- # add tags
138
182
if static_tags :
139
183
tags = dict (static_tags ) # make a copy, since we'll modify
140
184
tags .update (point .get ('tags' ) or {})
141
185
else :
142
186
tags = point .get ('tags' ) or {}
143
187
144
- # tags should be sorted client-side to take load off server
145
- for tag_key , tag_value in sorted (iteritems (tags )):
146
- key = _escape_tag (tag_key )
147
- value = _escape_tag_value (tag_value )
148
-
149
- if key != '' and value != '' :
150
- key_values .append (key + "=" + value )
151
-
152
- elements .append (',' .join (key_values ))
153
-
154
- # add fields
155
- field_values = []
156
- for field_key , field_value in sorted (iteritems (point ['fields' ])):
157
- key = _escape_tag (field_key )
158
- value = _escape_value (field_value )
159
-
160
- if key != '' and value != '' :
161
- field_values .append (key + "=" + value )
162
-
163
- elements .append (',' .join (field_values ))
164
-
165
- # add timestamp
166
- if 'time' in point :
167
- timestamp = _get_unicode (str (int (
168
- _convert_timestamp (point ['time' ], precision ))))
169
- elements .append (timestamp )
170
-
171
- line = ' ' .join (elements )
188
+ line = make_line (
189
+ point .get ('measurement' , data .get ('measurement' )),
190
+ tags = tags ,
191
+ fields = point .get ('fields' ),
192
+ precision = precision ,
193
+ time = point .get ('time' )
194
+ )
172
195
lines .append (line )
173
196
174
197
return '\n ' .join (lines ) + '\n '
0 commit comments