8000 add consistency parameter to write_points by RonRothman · Pull Request #664 · influxdata/influxdb-python · GitHub
[go: up one dir, main page]

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

add consistency parameter to write_points #664

Merged
merged 5 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- query() now accepts a bind_params argument for parameter binding (#678 thx @clslgrnc)

### Changed
- Add consistency param to InfluxDBClient.write_points (#643 thx @RonRothman)
- Update test suite to add support for Python 3.7 and InfluxDB v1.6.4 and 1.7.4 (#692 thx @clslgrnc)

### Removed
Expand Down
21 changes: 17 additions & 4 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ def write_points(self,
retention_policy=None,
tags=None,
batch_size=None,
protocol='json'
protocol='json',
consistency=None
):
"""Write to multiple time series names.

Expand Down Expand Up @@ -486,6 +487,9 @@ def write_points(self,
:type batch_size: int
:param protocol: Protocol for writing data. Either 'line' or 'json'.
:type protocol: str
:param consistency: Consistency for the points.
One of {'any','one','quorum','all'}.
:type consistency: str
:returns: True, if the operation is successful
:rtype: bool

Expand All @@ -498,14 +502,16 @@ def write_points(self,
time_precision=time_precision,
database=database,
retention_policy=retention_policy,
tags=tags, protocol=protocol)
tags=tags, protocol=protocol,
consistency=consistency)
return True

return self._write_points(points=points,
time_precision=time_precision,
database=database,
retention_policy=retention_policy,
tags=tags, protocol=protocol)
tags=tags, protocol=protocol,
consistency=consistency)

def ping(self):
"""Check connectivity to InfluxDB.
Expand All @@ -531,12 +537,16 @@ def _write_points(self,
database,
retention_policy,
tags,
protocol='json'):
protocol='json',
consistency=None):
if time_precision not in ['n', 'u', 'ms', 's', 'm', 'h', None]:
raise ValueError(
"Invalid time precision is given. "
"(use 'n', 'u', 'ms', 's', 'm' or 'h')")

if consistency not in ['any', 'one', 'quorum', 'all', None]:
raise ValueError('Invalid consistency: {}'.format(consistency))

if protocol == 'json':
data = {
'points': points
Expand All @@ -551,6 +561,9 @@ def _write_points(self,
'db': database or self._database
}

if consistency is not None:
params['consistency'] = consistency

if time_precision is not None:
params['precision'] = time_precision

Expand Down
26 changes: 26 additions & 0 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,23 @@ def test_write_points_with_precision(self):
m.last_request.body,
)

def test_write_points_with_consistency(self):
"""Test write points with consistency for TestInfluxDBClient object."""
with requests_mock.Mocker() as m:
m.register_uri(
requests_mock.POST,
'http://localhost:8086/write',
status_code=204
)

cli = InfluxDBClient(database='db')

cli.write_points(self.dummy_points, consistency='any')
self.assertEqual(
m.last_request.qs,
{'db': ['db'], 'consistency': ['any']}
)

def test_write_points_with_precision_udp(self):
"""Test write points with precision for TestInfluxDBClient object."""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Expand Down Expand Up @@ -409,6 +426,15 @@ def test_write_points_bad_precision(self):
time_precision='g'
)

def test_write_points_bad_consistency(self):
"""Test write points w/bad consistency value."""
cli = InfluxDBClient()
with self.assertRaises(ValueError):
cli.write_points(
self.dummy_points,
consistency='boo'
)

@raises(Exception)
def test_write_points_with_precision_fails(self):
"""Test write points w/precision fail for TestInfluxDBClient object."""
Expand Down
0