From 0af8be43f303c30ca02c3b17ffb1333f9ff9316f Mon Sep 17 00:00:00 2001 From: h0bot Date: Wed, 14 Dec 2016 21:43:59 +0000 Subject: [PATCH 01/15] Sending via UDP line protocol throws ... due to an uninitialized data variable. --- influxdb/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb/client.py b/influxdb/client.py index 6c9ed1b9..5f415abc 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -824,7 +824,7 @@ def send_packet(self, packet, protocol='json'): if protocol == 'json': data = make_lines(packet).encode('utf-8') elif protocol == 'line': - data = ('\n'.join(data) + '\n').encode('utf-8') + data = ('\n'.join(packet) + '\n').encode('utf-8') self.udp_socket.sendto(data, (self._host, self.udp_port)) From 422f144e27cc7e1554b8f7e3639470e96bd0f390 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 26 Apr 2017 13:26:39 -0400 Subject: [PATCH 02/15] added support for python 2.6.6 fixing flake8/pep8 issues fixing flake8/pep8 issues again travis for Python 2.6 fixing AttributeError on python 2.6.9 pep8 pep8 travis checks for 2.6.x unittest for travis checks for 2.6.x passing travis tests 2.7 - forward implemented changes suggested by @xginn8 copied requirements and __init__.py from upstream Added back the AttributeError exception Added back the AttributeError exception and included datetime removed comment --- influxdb/line_protocol.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index 59d93bff..e59841f4 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -5,14 +5,14 @@ from __future__ import print_function from __future__ import unicode_literals -from datetime import datetime +import datetime from numbers import Integral from pytz import UTC from dateutil.parser import parse from six import iteritems, binary_type, text_type, integer_types, PY2 -EPOCH = UTC.localize(datetime.utcfromtimestamp(0)) +EPOCH = UTC.localize(datetime.datetime.utcfromtimestamp(0)) def _convert_timestamp(timestamp, precision=None): @@ -20,10 +20,12 @@ def _convert_timestamp(timestamp, precision=None): return timestamp # assume precision is correct if timestamp is int if isinstance(_get_unicode(timestamp), text_type): timestamp = parse(timestamp) - if isinstance(timestamp, datetime): + if isinstance(timestamp, datetime.datetime): if not timestamp.tzinfo: timestamp = UTC.localize(timestamp) - ns = (timestamp - EPOCH).total_seconds() * 1e9 + ns_no_total = (timestamp - EPOCH) + ns = ((ns_no_total.microseconds + ( + ns_no_total.seconds + ns_no_total.days * 24 * 3600) * 10 ** 6) / 10 ** 6) * 1e9 if precision is None or precision == 'n': return ns elif precision == 'u': @@ -36,7 +38,7 @@ def _convert_timestamp(timestamp, precision=None): return ns / 1e9 / 60 elif precision == 'h': return ns / 1e9 / 3600 - raise ValueError(timestamp) + raise ValueError(timestamp) def _escape_tag(tag): From 4e7e8da94bb1d234c0937f54acccf1d8f189c772 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Mon, 1 May 2017 20:17:22 -0400 Subject: [PATCH 03/15] pep8 pep8 --- influxdb/line_protocol.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index e59841f4..7b988f9a 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -25,7 +25,8 @@ def _convert_timestamp(timestamp, precision=None): timestamp = UTC.localize(timestamp) ns_no_total = (timestamp - EPOCH) ns = ((ns_no_total.microseconds + ( - ns_no_total.seconds + ns_no_total.days * 24 * 3600) * 10 ** 6) / 10 ** 6) * 1e9 + ns_no_total.seconds + ns_no_total.days * 24 * 3600) * + 10 ** 6) / 10 ** 6) * 1e9 if precision is None or precision == 'n': return ns elif precision == 'u': From 23e5f44ef3765f92faad2520e9742c38daa9a61b Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Thu, 17 Aug 2017 17:53:33 -0400 Subject: [PATCH 04/15] merge influxdb/client.py --- influxdb/client.py | 120 +++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 53 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 5f415abc..f8cea2ac 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -1,17 +1,17 @@ # -*- coding: utf-8 -*- -""" -Python client for InfluxDB -""" +"""Python client for InfluxDB.""" + from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals +from sys import version_info + import json import socket import requests import requests.exceptions -from sys import version_info from influxdb.line_protocol import make_lines, quote_ident, quote_literal from influxdb.resultset import ResultSet @@ -30,7 +30,9 @@ class InfluxDBClient(object): - """The :class:`~.InfluxDBClient` object holds information necessary to + """InfluxDBClient primary client object to connect InfluxDB. + + The :class:`~.InfluxDBClient` object holds information necessary to connect to InfluxDB. Requests can be made to InfluxDB directly through the client. @@ -89,8 +91,8 @@ def __init__(self, self._verify_ssl = verify_ssl - self.use_udp = use_udp - self.udp_port = udp_port + self.__use_udp = use_udp + self.__udp_port = udp_port self._session = requests.Session() if use_udp: self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -117,28 +119,29 @@ def __init__(self, @property def _baseurl(self): - return self._get_baseurl() - - def _get_baseurl(self): return self.__baseurl @property def _host(self): - return self._get_host() - - def _get_host(self): return self.__host @property def _port(self): - return self._get_port() - - def _get_port(self): return self.__port + @property + def _udp_port(self): + return self.__udp_port + + @property + def _use_udp(self): + return self.__use_udp + @classmethod - def from_DSN(cls, dsn, **kwargs): - """Return an instance of :class:`~.InfluxDBClient` from the provided + def from_dsn(cls, dsn, **kwargs): + r"""Generate an instance of InfluxDBClient from given data source name. + + Return an instance of :class:`~.InfluxDBClient` from the provided data source name. Supported schemes are "influxdb", "https+influxdb" and "udp+influxdb". Parameters for the :class:`~.InfluxDBClient` constructor may also be passed to this method. @@ -153,12 +156,12 @@ def from_DSN(cls, dsn, **kwargs): :: - >> cli = InfluxDBClient.from_DSN('influxdb://username:password@\ -localhost:8086/databasename', timeout=5) + >> cli = InfluxDBClient.from_dsn('influxdb://username:password@\ + localhost:8086/databasename', timeout=5) >> type(cli) - >> cli = InfluxDBClient.from_DSN('udp+influxdb://username:pass@\ -localhost:8086/databasename', timeout=5, udp_port=159) + >> cli = InfluxDBClient.from_dsn('udp+influxdb://username:pass@\ + localhost:8086/databasename', timeout=5, udp_port=159) >> print('{0._baseurl} - {0.use_udp} {0.udp_port}'.format(cli)) http://localhost:8086 - True 159 @@ -167,8 +170,7 @@ def from_DSN(cls, dsn, **kwargs): be used for the TCP connection; specify the UDP port with the additional `udp_port` parameter (cf. examples). """ - - init_args = parse_dsn(dsn) + init_args = _parse_dsn(dsn) host, port = init_args.pop('hosts')[0] init_args['host'] = host init_args['port'] = port @@ -247,7 +249,7 @@ def request(self, url, method='GET', params=None, data=None, timeout=self._timeout ) break - except requests.exceptions.ConnectionError as e: + except requests.exceptions.ConnectionError: _try += 1 if self._retries != 0: retry = _try < self._retries @@ -279,7 +281,6 @@ def write(self, data, params=None, expected_response_code=204, :returns: True, if the write operation is successful :rtype: bool """ - headers = self._headers headers['Content-type'] = 'application/octet-stream' @@ -303,7 +304,8 @@ def write(self, data, params=None, expected_response_code=204, ) return True - def _read_chunked_response(self, response, raise_errors=True): + @staticmethod + def _read_chunked_response(response, raise_errors=True): result_set = {} for line in response.iter_lines(): if isinstance(line, bytes): @@ -311,8 +313,9 @@ def _read_chunked_response(self, response, raise_errors=True): data = json.loads(line) for result in data.get('results', []): for _key in result: - if type(result[_key]) == list: - result_set.setdefault(_key, []).extend(result[_key]) + if isinstance(result[_key], list): + result_set.setdefault( + _key, []).extend(result[_key]) return ResultSet(result_set, raise_errors=raise_errors) def query(self, @@ -329,7 +332,8 @@ def query(self, :param query: the actual query string :type query: str - :param params: additional parameters for the request, defaults to {} + :param params: additional parameters for the request, + defaults to {} :type params: dict :param epoch: response timestamps to be in epoch format either 'h', @@ -395,8 +399,8 @@ def query(self, # TODO(aviau): Always return a list. (This would be a breaking change) if len(results) == 1: return results[0] - else: - return results + + return results def write_points(self, points, @@ -439,7 +443,6 @@ def write_points(self, .. note:: if no retention policy is specified, the default retention policy for the database is used """ - if batch_size and batch_size > 0: for batch in self._batches(points, batch_size): self._write_points(points=batch, @@ -448,14 +451,15 @@ def write_points(self, retention_policy=retention_policy, tags=tags, protocol=protocol) return True - else: - return self._write_points(points=points, - time_precision=time_precision, - database=database, - retention_policy=retention_policy, - tags=tags, protocol=protocol) - def _batches(self, iterable, size): + return self._write_points(points=points, + time_precision=time_precision, + database=database, + retention_policy=retention_policy, + tags=tags, protocol=protocol) + + @staticmethod + def _batches(iterable, size): for i in xrange(0, len(iterable), size): yield iterable[i:i + size] @@ -471,7 +475,7 @@ def _write_points(self, "Invalid time precision is given. " "(use 'n', 'u', 'ms', 's', 'm' or 'h')") - if self.use_udp and time_precision and time_precision != 's': + if self._use_udp and time_precision and time_precision != 's': raise ValueError( "InfluxDB only supports seconds precision for udp writes" ) @@ -496,7 +500,7 @@ def _write_points(self, if retention_policy is not None: params['rp'] = retention_policy - if self.use_udp: + if self._use_udp: self.send_packet(data, protocol=protocol) else: self.write( @@ -549,8 +553,8 @@ def create_retention_policy(self, name, duration, replication, :param duration: the duration of the new retention policy. Durations such as 1h, 90m, 12h, 7d, and 4w, are all supported and mean 1 hour, 90 minutes, 12 hours, 7 day, and 4 weeks, - respectively. For infinite retention – meaning the data will - never be deleted – use 'INF' for duration. + respectively. For infinite retention - meaning the data will + never be deleted - use 'INF' for duration. The minimum retention period is 1 hour. :type duration: str :param replication: the replication of the retention policy @@ -584,8 +588,8 @@ def alter_retention_policy(self, name, database=None, :param duration: the new duration of the existing retention policy. Durations such as 1h, 90m, 12h, 7d, and 4w, are all supported and mean 1 hour, 90 minutes, 12 hours, 7 day, and 4 weeks, - respectively. For infinite retention – meaning the data will - never be deleted – use 'INF' for duration. + respectively. For infinite retention, meaning the data will + never be deleted, use 'INF' for duration. The minimum retention period is 1 hour. :type duration: str :param replication: the new replication of the existing @@ -642,8 +646,7 @@ def get_list_retention_policies(self, database=None): u'duration': u'0', u'name': u'default', u'replicaN': 1}] - """ - + """ if not (database or self._database): raise InfluxDBClientError( "get_list_retention_policies() requires a database as a " @@ -674,7 +677,7 @@ def get_list_users(self): return list(self.query("SHOW USERS").get_points()) def create_user(self, username, password, admin=False): - """Create a new user in InfluxDB + """Create a new user in InfluxDB. :param username: the new username to create :type username: str @@ -712,8 +715,9 @@ def set_user_password(self, username, password): self.query(text) def delete_series(self, database=None, measurement=None, tags=None): - """Delete series from a database. Series can be filtered by - measurement and tags. + """Delete series from a database. + + Series can be filtered by measurement and tags. :param database: the database from which the series should be deleted, defaults to client's current database @@ -825,10 +829,20 @@ def send_packet(self, packet, protocol='json'): data = make_lines(packet).encode('utf-8') elif protocol == 'line': data = ('\n'.join(packet) + '\n').encode('utf-8') - self.udp_socket.sendto(data, (self._host, self.udp_port)) + self.udp_socket.sendto(data, (self._host, self._udp_port)) + def close(self): + """Close http session.""" + if isinstance(self._session, requests.Session): + self._session.close() -def parse_dsn(dsn): + +def _parse_dsn(dsn): + """Parse data source name. + + This is a helper function to split the data source name provided in + the from_dsn classmethod + """ conn_params = urlparse(dsn) init_args = {} scheme_info = conn_params.scheme.split('+') From b88caf573de28f4c438912463c1718af35d1e635 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Thu, 17 Aug 2017 18:06:01 -0400 Subject: [PATCH 05/15] dealing with merge conflicts --- influxdb/line_protocol.py | 179 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 influxdb/line_protocol.py diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py new file mode 100644 index 00000000..225d0a1b --- /dev/null +++ b/influxdb/line_protocol.py @@ -0,0 +1,179 @@ +# -*- coding: utf-8 -*- +"""Define the line_protocol handler.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +import datetime +from numbers import Integral + +from pytz import UTC +from dateutil.parser import parse +from six import iteritems, binary_type, text_type, integer_types, PY2 + +EPOCH = UTC.localize(datetime.datetime.utcfromtimestamp(0)) + + +def _convert_timestamp(timestamp, precision=None): + if isinstance(timestamp, Integral): + return timestamp # assume precision is correct if timestamp is int + + if isinstance(_get_unicode(timestamp), text_type): + timestamp = parse(timestamp) +<<<<<<< HEAD + if isinstance(timestamp, datetime.datetime): + if not timestamp.tzinfo: + timestamp = UTC.localize(timestamp) + ns_no_total = (timestamp - EPOCH) + ns = ((ns_no_total.microseconds + ( + ns_no_total.seconds + ns_no_total.days * 24 * 3600) * + 10 ** 6) / 10 ** 6) * 1e9 +======= + + if isinstance(timestamp, datetime): + if not timestamp.tzinfo: + timestamp = UTC.localize(timestamp) + + ns = (timestamp - EPOCH).total_seconds() * 1e9 +>>>>>>> e07dafccf48646f1c795a71fde467fa448b02605 + if precision is None or precision == 'n': + return ns + elif precision == 'u': + return ns / 1e3 + elif precision == 'ms': + return ns / 1e6 + elif precision == 's': + return ns / 1e9 + elif precision == 'm': + return ns / 1e9 / 60 + elif precision == 'h': + return ns / 1e9 / 3600 +<<<<<<< HEAD + raise ValueError(timestamp) +======= + + raise ValueError(timestamp) +>>>>>>> e07dafccf48646f1c795a71fde467fa448b02605 + + +def _escape_tag(tag): + tag = _get_unicode(tag, force=True) + return tag.replace( + "\\", "\\\\" + ).replace( + " ", "\\ " + ).replace( + ",", "\\," + ).replace( + "=", "\\=" + ) + + +def quote_ident(value): + """Indent the quotes.""" + return "\"{}\"".format(value + .replace("\\", "\\\\") + .replace("\"", "\\\"") + .replace("\n", "\\n")) + + +def quote_literal(value): + """Quote provided literal.""" + return "'{}'".format(value + .replace("\\", "\\\\") + .replace("'", "\\'")) + + +def _is_float(value): + try: + float(value) + except (TypeError, ValueError): + return False + + return True + + +def _escape_value(value): + value = _get_unicode(value) + + if isinstance(value, text_type) and value != '': + return quote_ident(value) + elif isinstance(value, integer_types) and not isinstance(value, bool): + return str(value) + 'i' + elif _is_float(value): + return repr(value) + + 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: + if PY2: + return unicode(data) + else: + return str(data) + else: + return data + + +def make_lines(data, precision=None): + """Extract points from given dict. + + Extracts the points from the given dict and returns a Unicode string + matching the line protocol introduced in InfluxDB 0.9.0. + """ + lines = [] + static_tags = data.get('tags') + 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: + tags = dict(static_tags) # make a copy, since we'll modify + tags.update(point.get('tags') or {}) + else: + tags = point.get('tags') or {} + + # tags should be sorted client-side to take load off server + for tag_key, tag_value in sorted(iteritems(tags)): + key = _escape_tag(tag_key) + value = _escape_tag(tag_value) + + if key != '' and value != '': + key_values.append(key + "=" + value) + + elements.append(','.join(key_values)) + + # add fields + field_values = [] + for field_key, field_value in sorted(iteritems(point['fields'])): + key = _escape_tag(field_key) + value = _escape_value(field_value) + + if key != '' and value != '': + field_values.append(key + "=" + value) + + elements.append(','.join(field_values)) + + # add timestamp + if 'time' in point: + timestamp = _get_unicode(str(int( + _convert_timestamp(point['time'], precision)))) + elements.append(timestamp) + + line = ' '.join(elements) + lines.append(line) + + return '\n'.join(lines) + '\n' From c6f826177efce8112f062f9496c52d3db3a23f8f Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Thu, 17 Aug 2017 18:12:27 -0400 Subject: [PATCH 06/15] fixing beyond compare addons --- influxdb/line_protocol.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index 225d0a1b..fb1e76e4 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -22,7 +22,6 @@ def _convert_timestamp(timestamp, precision=None): if isinstance(_get_unicode(timestamp), text_type): timestamp = parse(timestamp) -<<<<<<< HEAD if isinstance(timestamp, datetime.datetime): if not timestamp.tzinfo: timestamp = UTC.localize(timestamp) @@ -30,14 +29,7 @@ def _convert_timestamp(timestamp, precision=None): ns = ((ns_no_total.microseconds + ( ns_no_total.seconds + ns_no_total.days * 24 * 3600) * 10 ** 6) / 10 ** 6) * 1e9 -======= - if isinstance(timestamp, datetime): - if not timestamp.tzinfo: - timestamp = UTC.localize(timestamp) - - ns = (timestamp - EPOCH).total_seconds() * 1e9 ->>>>>>> e07dafccf48646f1c795a71fde467fa448b02605 if precision is None or precision == 'n': return ns elif precision == 'u': @@ -50,12 +42,7 @@ def _convert_timestamp(timestamp, precision=None): return ns / 1e9 / 60 elif precision == 'h': return ns / 1e9 / 3600 -<<<<<<< HEAD raise ValueError(timestamp) -======= - - raise ValueError(timestamp) ->>>>>>> e07dafccf48646f1c795a71fde467fa448b02605 def _escape_tag(tag): From 086b9dda1df952c22887aad21615a7027df4fe93 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 23 Aug 2017 20:05:44 -0400 Subject: [PATCH 07/15] addding test and updated travis.yml for legacy python 2.6 --- .travis.yml | 3 ++ influxdb/tests/py26_test.py | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 influxdb/tests/py26_test.py diff --git a/.travis.yml b/.travis.yml index 8c0093b8..9c4f7f03 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,8 @@ matrix: - python: 3.4 env: TOX_ENV=docs include: + - python: 2.6 + env: TOX_ENV=py26 - python: 2.7 env: TOX_ENV=py27 - python: 2.7 @@ -30,6 +32,7 @@ matrix: env: TOX_ENV=coverage install: + - if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install importlib unittest2; fi - pip install tox - pip install coveralls - mkdir influxdb_install diff --git a/influxdb/tests/py26_test.py b/influxdb/tests/py26_test.py new file mode 100644 index 00000000..c5084062 --- /dev/null +++ b/influxdb/tests/py26_test.py @@ -0,0 +1,57 @@ +#!/usr/bin/python + +import sys +import os +import datetime +import requests +import time + +from influxdb import InfluxDBClient + +# InfluxDB Server parameters +influxdb_server = "localhost" +port = 8086 +database_name = "test_db" +ip_addr = os.popen("hostname").read() + +# InfluxDB connection and database creation if it doesn't exists +client = InfluxDBClient(influxdb_server, port, "influx-user", "influx-pass") + +try: + client.create_database(database_name) +except requests.ConnectionError as e: + print("Failed to start, unable to establish connection with InfluxDB server %s - %s" % (influxdb_server, e)) + sys.exit(2) + +# System Load +load_avg_1 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read()[0] +load_avg_5 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read()[1] +load_avg_15 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read()[2] + +counter = 0 + +while counter <= 5: + load_1 = [ + {"measurement": "Load_1_minute", + "tags": {"hosts": ip_addr}, + "time": datetime.datetime.now(), "fields": {"load_avg_1": int(load_avg_1)}}] + client.write_points(load_1) + + load_5 = [ + {"measurement": "Load_5_minutes", + "tags": {"hosts": ip_addr}, + "time": datetime.datetime.now(), "fields": {"load_avg_5": int(load_avg_5)}}] + client.write_points(load_5) + + load_15 = [ + {"measurement": "Load_15_minute", + "tags": {"hosts": ip_addr}, + "time": datetime.datetime.now(), "fields": {"load_avg_15": int(load_avg_15)}}] + client.write_points(load_15) + + counter += 1 + time.sleep(3) + +result = client.query("select value from Load_1_minute;") + +print("Result: %s" % result) \ No newline at end of file From a4774a22f73c2c1018b545afb2d59347430bbcb0 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 23 Aug 2017 20:21:58 -0400 Subject: [PATCH 08/15] addding test and updated travis.yml for legacy python 2.6 --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9c4f7f03..5ee66dab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,9 @@ matrix: env: TOX_ENV=coverage install: - - if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install importlib unittest2; fi + - if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install importlib unittest2 tox coveralls;mkdir influxdb_install; + wget https://dl.influxdata.com/influxdb/releases/influxdb_1.2.4_amd64.deb;dpkg -x influxdb*.deb influxdb_install; + fi - pip install tox - pip install coveralls - mkdir influxdb_install From 03da1ae54854fa29a0c3201b6c01e097acb170b6 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 23 Aug 2017 20:28:36 -0400 Subject: [PATCH 09/15] checking travis.yml parameters --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5ee66dab..0974b434 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,9 +32,6 @@ matrix: env: TOX_ENV=coverage install: - - if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install importlib unittest2 tox coveralls;mkdir influxdb_install; - wget https://dl.influxdata.com/influxdb/releases/influxdb_1.2.4_amd64.deb;dpkg -x influxdb*.deb influxdb_install; - fi - pip install tox - pip install coveralls - mkdir influxdb_install From 62d62a7247e3d7953298ea60d41d7845c5d91948 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 23 Aug 2017 21:54:08 -0400 Subject: [PATCH 10/15] removed python2.6 from travis.yml for testing purposes --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0974b434..8c0093b8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,6 @@ matrix: - python: 3.4 env: TOX_ENV=docs include: - - python: 2.6 - env: TOX_ENV=py26 - python: 2.7 env: TOX_ENV=py27 - python: 2.7 From 4ede30f3bd88bb9a3bcb733bf31d085b999f84af Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 23 Aug 2017 22:05:54 -0400 Subject: [PATCH 11/15] removed python2.6 from travis.yml for testing purposes --- tox.ini | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index bfa25165..ecb957ab 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27, py34, py35, py36, pypy, pypy3, flake8, pep257, coverage, docs +envlist = py26, py27, py34, py35, py36, pypy, pypy3, flake8, pep257, coverage, docs [testenv] passenv = INFLUXDB_PYTHON_INFLUXD_PATH @@ -7,6 +7,7 @@ setenv = INFLUXDB_PYTHON_SKIP_SERVER_TESTS=False deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt py27,py34,py35,py36: pandas==0.20.1 + py26: pandas==0.16.2 # Only install pandas with non-pypy interpreters commands = nosetests -v --with-doctest {posargs} From 9d2e317eb384914e648ad40cd7f8fea0f5b993fd Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 23 Aug 2017 22:12:04 -0400 Subject: [PATCH 12/15] reverted travis.yml and tox.ini --- .travis.yml | 2 +- tox.ini | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8c0093b8..b63e1b2a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,4 +50,4 @@ cache: false # directories: # - $HOME/.cache/pip #before_cache: -# - rm -f $HOME/.cache/pip/log/debug.log +# - rm -f $HOME/.cache/pip/log/debug.log \ No newline at end of file diff --git a/tox.ini b/tox.ini index ecb957ab..bfa25165 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py26, py27, py34, py35, py36, pypy, pypy3, flake8, pep257, coverage, docs +envlist = py27, py34, py35, py36, pypy, pypy3, flake8, pep257, coverage, docs [testenv] passenv = INFLUXDB_PYTHON_INFLUXD_PATH @@ -7,7 +7,6 @@ setenv = INFLUXDB_PYTHON_SKIP_SERVER_TESTS=False deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt py27,py34,py35,py36: pandas==0.20.1 - py26: pandas==0.16.2 # Only install pandas with non-pypy interpreters commands = nosetests -v --with-doctest {posargs} From 817f4d9d184c921fad2d166d25a7728c65fce5fa Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Sat, 16 Sep 2017 19:04:49 -0400 Subject: [PATCH 13/15] implemented requested changes --- influxdb/line_protocol.py | 2 +- influxdb/tests/py26_test.py | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index fb1e76e4..41213767 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -42,7 +42,7 @@ def _convert_timestamp(timestamp, precision=None): return ns / 1e9 / 60 elif precision == 'h': return ns / 1e9 / 3600 - raise ValueError(timestamp) + raise ValueError(timestamp) def _escape_tag(tag): diff --git a/influxdb/tests/py26_test.py b/influxdb/tests/py26_test.py index c5084062..3713f951 100644 --- a/influxdb/tests/py26_test.py +++ b/influxdb/tests/py26_test.py @@ -1,9 +1,10 @@ -#!/usr/bin/python +#!/usr/bin/env python import sys import os import datetime import requests +import socket import time from influxdb import InfluxDBClient @@ -12,10 +13,10 @@ influxdb_server = "localhost" port = 8086 database_name = "test_db" -ip_addr = os.popen("hostname").read() +host_name = socket.gethostname() # InfluxDB connection and database creation if it doesn't exists -client = InfluxDBClient(influxdb_server, port, "influx-user", "influx-pass") +client = InfluxDBClient(influxdb_server, port, "influx-user", "influx-pass", database_name) try: client.create_database(database_name) @@ -24,34 +25,34 @@ sys.exit(2) # System Load -load_avg_1 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read()[0] -load_avg_5 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read()[1] -load_avg_15 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read()[2] +load_avg_1 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read().split()[0] +load_avg_5 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read().split()[1] +load_avg_15 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read().split()[2] counter = 0 while counter <= 5: load_1 = [ {"measurement": "Load_1_minute", - "tags": {"hosts": ip_addr}, - "time": datetime.datetime.now(), "fields": {"load_avg_1": int(load_avg_1)}}] + "tags": {"hosts": host_name}, + "time": datetime.datetime.now(), "fields": {"load_avg_1": float(load_avg_1)}}] client.write_points(load_1) load_5 = [ {"measurement": "Load_5_minutes", - "tags": {"hosts": ip_addr}, - "time": datetime.datetime.now(), "fields": {"load_avg_5": int(load_avg_5)}}] + "tags": {"hosts": host_name}, + "time": datetime.datetime.now(), "fields": {"load_avg_5": float(load_avg_5)}}] client.write_points(load_5) load_15 = [ {"measurement": "Load_15_minute", - "tags": {"hosts": ip_addr}, - "time": datetime.datetime.now(), "fields": {"load_avg_15": int(load_avg_15)}}] + "tags": {"hosts": host_name}, + "time": datetime.datetime.now(), "fields": {"load_avg_15": float(load_avg_15)}}] client.write_points(load_15) counter += 1 - time.sleep(3) + time.sleep(1) -result = client.query("select value from Load_1_minute;") +result = client.query("select load_avg_1 from Load_1_minute;") print("Result: %s" % result) \ No newline at end of file From 5e386bbad3dbad596d633de9a2c2194937e0f2d6 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 20 Sep 2017 20:52:09 -0400 Subject: [PATCH 14/15] Replaced py26_test.py with new version as suggested --- influxdb/tests/py26_test.py | 58 --------------- influxdb/tests/server_tests/py26_test.py | 90 ++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 58 deletions(-) delete mode 100644 influxdb/tests/py26_test.py create mode 100644 influxdb/tests/server_tests/py26_test.py diff --git a/influxdb/tests/py26_test.py b/influxdb/tests/py26_test.py deleted file mode 100644 index 3713f951..00000000 --- a/influxdb/tests/py26_test.py +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env python - -import sys -import os -import datetime -import requests -import socket -import time - -from influxdb import InfluxDBClient - -# InfluxDB Server parameters -influxdb_server = "localhost" -port = 8086 -database_name = "test_db" -host_name = socket.gethostname() - -# InfluxDB connection and database creation if it doesn't exists -client = InfluxDBClient(influxdb_server, port, "influx-user", "influx-pass", database_name) - -try: - client.create_database(database_name) -except requests.ConnectionError as e: - print("Failed to start, unable to establish connection with InfluxDB server %s - %s" % (influxdb_server, e)) - sys.exit(2) - -# System Load -load_avg_1 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read().split()[0] -load_avg_5 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read().split()[1] -load_avg_15 = os.popen("cat /proc/loadavg|awk '{print $1,$2,$3}'").read().split()[2] - -counter = 0 - -while counter <= 5: - load_1 = [ - {"measurement": "Load_1_minute", - "tags": {"hosts": host_name}, - "time": datetime.datetime.now(), "fields": {"load_avg_1": float(load_avg_1)}}] - client.write_points(load_1) - - load_5 = [ - {"measurement": "Load_5_minutes", - "tags": {"hosts": host_name}, - "time": datetime.datetime.now(), "fields": {"load_avg_5": float(load_avg_5)}}] - client.write_points(load_5) - - load_15 = [ - {"measurement": "Load_15_minute", - "tags": {"hosts": host_name}, - "time": datetime.datetime.now(), "fields": {"load_avg_15": float(load_avg_15)}}] - client.write_points(load_15) - - counter += 1 - time.sleep(1) - -result = client.query("select load_avg_1 from Load_1_minute;") - -print("Result: %s" % result) \ No newline at end of file diff --git a/influxdb/tests/server_tests/py26_test.py b/influxdb/tests/server_tests/py26_test.py new file mode 100644 index 00000000..245b747b --- /dev/null +++ b/influxdb/tests/server_tests/py26_test.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- + +from __future__ import unicode_literals + +import datetime +import os +import socket +import subprocess +import time +import unittest + +from influxdb.tests.server_tests.base import SingleTestCaseWithServerMixin + +THIS_DIR = os.path.abspath(os.path.dirname(__file__)) + +# hack in check_output if it's not defined, like for python 2.6 +if "check_output" not in dir(subprocess): + def f(*popenargs, **kwargs): + """Check for output.""" + if 'stdout' in kwargs: + raise ValueError( + 'stdout argument not allowed, it will be overridden.' + ) + process = subprocess.Popen(stdout=subprocess.PIPE, + *popenargs, + **kwargs) + output, unused_err = process.communicate() + retcode = process.poll() + if retcode: + cmd = kwargs.get("args") + if cmd is None: + cmd = popenargs[0] + raise subprocess.CalledProcessError(retcode, cmd) + return output + subprocess.check_output = f + + +class TestPython26Set(SingleTestCaseWithServerMixin, unittest.TestCase): + """Define the Python26Set test object.""" + + influxdb_template_conf = os.path.join(THIS_DIR, 'influxdb.conf.template') + + def test_write_points(self): + """Test write points for Python26Set object.""" + self.host_name = socket.gethostname() + self.assertTrue(self.cli.create_database('db') is None) + + # System Load + self.lf = ["cat", "/proc/loadavg"] + + c = 0 + + while c < 5: + d = subprocess.check_output(self.lf).strip().split() + + load_1 = [ + { + "measurement": "Load_1_minute", + "tags": {"hosts": self.host_name}, + "time": datetime.datetime.now(), + "fields": {"load_avg_1": float(d[0])} + } + ] + self.cli.write_points(load_1) + + load_5 = [ + { + "measurement": "Load_5_minutes", + "tags": {"hosts": self.host_name}, + "time": datetime.datetime.now(), + "fields": {"load_avg_5": float(d[1])} + } + ] + self.cli.write_points(load_5) + + load_15 = [ + { + "measurement": "Load_15_minute", + "tags": {"hosts": self.host_name}, + "time": datetime.datetime.now(), + "fields": {"load_avg_15": float(d[2])} + } + ] + self.cli.write_points(load_15) + + c += 1 + time.sleep(1) + + result = self.cli.query("select load_avg_1 from Load_1_minute;") + self.assertTrue(result is not None) \ No newline at end of file From c1513a3181156f2b13d7b399a6dcab741ad2afe1 Mon Sep 17 00:00:00 2001 From: Eduardo Orochena Date: Wed, 20 Sep 2017 21:02:37 -0400 Subject: [PATCH 15/15] Replaced py26_test.py with new version as suggested and fix flake8 --- influxdb/tests/server_tests/py26_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/influxdb/tests/server_tests/py26_test.py b/influxdb/tests/server_tests/py26_test.py index 245b747b..e952cc98 100644 --- a/influxdb/tests/server_tests/py26_test.py +++ b/influxdb/tests/server_tests/py26_test.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""Define the resultset test package.""" from __future__ import unicode_literals @@ -87,4 +88,4 @@ def test_write_points(self): time.sleep(1) result = self.cli.query("select load_avg_1 from Load_1_minute;") - self.assertTrue(result is not None) \ No newline at end of file + self.assertTrue(result is not None)