Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit dc54e5e

Browse files
RonRothmanxginn8
authored andcommitted
add consistency parameter to write_points (#664)
* add consistency parameter to write_points [#643]
1 parent 47aeb9b commit dc54e5e

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- query() now accepts a bind_params argument for parameter binding (#678 thx @clslgrnc)
1313

1414
### Changed
15+
- Add consistency param to InfluxDBClient.write_points (#643 thx @RonRothman)
1516
- Update test suite to add support for Python 3.7 and InfluxDB v1.6.4 and 1.7.4 (#692 thx @clslgrnc)
1617
- Update classifiers tuple to list in setup.py (#697 thx @Hanaasagi)
1718
- Update documentation for empty `delete_series` confusion

influxdb/client.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ def write_points(self,
458458
retention_policy=None,
459459
tags=None,
460460
batch_size=None,
461-
protocol='json'
461+
protocol='json',
462+
consistency=None
462463
):
463464
"""Write to multiple time series names.
464465
@@ -486,6 +487,9 @@ def write_points(self,
486487
:type batch_size: int
487488
:param protocol: Protocol for writing data. Either 'line' or 'json'.
488489
:type protocol: str
490+
:param consistency: Consistency for the points.
491+
One of {'any','one','quorum','all'}.
492+
:type consistency: str
489493
:returns: True, if the operation is successful
490494
:rtype: bool
491495
@@ -498,14 +502,16 @@ def write_points(self,
498502
time_precision=time_precision,
499503
database=database,
500504
retention_policy=retention_policy,
501-
tags=tags, protocol=protocol)
505+
tags=tags, protocol=protocol,
506+
consistency=consistency)
502507
return True
503508

504509
return self._write_points(points=points,
505510
time_precision=time_precision,
506511
database=database,
507512
retention_policy=retention_policy,
508-
tags=tags, protocol=protocol)
513+
tags=tags, protocol=protocol,
514+
consistency=consistency)
509515

510516
def ping(self):
511517
"""Check connectivity to InfluxDB.
@@ -531,12 +537,16 @@ def _write_points(self,
531537
database,
532538
retention_policy,
533539
tags,
534-
protocol='json'):
540+
protocol='json',
541+
consistency=None):
535542
if time_precision not in ['n', 'u', 'ms', 's', 'm', 'h', None]:
536543
raise ValueError(
537544
"Invalid time precision is given. "
538545
"(use 'n', 'u', 'ms', 's', 'm' or 'h')")
539546

547+
if consistency not in ['any', 'one', 'quorum', 'all', None]:
548+
raise ValueError('Invalid consistency: {}'.format(consistency))
549+
540550
if protocol == 'json':
541551
data = {
542552
'points': points
@@ -551,6 +561,9 @@ def _write_points(self,
551561
'db': database or self._database
552562
}
553563

564+
if consistency is not None:
565+
params['consistency'] = consistency
566+
554567
if time_precision is not None:
555568
params['precision'] = time_precision
556569

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)