Skip to content

Commit df6e2fe

Browse files
Vic020sebito91
authored andcommitted
Add get_list_measurements and drop_measurement (influxdata#402)
* Add get_list_measurements and drop_measurement Dear maintainer, Look likes we are missing the get list measurements and drop measurement feature for client. pls accept this pr. * add test case * fix: dbname -> measurement * add docstring
1 parent 265d147 commit df6e2fe

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

influxdb/client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,32 @@ def drop_database(self, dbname):
547547
"""
548548
self.query("DROP DATABASE {0}".format(quote_ident(dbname)))
549549

550+
def get_list_measurements(self):
551+
"""Get the list of measurements in InfluxDB.
552+
553+
:returns: all measurements in InfluxDB
554+
:rtype: list of dictionaries
555+
556+
:Example:
557+
558+
::
559+
560+
>> dbs = client.get_list_measurements()
561+
>> dbs
562+
[{u'name': u'measurements1'},
563+
{u'name': u'measurements2'},
564+
{u'name': u'measurements3'}]
565+
"""
566+
return list(self.query("SHOW MEASUREMENTS").get_points())
567+
568+
def drop_measurement(self, measurement):
569+
"""Drop a measurement from InfluxDB.
570+
571+
:param measurement: the name of the measurement to drop
572+
:type measurement: str
573+
"""
574+
self.query("DROP MEASUREMENT {0}".format(quote_ident(measurement)))
575+
550576
def create_retention_policy(self, name, duration, replication,
551577
database=None, default=False):
552578
"""Create a retention policy for a database.

influxdb/tests/client_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,20 @@ def test_drop_database(self):
466466
'drop database "new_db"'
467467
)
468468

469+
def test_drop_measurement(self):
470+
"""Test drop measurement for TestInfluxDBClient object."""
471+
with requests_mock.Mocker() as m:
472+
m.register_uri(
473+
requests_mock.GET,
474+
"http://localhost:8086/query",
475+
text='{"results":[{}]}'
476+
)
477+
self.cli.drop_measurement('new_measurement')
478+
self.assertEqual(
479+
m.last_request.qs['q'][0],
480+
'drop measurement "new_measurement"'
481+
)
482+
469483
def test_drop_numeric_named_database(self):
470484
"""Test drop numeric db for TestInfluxDBClient object."""
471485
with requests_mock.Mocker() as m:
@@ -504,6 +518,24 @@ def test_get_list_database_fails(self):
504518
with _mocked_session(cli, 'get', 401):
505519
cli.get_list_database()
506520

521+
def test_get_list_measurements(self):
522+
"""Test get list of measurements for TestInfluxDBClient object."""
523+
data = {
524+
"results": [{
525+
"series": [
526+
{"name": "measurements",
527+
"columns": ["name"],
528+
"values": [["cpu"], ["disk"]
529+
]}]}
530+
]
531+
}
532+
533+
with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
534+
self.assertListEqual(
535+
self.cli.get_list_measurements(),
536+
[{'name': 'cpu'}, {'name': 'disk'}]
537+
)
538+
507539
def test_create_retention_policy_default(self):
508540
"""Test create default ret policy for TestInfluxDBClient object."""
509541
example_response = '{"results":[{}]}'

0 commit comments

Comments
 (0)