diff --git a/.travis.yml b/.travis.yml index 03e6bc36..986205c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,4 +51,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/influxdb/line_protocol.py b/influxdb/line_protocol.py index e8816fc0..1d169317 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -6,14 +6,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): @@ -22,12 +22,14 @@ def _convert_timestamp(timestamp, precision=None): 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_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 = (timestamp - EPOCH).total_seconds() * 1e9 if precision is None or precision == 'n': return ns elif precision == 'u': @@ -40,7 +42,6 @@ def _convert_timestamp(timestamp, precision=None): return ns / 1e9 / 60 elif precision == 'h': return ns / 1e9 / 3600 - raise ValueError(timestamp) diff --git a/influxdb/tests/server_tests/py26_test.py b/influxdb/tests/server_tests/py26_test.py new file mode 100644 index 00000000..e952cc98 --- /dev/null +++ b/influxdb/tests/server_tests/py26_test.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +"""Define the resultset test package.""" + +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)