From c5c9a48610bb587f25135f1d0758472c1fbe876c Mon Sep 17 00:00:00 2001 From: chenjiankun Date: Fri, 14 Oct 2016 09:38:54 +0000 Subject: influxdb_line_protocol.py should move to 3rd party dir JIRA: YARDSTICK-369 Change-Id: I286e883d88a47d2befa4bb523ad9384937e079b3 Signed-off-by: chenjiankun --- .../unit/dispatcher/test_influxdb_line_protocol.py | 2 +- third_party/__init__.py | 0 third_party/influxdb/__init__.py | 0 third_party/influxdb/influxdb_line_protocol.py | 137 +++++++++++++++++++++ yardstick/dispatcher/influxdb.py | 2 +- yardstick/dispatcher/influxdb_line_protocol.py | 137 --------------------- 6 files changed, 139 insertions(+), 139 deletions(-) create mode 100644 third_party/__init__.py create mode 100644 third_party/influxdb/__init__.py create mode 100644 third_party/influxdb/influxdb_line_protocol.py delete mode 100644 yardstick/dispatcher/influxdb_line_protocol.py diff --git a/tests/unit/dispatcher/test_influxdb_line_protocol.py b/tests/unit/dispatcher/test_influxdb_line_protocol.py index cb05bf4d2..42553c498 100644 --- a/tests/unit/dispatcher/test_influxdb_line_protocol.py +++ b/tests/unit/dispatcher/test_influxdb_line_protocol.py @@ -4,7 +4,7 @@ # influxdb-python/influxdb/tests/test_line_protocol.py import unittest -from yardstick.dispatcher.influxdb_line_protocol import make_lines +from third_party.influxdb.influxdb_line_protocol import make_lines class TestLineProtocol(unittest.TestCase): diff --git a/third_party/__init__.py b/third_party/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/third_party/influxdb/__init__.py b/third_party/influxdb/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/third_party/influxdb/influxdb_line_protocol.py b/third_party/influxdb/influxdb_line_protocol.py new file mode 100644 index 000000000..eee982163 --- /dev/null +++ b/third_party/influxdb/influxdb_line_protocol.py @@ -0,0 +1,137 @@ +# The MIT License (MIT) + +# Copyright (c) 2013 InfluxDB + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to +# do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +# yardstick comment: this file is a modified copy of +# influxdb-python/influxdb/line_protocol.py + +from __future__ import unicode_literals +from copy import copy + +from six import binary_type, text_type, integer_types + + +def _escape_tag(tag): + tag = _get_unicode(tag, force=True) + return tag.replace( + "\\", "\\\\" + ).replace( + " ", "\\ " + ).replace( + ",", "\\," + ).replace( + "=", "\\=" + ) + + +def _escape_value(value): + value = _get_unicode(value) + if isinstance(value, text_type) and value != '': + return "\"{}\"".format( + value.replace( + "\"", "\\\"" + ).replace( + "\n", "\\n" + ) + ) + elif isinstance(value, integer_types) and not isinstance(value, bool): + return str(value) + 'i' + else: + return str(value) + + +def _get_unicode(data, force=False): + """ + Try to return a text aka unicode object from the given data. + """ + if isinstance(data, binary_type): + return data.decode('utf-8') + elif data is None: + return '' + elif force: + return str(data) + else: + return data + + +def make_lines(data): + """ + Extracts the points from the given dict and returns a Unicode string + matching the line protocol introduced in InfluxDB 0.9.0. + + line protocol format: + [,=...] =\ + [,=...] [unix-nano-timestamp] + + Ref: + https://influxdb.com/docs/v0.9/write_protocols/write_syntax.html + https://influxdb.com/docs/v0.9/write_protocols/line.html + """ + lines = [] + static_tags = data.get('tags', None) + for point in data['points']: + elements = [] + + # add measurement name + measurement = _escape_tag(_get_unicode( + point.get('measurement', data.get('measurement')) + )) + key_values = [measurement] + + # add tags + if static_tags is None: + tags = point.get('tags', {}) + else: + tags = copy(static_tags) + tags.update(point.get('tags', {})) + + # tags should be sorted client-side to take load off server + for tag_key in sorted(tags.keys()): + key = _escape_tag(tag_key) + value = _escape_tag(tags[tag_key]) + + if key != '' and value != '': + key_values.append("{key}={value}".format(key=key, value=value)) + key_values = ','.join(key_values) + elements.append(key_values) + + # add fields + field_values = [] + for field_key in sorted(point['fields'].keys()): + key = _escape_tag(field_key) + value = _escape_value(point['fields'][field_key]) + if key != '' and value != '': + field_values.append("{key}={value}".format( + key=key, + value=value + )) + field_values = ','.join(field_values) + elements.append(field_values) + + # add timestamp + if 'time' in point: + elements.append(point['time']) + + line = ' '.join(elements) + lines.append(line) + lines = '\n'.join(lines) + return lines + '\n' diff --git a/yardstick/dispatcher/influxdb.py b/yardstick/dispatcher/influxdb.py index e431f2c1e..8673253b4 100644 --- a/yardstick/dispatcher/influxdb.py +++ b/yardstick/dispatcher/influxdb.py @@ -16,7 +16,7 @@ import time from oslo_config import cfg from yardstick.dispatcher.base import Base as DispatchBase -from yardstick.dispatcher.influxdb_line_protocol import make_lines +from third_party.influxdb.influxdb_line_protocol import make_lines LOG = logging.getLogger(__name__) diff --git a/yardstick/dispatcher/influxdb_line_protocol.py b/yardstick/dispatcher/influxdb_line_protocol.py deleted file mode 100644 index eee982163..000000000 --- a/yardstick/dispatcher/influxdb_line_protocol.py +++ /dev/null @@ -1,137 +0,0 @@ -# The MIT License (MIT) - -# Copyright (c) 2013 InfluxDB - -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in -# the Software without restriction, including without limitation the rights to -# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -# of the Software, and to permit persons to whom the Software is furnished to -# do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -# yardstick comment: this file is a modified copy of -# influxdb-python/influxdb/line_protocol.py - -from __future__ import unicode_literals -from copy import copy - -from six import binary_type, text_type, integer_types - - -def _escape_tag(tag): - tag = _get_unicode(tag, force=True) - return tag.replace( - "\\", "\\\\" - ).replace( - " ", "\\ " - ).replace( - ",", "\\," - ).replace( - "=", "\\=" - ) - - -def _escape_value(value): - value = _get_unicode(value) - if isinstance(value, text_type) and value != '': - return "\"{}\"".format( - value.replace( - "\"", "\\\"" - ).replace( - "\n", "\\n" - ) - ) - elif isinstance(value, integer_types) and not isinstance(value, bool): - return str(value) + 'i' - else: - return str(value) - - -def _get_unicode(data, force=False): - """ - Try to return a text aka unicode object from the given data. - """ - if isinstance(data, binary_type): - return data.decode('utf-8') - elif data is None: - return '' - elif force: - return str(data) - else: - return data - - -def make_lines(data): - """ - Extracts the points from the given dict and returns a Unicode string - matching the line protocol introduced in InfluxDB 0.9.0. - - line protocol format: - [,=...] =\ - [,=...] [unix-nano-timestamp] - - Ref: - https://influxdb.com/docs/v0.9/write_protocols/write_syntax.html - https://influxdb.com/docs/v0.9/write_protocols/line.html - """ - lines = [] - static_tags = data.get('tags', None) - for point in data['points']: - elements = [] - - # add measurement name - measurement = _escape_tag(_get_unicode( - point.get('measurement', data.get('measurement')) - )) - key_values = [measurement] - - # add tags - if static_tags is None: - tags = point.get('tags', {}) - else: - tags = copy(static_tags) - tags.update(point.get('tags', {})) - - # tags should be sorted client-side to take load off server - for tag_key in sorted(tags.keys()): - key = _escape_tag(tag_key) - value = _escape_tag(tags[tag_key]) - - if key != '' and value != '': - key_values.append("{key}={value}".format(key=key, value=value)) - key_values = ','.join(key_values) - elements.append(key_values) - - # add fields - field_values = [] - for field_key in sorted(point['fields'].keys()): - key = _escape_tag(field_key) - value = _escape_value(point['fields'][field_key]) - if key != '' and value != '': - field_values.append("{key}={value}".format( - key=key, - value=value - )) - field_values = ','.join(field_values) - elements.append(field_values) - - # add timestamp - if 'time' in point: - elements.append(point['time']) - - line = ' '.join(elements) - lines.append(line) - lines = '\n'.join(lines) - return lines + '\n' -- cgit