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

Commit 4799c58

Browse files
authored
feat(line_protocol): split out make_line function from core make_lines (#810)
* feat(line_protocol): split out make_line function from core make_lines * chore(line_protocol): fix malformed testcase
1 parent ad5e5b6 commit 4799c58

File tree

3 files changed

+75
-51
lines changed

3 files changed

+75
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2121
### Changed
2222
- Clean up stale CI config (#755)
2323
- Add legacy client test (#752 & #318 thx @oldmantaiter & @sebito91)
24+
- Update make_lines section in line_protocol.py to split out core function (#375 thx @aisbaa)
2425

2526
### Removed
2627

influxdb/line_protocol.py

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from pytz import UTC
1313
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
1515

1616
EPOCH = UTC.localize(datetime.utcfromtimestamp(0))
1717

@@ -30,15 +30,20 @@ def _convert_timestamp(timestamp, precision=None):
3030
ns = (timestamp - EPOCH).total_seconds() * 1e9
3131
if precision is None or precision == 'n':
3232
return ns
33-
elif precision == 'u':
33+
34+
if precision == 'u':
3435
return ns / 1e3
35-
elif precision == 'ms':
36+
37+
if precision == 'ms':
3638
return ns / 1e6
37-
elif precision == 's':
39+
40+
if precision == 's':
3841
return ns / 1e9
39-
elif precision == 'm':
42+
43+
if precision == 'm':
4044
return ns / 1e9 / 60
41-
elif precision == 'h':
45+
46+
if precision == 'h':
4247
return ns / 1e9 / 3600
4348

4449
raise ValueError(timestamp)
@@ -95,9 +100,11 @@ def _escape_value(value):
95100

96101
if isinstance(value, text_type) and value != '':
97102
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):
99105
return str(value) + 'i'
100-
elif _is_float(value):
106+
107+
if _is_float(value):
101108
return repr(value)
102109

103110
return str(value)
@@ -107,15 +114,60 @@ def _get_unicode(data, force=False):
107114
"""Try to return a text aka unicode object from the given data."""
108115
if isinstance(data, binary_type):
109116
return data.decode('utf-8')
110-
elif data is None:
117+
118+
if data is None:
111119
return ''
112-
elif force:
120+
121+
if force:
113122
if PY2:
114123
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
119171

120172

121173
def make_lines(data, precision=None):
@@ -127,48 +179,19 @@ def make_lines(data, precision=None):
127179
lines = []
128180
static_tags = data.get('tags')
129181
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
138182
if static_tags:
139183
tags = dict(static_tags) # make a copy, since we'll modify
140184
tags.update(point.get('tags') or {})
141185
else:
142186
tags = point.get('tags') or {}
143187

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+
)
172195
lines.append(line)
173196

174197
return '\n'.join(lines) + '\n'

influxdb/tests/test_line_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_make_lines(self):
4242

4343
self.assertEqual(
4444
line_protocol.make_lines(data),
45-
'test,backslash_tag=C:\\\\ ,integer_tag=2,string_tag=hello '
45+
'test,backslash_tag=C:\\\\,integer_tag=2,string_tag=hello '
4646
'bool_val=True,float_val=1.1,int_val=1i,string_val="hello!"\n'
4747
)
4848

0 commit comments

Comments
 (0)