Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ea2437

Browse files
author
aviau
committed
New epoch parameter + pep8 + enable udp tests
* Added epoch parameter to ``query`` * Removed test_write_points_with_precision as queries now always return nanosecond precision See doc: > The format of the returned timestamps complies with RFC3339, and > has nanosecond precision.
1 parent 6627efa commit 2ea2437

File tree

4 files changed

+19
-101
lines changed

4 files changed

+19
-101
lines changed

influxdb/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def write(self, data, params=None, expected_response_code=204):
270270
def query(self,
271271
query,
272272
params={},
273+
epoch=None,
273274
expected_response_code=200,
274275
database=None,
275276
raise_errors=True):
@@ -298,6 +299,9 @@ def query(self,
298299
params['q'] = query
299300
params['db'] = database or self._database
300301

302+
if epoch is not None:
303+
params['epoch'] = epoch
304+
301305
response = self.request(
302306
url="query",
303307
method='GET',

tests/influxdb/client_test.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ def test_write(self):
155155

156156
self.assertEqual(
157157
m.last_request.body,
158-
b"cpu_load_short,host=server01,region=us-west value=0.64 1257894000000000000\n",
158+
b"cpu_load_short,host=server01,region=us-west "
159+
b"value=0.64 1257894000000000000\n",
159160
)
160161

161162
def test_write_points(self):
@@ -171,7 +172,8 @@ def test_write_points(self):
171172
self.dummy_points,
172173
)
173174
self.assertEqual(
174-
"cpu_load_short,host=server01,region=us-west value=0.64 1257894000000000000\n",
175+
"cpu_load_short,host=server01,region=us-west "
176+
"value=0.64 1257894000000000000\n",
175177
m.last_request.body.decode('utf-8'),
176178
)
177179

@@ -191,7 +193,8 @@ def test_write_points_toplevel_attributes(self):
191193
retention_policy="somepolicy"
192194
)
193195
self.assertEqual(
194-
"cpu_load_short,host=server01,region=us-west,tag=hello value=0.64 1257894000000000000\n",
196+
"cpu_load_short,host=server01,region=us-west,tag=hello "
197+
"value=0.64 1257894000000000000\n",
195198
m.last_request.body.decode('utf-8'),
196199
)
197200

@@ -281,7 +284,8 @@ def test_write_points_with_precision(self):
281284
)
282285

283286
self.assertEqual(
284-
b"cpu_load_short,host=server01,region=us-west value=0.64 1257894000000000000\n",
287+
b"cpu_load_short,host=server01,region=us-west "
288+
b"value=0.64 1257894000000000000\n",
285289
m.last_request.body,
286290
)
287291

tests/influxdb/client_test_with_server.py

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import distutils.spawn
1616
from functools import partial
1717
import os
18-
import re
1918
import shutil
2019
import subprocess
2120
import sys
@@ -653,96 +652,6 @@ def test_write_points_batch(self):
653652
self.assertIn(12, net_out['series'][0]['values'][0])
654653
self.assertIn(12.34, cpu['series'][0]['values'][0])
655654

