Skip to content

Commit 5319e87

Browse files
RonRothmanLloyd Wallis
authored and
Lloyd Wallis
committed
add consistency parameter to write_points (influxdata#664)
* add consistency parameter to write_points [influxdata#643]
1 parent b51f433 commit 5319e87

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
@@ -472,7 +472,8 @@ def write_points(self,
472472
retention_policy=None,
473473
tags=None,
474474
batch_size=None,
475-
protocol='json'
475+
protocol='json',
476+
consistency=None
476477
):
477478
"""Write to multiple time series names.
478479
@@ -500,6 +501,9 @@ def write_points(self,
500501
:type batch_size: int
501502
:param protocol: Protocol for writing data. Either 'line' or 'json'.
502503
:type protocol: str
504+
:param consistency: Consistency for the points.
505+
One of {'any','one','quorum','all'}.
506+
:type consistency: str
503507
:returns: True, if the operation is successful
504508
:rtype: bool
505509
@@ -512,14 +516,16 @@ def write_points(self,
512516
time_precision=time_precision,
513517
database=database,
514518
retention_policy=retention_policy,
515-
tags=tags, protocol=protocol)
519+
tags=tags, protocol=protocol,
520+
consistency=consistency)
516521
return True
517522

518523
return self._write_points(points=points,
519524
time_precision=time_precision,
520525
database=database,
521526
retention_policy=retention_policy,
522-
tags=tags, protocol=protocol)
527+
tags=tags, protocol=protocol,
528+
consistency=consistency)
523529

524530
def ping(self):
525531
"""Check connectivity to InfluxDB.
@@ -545,12 +551,16 @@ def _write_points(self,
545551
database,
546552
retention_policy,
547553
tags,
548-
protocol='json'):
554+
protocol='json',
555+
consistency=None):
549556
if time_precision not in ['n', 'u', 'ms', 's', 'm', 'h', None]:
550557
raise ValueError(
551558
"Invalid time precision is given. "
552559
"(use 'n', 'u', 'ms', 's', 'm' or 'h')")
553560

561+
if consistency not in ['any', 'one', 'quorum', 'all', None]:
562+
raise ValueError('Invalid consistency: {}'.format(consistency))
563+
554564
if protocol == 'json':
555565
data = {
556566
'points': points
@@ -565,6 +575,9 @@ def _write_points(self,
565575
'db': database or self._database
566576
}
567577

578+
if consistency is not None:
579+
params['consistency'] = consistency
580+
568581
if time_precision is not None:
569582
params['precision'] = time_precision
570583

influxdb/tests/client_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ def test_write_points_with_precision(self):
345345
m.last_request.body,
346346
)
347347

348+
def test_write_points_with_consistency(self):
349+
"""Test write points with consistency for TestInfluxDBClient object."""
350+
with requests_mock.Mocker() as m:
351+
m.register_uri(
352+
requests_mock.POST,
353+
'http://localhost:8086/write',
354+
status_code=204
355+
)
356+
357+
cli = InfluxDBClient(database='db')
358+
359+
cli.write_points(self.dummy_points, consistency='any')
360+
self.assertEqual(
361+
m.last_request.qs,
362+
{'db': ['db'], 'consistency': ['any']}
363+
)
364+
348365
def test_write_points_with_precision_udp(self):
349366
"""Test write points with precision for TestInfluxDBClient object."""
350367
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -417,6 +434,15 @@ def test_write_points_bad_precision(self):
417434
time_precision='g'
418435
)
419436

437+
def test_write_points_bad_consistency(self):
438+
"""Test write points w/bad consistency value."""
439+
cli = InfluxDBClient()
440+
with self.assertRaises(ValueError):
441+
cli.write_points(
442+
self.dummy_points,
443+
consistency='boo'
444+
)
445+
420446
@raises(Exception)
421447
def test_write_points_with_precision_fails(self):
422448
"""Test write points w/precision fail for TestInfluxDBClient object."""

0 commit comments

Comments
 (0)