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

Commit 308a913

Browse files
committed
Line protocol: handle integer values (Closes: #225)
1 parent 54f2a2b commit 308a913

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

influxdb/line_protocol.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def _convert_timestamp(timestamp, precision=None):
1313
if isinstance(timestamp, int):
1414
return timestamp # assume precision is correct if timestamp is int
15-
if isinstance(_force_text(timestamp), text_type):
15+
if isinstance(_get_unicode(timestamp), text_type):
1616
timestamp = parse(timestamp)
1717
if isinstance(timestamp, datetime):
1818
ns = (
@@ -32,6 +32,7 @@ def _convert_timestamp(timestamp, precision=None):
3232

3333

3434
def _escape_tag(tag):
35+
tag = _get_unicode(tag, force=True)
3536
return tag.replace(
3637
"\\", "\\\\"
3738
).replace(
@@ -44,21 +45,23 @@ def _escape_tag(tag):
4445

4546

4647
def _escape_value(value):
47-
value = _force_text(value)
48-
if isinstance(value, text_type):
48+
value = _get_unicode(value)
49+
if isinstance(value, text_type) and value != '':
4950
return "\"{}\"".format(value.replace(
5051
"\"", "\\\""
5152
))
5253
else:
5354
return str(value)
5455

5556

56-
def _force_text(data):
57+
def _get_unicode(data, force=False):
5758
"""
5859
Try to return a text aka unicode object from the given data.
5960
"""
6061
if isinstance(data, binary_type):
6162
return data.decode('utf-8')
63+
elif force:
64+
return str(data)
6265
else:
6366
return data
6467

@@ -74,7 +77,7 @@ def make_lines(data, precision=None):
7477
elements = []
7578

7679
# add measurement name
77-
measurement = _escape_tag(_force_text(
80+
measurement = _escape_tag(_get_unicode(
7881
point.get('measurement', data.get('measurement'))
7982
))
8083
key_values = [measurement]
@@ -90,6 +93,7 @@ def make_lines(data, precision=None):
9093
for tag_key in sorted(tags.keys()):
9194
key = _escape_tag(tag_key)
9295
value = _escape_tag(tags[tag_key])
96+
9397
if key != '' and value != '':
9498
key_values.append("{key}={value}".format(key=key, value=value))
9599
key_values = ','.join(key_values)
@@ -107,7 +111,7 @@ def make_lines(data, precision=None):
107111

108112
# add timestamp
109113
if 'time' in point:
110-
timestamp = _force_text(str(int(
114+
timestamp = _get_unicode(str(int(
111115
_convert_timestamp(point['time'], precision)
112116
)))
113117
elements.append(timestamp)

influxdb/tests/test_line_protocol.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66

77
class TestLineProtocol(unittest.TestCase):
88

9-
def test_empty_tag(self):
9+
def test_make_lines(self):
1010
data = {
1111
"tags": {
12-
"my_tag": ""
12+
"empty_tag": "",
13+
"integer_tag": 2,
14+
"string_tag": "hello"
1315
},
1416
"points": [
1517
{
1618
"measurement": "test",
1719
"fields": {
18-
"value": "hello!"
20+
"string_val": "hello!",
21+
"int_val": 1,
1922
}
2023
}
2124
]
2225
}
2326

2427
self.assertEqual(
2528
line_protocol.make_lines(data),
26-
'test value="hello!"\n'
29+
'test,integer_tag=2,string_tag=hello int_val=1,string_val="hello!"\n'
2730
)

0 commit comments

Comments
 (0)