656-
def test_write_points_with_precision(self):
657-
""" check that points written with an explicit precision have
658-
actually that precision used.
659-
"""
660-
# for that we'll check that - for each precision - the actual 'time'
661-
# value returned by a select has the correct regex format..
662-
# n : u'2015-03-20T15:23:36.615654966Z'
663-
# u : u'2015-03-20T15:24:10.542554Z'
664-
# ms : u'2015-03-20T15:24:50.878Z'
665-
# s : u'2015-03-20T15:20:24Z'
666-
# m : u'2015-03-20T15:25:00Z'
667-
# h : u'2015-03-20T15:00:00Z'
668-
base_regex = '\d{4}-\d{2}-\d{2}T\d{2}:' # YYYY-MM-DD 'T' hh:
669-
base_s_regex = base_regex + '\d{2}:\d{2}' # base_regex + mm:ss
670-
671-
point = {
672-
"measurement": "cpu_load_short",
673-
"tags": {
674-
"host": "server01",
675-
"region": "us-west"
676-
},
677-
"time": "2009-11-10T12:34:56.123456789Z",
678-
"fields": {
679-
"value": 0.64
680-
}
681-
}
682-
683-
# As far as we can see the values aren't directly available depending
684-
# on the precision used.
685-
# The less the precision, the more to wait for the value to be
686-
# actually written/available.
687-
for idx, (precision, expected_regex, sleep_time) in enumerate((
688-
('n', base_s_regex + '\.\d{9}Z', 1),
689-
('u', base_s_regex + '\.\d{6}Z', 1),
690-
('ms', base_s_regex + '\.\d{3}Z', 1),
691-
('s', base_s_regex + 'Z', 1),
692-
693-
# ('h', base_regex + '00:00Z', ),
694-
# that would require a sleep of possibly up to 3600 secs (/ 2 ?)..
695-
)):
696-
db = 'db1' # to not shoot us in the foot/head,
697-
# we work on a fresh db each time:
698-
self.cli.create_database(db)
699-
before = datetime.datetime.now()
700-
self.assertIs(
701-
True,
702-
self.cli.write_points(
703-
[point],
704-
time_precision=precision,
705-
database=db))
706-
707-
# sys.stderr.write('checking presision with %r :
708-
# before=%s\n' % (precision, before))
709-
after = datetime.datetime.now()
710-
711-
if sleep_time > 1:
712-
sleep_time -= (after if before.min != after.min
713-
else before).second
714-
715-
start = time.time()
716-
timeout = start + sleep_time
717-
# sys.stderr.write('should sleep %s ..\n' % sleep_time)
718-
while time.time() < timeout:
719-
rsp = self.cli.query('SELECT * FROM cpu_load_short',
720-
database=db)
721-
if rsp != {'cpu_load_short': []}:
722-
# sys.stderr.write('already ? only slept %s\n' % (
723-
# time.time() - start))
724-
break
725-
time.sleep(1)
726-
else:
727-
pass
728-
# sys.stderr.write('ok !\n')
729-
730-
# sys.stderr.write('sleeping %s..\n' % sleep_time)
731-
732-
if sleep_time:
733-
time.sleep(sleep_time)
734-
735-
rsp = self.cli.query('SELECT * FROM cpu_load_short', database=db)
736-
# sys.stderr.write('precision=%s rsp_timestamp = %r\n' % (
737-
# precision, rsp['cpu_load_short'][0]['time']))
738-
739-
m = re.match(
740-
expected_regex,
741-
list(rsp['cpu_load_short'])[0]['time']
742-
)
743-
self.assertIsNotNone(m)
744-
self.cli.drop_database(db)
745-
746655
def test_query(self):
747656
self.assertIs(True, self.cli.write_points(dummy_point))
748657

@@ -973,13 +882,11 @@ def test_query_multiple_series(self):
973882

974883
############################################################################
975884

976-
@unittest.skip("Broken as of 0.9.0")
977885
@unittest.skipIf(not is_influxdb_bin_ok, "could not find influxd binary")
978886
class UdpTests(ManyTestCasesWithServerMixin,
979887
unittest.TestCase):
980888

981889
influxdb_udp_enabled = True
982-
983890
influxdb_template_conf = os.path.join(THIS_DIR,
984891
'influxdb.conf.template')
985892

@@ -990,14 +897,15 @@ def test_write_points_udp(self):
990897
'root',
991898
'',
992899
database='db',
993-
use_udp=True, udp_port=self.influxd_inst.udp_port
900+
use_udp=True,
901+
udp_port=self.influxd_inst.udp_port
994902
)
995903
cli.write_points(dummy_point)
996904

997905
# The points are not immediately available after write_points.
998906
# This is to be expected because we are using udp (no response !).
999907
# So we have to wait some time,
1000-
time.sleep(1) # 1 sec seems to be a good choice.
908+
time.sleep(3) # 3 sec seems to be a good choice.
1001909
rsp = self.cli.query('SELECT * FROM cpu_load_short')
1002910

1003911
self.assertEqual(

tests/influxdb/dataframe_client_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def test_write_points_from_dataframe(self):
3333
"column_three"])
3434
expected = (
3535
b"foo column_one=\"1\",column_three=1.0,column_two=1 0\n"
36-
b"foo column_one=\"2\",column_three=2.0,column_two=2 3600000000000\n"
36+
b"foo column_one=\"2\",column_three=2.0,column_two=2 "
37+
b"3600000000000\n"
3738
)
3839

3940
with requests_mock.Mocker() as m:
@@ -92,7 +93,8 @@ def test_write_points_from_dataframe_with_period_index(self):
9293
"column_three"])
9394
expected = (
9495
b"foo column_one=\"1\",column_three=1.0,column_two=1 0\n"
95-
b"foo column_one=\"2\",column_three=2.0,column_two=2 86400000000000\n"
96+
b"foo column_one=\"2\",column_three=2.0,column_two=2 "
97+
b"86400000000000\n"
9698
)
9799

98100
with requests_mock.Mocker() as m:

0 commit comments

Comments
 (0)