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

Add get_list_measurements and drop_measurement #402

Merged
merged 4 commits into from
Sep 8, 2017
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
26 changes: 26 additions & 0 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,32 @@ def drop_database(self, dbname):
"""
self.query("DROP DATABASE {0}".format(quote_ident(dbname)))

def get_list_measurements(self):
"""Get the list of measurements in InfluxDB.

:returns: all measurements in InfluxDB
:rtype: list of dictionaries

:Example:

::

>> dbs = client.get_list_measurements()
>> dbs
[{u'name': u'measurements1'},
{u'name': u'measurements2'},
{u'name': u'measurements3'}]
"""
return list(self.query("SHOW MEASUREMENTS").get_points())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add support for on clause which was added in InfluxDB 1.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smolse

By checking InfluxDB 1.2 docs about show measurements.
There was more than one clause needed to support. on , with measurement, where, limit, offset.
Support all the clauses maybe coming from another PR, and it's better that we could discuss the interface of this feature.

Copy link
Contributor

@sebito91 sebito91 Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur with @Vic020, there are features added regularly that we'll need to keep up with. Since we're officially supporting the v1.2.4 release this is fine as-is.


def drop_measurement(self, measurement):
"""Drop a measurement from InfluxDB.

:param measurement: the name of the measurement to drop
:type measurement: str
"""
self.query("DROP MEASUREMENT {0}".format(quote_ident(measurement)))

def create_retention_policy(self, name, duration, replication,
database=None, default=False):
"""Create a retention policy for a database.
Expand Down
32 changes: 32 additions & 0 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ def test_drop_database(self):
'drop database "new_db"'
)

def test_drop_measurement(self):
"""Test drop measurement for TestInfluxDBClient object."""
with requests_mock.Mocker() as m:
m.register_uri(
requests_mock.GET,
"http://localhost:8086/query",
text='{"results":[{}]}'
)
self.cli.drop_measurement('new_measurement')
self.assertEqual(
m.last_request.qs['q'][0],
'drop measurement "new_measurement"'
)

def test_drop_numeric_named_database(self):
"""Test drop numeric db for TestInfluxDBClient object."""
with requests_mock.Mocker() as m:
Expand Down Expand Up @@ -504,6 +518,24 @@ def test_get_list_database_fails(self):
with _mocked_session(cli, 'get', 401):
cli.get_list_database()

def test_get_list_measurements(self):
"""Test get list of measurements for TestInfluxDBClient object."""
data = {
"results": [{
"series": [
{"name": "measurements",
"columns": ["name"],
"values": [["cpu"], ["disk"]
]}]}
]
}

with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
self.assertListEqual(
self.cli.get_list_measurements(),
[{'name': 'cpu'}, {'name': 'disk'}]
)

def test_create_retention_policy_default(self):
"""Test create default ret policy for TestInfluxDBClient object."""
example_response = '{"results":[{}]}'
Expand Down