Skip to content

Commit dd4d6a6

Browse files
committed
add consistency parameter to write_points [influxdata#643]
1 parent d370895 commit dd4d6a6

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

influxdb/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ def write_points(self,
440440
retention_policy=None,
441441
tags=None,
442442
batch_size=None,
443-
protocol='json'
443+
protocol='json',
444+
consistency=None
444445
):
445446
"""Write to multiple time series names.
446447
@@ -468,6 +469,8 @@ def write_points(self,
468469
:type batch_size: int
469470
:param protocol: Protocol for writing data. Either 'line' or 'json'.
470471
:type protocol: str
472+
:param consistency: Consistency for the points. One of {'any','one','quorum','all'}.
473+
:type consistency: str
471474
:returns: True, if the operation is successful
472475
:rtype: bool
473476
@@ -480,14 +483,14 @@ def write_points(self,
480483
time_precision=time_precision,
481484
database=database,
482485
retention_policy=retention_policy,
483-
tags=tags, protocol=protocol)
486+
tags=tags, protocol=protocol, consistency=consistency)
484487
return True
485488

486489
return self._write_points(points=points,
487490
time_precision=time_precision,
488491
database=database,
489492
retention_policy=retention_policy,
490-
tags=tags, protocol=protocol)
493+
tags=tags, protocol=protocol, consistency=consistency)
491494

492495
def ping(self):
493496
"""Check connectivity to InfluxDB.
@@ -513,12 +516,16 @@ def _write_points(self,
513516
database,
514517
retention_policy,
515518
tags,
516-
protocol='json'):
519+
protocol='json',
520+
consistency=None):
517521
if time_precision not in ['n', 'u', 'ms', 's', 'm', 'h', None]:
518522
raise ValueError(
519523
"Invalid time precision is given. "
520524
"(use 'n', 'u', 'ms', 's', 'm' or 'h')")
521525

526+
if consistency not in ['any', 'one', 'quorum', 'all', None]:
527+
raise ValueError('Invalid consistency: {}'.format(consistency))
528+
522529
if protocol == 'json':
523530
data = {
524531
'points': points
@@ -533,6 +540,9 @@ def _write_points(self,
533540
'db': database or self._database
534541
}
535542

543+
if consistency is not None:
544+
params['consistency'] = consistency
545+
536546
if time_precision is not None:
537547
params['precision'] = time_precision
538548

influxdb/tests/client_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,23 @@ def test_write_points_with_precision(self):
337337
m.last_request.body,
338338
)
339339

340+
def test_write_points_with_consistency(self):
341+
'''Test write points with consistency for TestInfluxDBClient object.'''
342+
with requests_mock.Mocker() as m:
343+
m.register_uri(
344+
requests_mock.POST,
345+
'http://localhost:8086/write',
346+
status_code=204
347+
)
348+
349+
cli = InfluxDBClient(database='db')
350+
351+
cli.write_points(self.dummy_points, consistency='any')
352+
self.assertEqual(
353+
m.last_request.qs,
354+
{'db': ['db'], 'consistency': ['any']}
355+
)
356+
340357
def test_write_points_with_precision_udp(self):
341358
"""Test write points with precision for TestInfluxDBClient object."""
342359
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -409,6 +426,15 @@ def test_write_points_bad_precision(self):
409426
time_precision='g'
410427
)
411428

429+
def test_write_points_bad_consistency(self):
430+
"""Test write points w/bad consistency value."""
431+
cli = InfluxDBClient()
432+
with self.assertRaises(ValueError):
433+
cli.write_points(
434+
self.dummy_points,
435+
consistency='boo'
436+
)
437+
412438
@raises(Exception)
413439
def test_write_points_with_precision_fails(self):
414440
"""Test write points w/precision fail for TestInfluxDBClient object."""

0 commit comments

Comments
 (0)