From c694027dedae3a4f27ab2420dde1f4041867670b Mon Sep 17 00:00:00 2001 From: Sebastian Borza Date: Tue, 7 Apr 2020 15:13:11 -0500 Subject: [PATCH 1/3] feat(client): re-add support for 'show series' from legacy PR #357. Closes #353 --- CHANGELOG.md | 7 +++ influxdb/client.py | 26 +++++++++ influxdb/tests/client_test.py | 58 +++++++++++++++++++ .../server_tests/client_test_with_server.py | 58 +++++++++++++++++++ 4 files changed, 149 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13445e97..bdc08569 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +### Changed + +## [v5.2.4] - 2020-04-10 + ### Added - Add mypy testing framework (#756) - Add support for messagepack (#734 thx @lovasoa) +- Add support for 'show series' (#357 thx @gaker) ### Changed - Clean up stale CI config (#755) diff --git a/influxdb/client.py b/influxdb/client.py index 5e39f490..4d1c7c5e 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -7,6 +7,7 @@ from __future__ import unicode_literals import datetime +import itertools import json import random import socket @@ -637,6 +638,31 @@ def get_list_database(self): """ return list(self.query("SHOW DATABASES").get_points()) + def get_list_series(self, database=None, measurement=None, tags=None): + """ + The SHOW SERIES query returns the distinct series in your database. + FROM and WHERE clauses are optional. + + :param measurement: Show all series from a measurement + :type id: string + :param tags: Show all series that match given tags + :type id: dict + :param database: the database from which the series should be + shows, defaults to client's current database + :type database: str + """ + database = database or self._database + query_str = 'SHOW SERIES' + + if measurement: + query_str += ' FROM "{0}"'.format(measurement) + + if tags: + query_str += ' WHERE ' + ' and '.join(["{0}='{1}'".format(k, v) + for k, v in tags.items()]) + + return list(itertools.chain.from_iterable([x.values() for x in (self.query(query_str, database=database).get_points())])) + def create_database(self, dbname): """Create a new database in InfluxDB. diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 54116f7e..7c20c4eb 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -689,6 +689,64 @@ def test_get_list_measurements(self): [{'name': 'cpu'}, {'name': 'disk'}] ) + def test_get_list_series(self): + + data = {'results': [ + {'series': [ + { + 'values': [ + ['cpu_load_short,host=server01,region=us-west'], + ['memory_usage,host=server02,region=us-east']], + 'columns': ['key'] + } + ]} + ]} + + with _mocked_session(self.cli, 'get', 200, json.dumps(data)): + self.assertListEqual( + self.cli.get_list_series(), + [{'key': 'cpu_load_short,host=server01,region=us-west'}, + {'key': 'memory_usage,host=server02,region=us-east'}]) + + def test_get_list_series_with_measurement(self): + + data = {'results': [ + {'series': [ + { + 'values': [ + ['cpu_load_short,host=server01,region=us-west']], + 'columns': ['key'] + } + ]} + ]} + + with _mocked_session(self.cli, 'get', 200, json.dumps(data)): + self.assertListEqual( + self.cli.get_list_series(measurement='cpu_load_short'), + ['cpu_load_short,host=server01,region=us-west']) + + def test_get_list_series_with_tags(self): + data = {'results': [ + {'series': [ + { + 'values': [ + ['cpu_load_short,host=server01,region=us-west']], + 'columns': ['key'] + } + ]} + ]} + + with _mocked_session(self.cli, 'get', 200, json.dumps(data)): + self.assertListEqual( + self.cli.get_list_series(tags={'region': 'us-west'}), + ['cpu_load_short,host=server01,region=us-west']) + + @raises(Exception) + def test_get_list_series_fails(self): + cli = InfluxDBClient('host', 8086, 'username', 'password') + with _mocked_session(cli, 'get', 401): + cli.get_list_series() + def test_create_retention_policy_default(self): """Test create default ret policy for TestInfluxDBClient object.""" example_response = '{"results":[{}]}' diff --git a/influxdb/tests/server_tests/client_test_with_server.py b/influxdb/tests/server_tests/client_test_with_server.py index fda3f720..754aa0db 100644 --- a/influxdb/tests/server_tests/client_test_with_server.py +++ b/influxdb/tests/server_tests/client_test_with_server.py @@ -817,6 +817,64 @@ def test_query_multiple_series(self): ] self.cli.write_points(pts) + def test_get_list_series(self): + + dummy_points = [ + { + "measurement": "cpu_load_short", + "tags": { + "host": "server01", + "region": "us-west" + }, + "time": "2009-11-10T23:00:00.123456Z", + "fields": { + "value": 0.64 + } + } + ] + + dummy_points_2 = [ + { + "measurement": "memory_usage", + "tags": { + "host": "server02", + "region": "us-east" + }, + "time": "2009-11-10T23:00:00.123456Z", + "fields": { + "value": 80 + } + } + ] + + self.cli.write_points(dummy_points) + self.cli.write_points(dummy_points_2) + + self.assertEquals( + self.cli.get_list_series(), + ['cpu_load_short,host=server01,region=us-west', + 'memory_usage,host=server02,region=us-east'] + ) + + self.assertEquals( + self.cli.get_list_series(measurement='memory_usage'), + ['memory_usage,host=server02,region=us-east'] + ) + + self.assertEquals( + self.cli.get_list_series(measurement='memory_usage'), + ['memory_usage,host=server02,region=us-east'] + ) + + self.assertEquals( + self.cli.get_list_series(tags={'host': 'server02'}), + ['memory_usage,host=server02,region=us-east']) + + self.assertEquals( + self.cli.get_list_series( + measurement='cpu_load_short', tags={'host': 'server02'}), + []) + @skip_server_tests class UdpTests(ManyTestCasesWithServerMixin, unittest.TestCase): From 77451106cbf3492a71a4487241a1946f23dc6e43 Mon Sep 17 00:00:00 2001 From: Sebastian Borza Date: Tue, 7 Apr 2020 15:26:32 -0500 Subject: [PATCH 2/3] chore(client): fix failing tests --- influxdb/tests/client_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 7c20c4eb..37b1e92a 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -705,8 +705,8 @@ def test_get_list_series(self): with _mocked_session(self.cli, 'get', 200, json.dumps(data)): self.assertListEqual( self.cli.get_list_series(), - [{'key': 'cpu_load_short,host=server01,region=us-west'}, - {'key': 'memory_usage,host=server02,region=us-east'}]) + ['cpu_load_short,host=server01,region=us-west', + 'memory_usage,host=server02,region=us-east']) def test_get_list_series_with_measurement(self): From 4871a49a5ddee4ae5a7c96810acc2c5a3f2e40a8 Mon Sep 17 00:00:00 2001 From: Sebastian Borza Date: Tue, 7 Apr 2020 16:52:32 -0500 Subject: [PATCH 3/3] chore(client): update linters to pass --- influxdb/client.py | 13 +++++++++++-- influxdb/tests/client_test.py | 6 ++++-- .../tests/server_tests/client_test_with_server.py | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 4d1c7c5e..43427a11 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -640,7 +640,8 @@ def get_list_database(self): def get_list_series(self, database=None, measurement=None, tags=None): """ - The SHOW SERIES query returns the distinct series in your database. + Query SHOW SERIES returns the distinct series in your database. + FROM and WHERE clauses are optional. :param measurement: Show all series from a measurement @@ -661,7 +662,15 @@ def get_list_series(self, database=None, measurement=None, tags=None): query_str += ' WHERE ' + ' and '.join(["{0}='{1}'".format(k, v) for k, v in tags.items()]) - return list(itertools.chain.from_iterable([x.values() for x in (self.query(query_str, database=database).get_points())])) + return list( + itertools.chain.from_iterable( + [ + x.values() + for x in (self.query(query_str, database=database) + .get_points()) + ] + ) + ) def create_database(self, dbname): """Create a new database in InfluxDB. diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 37b1e92a..99a7f42b 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -690,7 +690,7 @@ def test_get_list_measurements(self): ) def test_get_list_series(self): - + """Test get a list of series from the database.""" data = {'results': [ {'series': [ { @@ -709,7 +709,7 @@ def test_get_list_series(self): 'memory_usage,host=server02,region=us-east']) def test_get_list_series_with_measurement(self): - + """Test get a list of series from the database by filter.""" data = {'results': [ {'series': [ { @@ -726,6 +726,7 @@ def test_get_list_series_with_measurement(self): ['cpu_load_short,host=server01,region=us-west']) def test_get_list_series_with_tags(self): + """Test get a list of series from the database by tags.""" data = {'results': [ {'series': [ { @@ -743,6 +744,7 @@ def test_get_list_series_with_tags(self): @raises(Exception) def test_get_list_series_fails(self): + """Test get a list of series from the database but fail.""" cli = InfluxDBClient('host', 8086, 'username', 'password') with _mocked_session(cli, 'get', 401): cli.get_list_series() diff --git a/influxdb/tests/server_tests/client_test_with_server.py b/influxdb/tests/server_tests/client_test_with_server.py index 754aa0db..94f28b66 100644 --- a/influxdb/tests/server_tests/client_test_with_server.py +++ b/influxdb/tests/server_tests/client_test_with_server.py @@ -818,7 +818,7 @@ def test_query_multiple_series(self): self.cli.write_points(pts) def test_get_list_series(self): - + """Test get a list of series from the database.""" dummy_points = [ { "measurement": "cpu_load_short", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy