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

Commit ea1b995

Browse files
authored
feat(client): re-add support for 'show series' from legacy PR #357. Closes #353 (#806)
* feat(client): re-add support for 'show series' from legacy PR #357. Closes #353 * chore(client): fix failing tests * chore(client): update linters to pass
1 parent cb15c2e commit ea1b995

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added
10+
11+
### Changed
12+
13+
## [v5.2.4] - 2020-04-10
14+
915
### Added
1016
- Add mypy testing framework (#756)
1117
- Add support for messagepack (#734 thx @lovasoa)
18+
- Add support for 'show series' (#357 thx @gaker)
1219

1320
### Changed
1421
- Clean up stale CI config (#755)

influxdb/client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import unicode_literals
88

99
import datetime
10+
import itertools
1011
import json
1112
import random
1213
import socket
@@ -637,6 +638,40 @@ def get_list_database(self):
637638
"""
638639
return list(self.query("SHOW DATABASES").get_points())
639640

641+
def get_list_series(self, database=None, measurement=None, tags=None):
642+
"""
643+
Query SHOW SERIES returns the distinct series in your database.
644+
645+
FROM and WHERE clauses are optional.
646+
647+
:param measurement: Show all series from a measurement
648+
:type id: string
649+
:param tags: Show all series that match given tags
650+
:type id: dict
651+
:param database: the database from which the series should be
652+
shows, defaults to client's current database
653+
:type database: str
654+
"""
655+
database = database or self._database
656+
query_str = 'SHOW SERIES'
657+
658+
if measurement:
659+
query_str += ' FROM "{0}"'.format(measurement)
660+
661+
if tags:
662+
query_str += ' WHERE ' + ' and '.join(["{0}='{1}'".format(k, v)
663+
for k, v in tags.items()])
664+
665+
return list(
666+
itertools.chain.from_iterable(
667+
[
668+
x.values()
669+
for x in (self.query(query_str, database=database)
670+
.get_points())
671+
]
672+
)
673+
)
674+
640675
def create_database(self, dbname):
641676
"""Create a new database in InfluxDB.
642677

influxdb/tests/client_test.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,66 @@ def test_get_list_measurements(self):
689689
[{'name': 'cpu'}, {'name': 'disk'}]
690690
)
691691

692+
def test_get_list_series(self):
693+
"""Test get a list of series from the database."""
694+
data = {'results': [
695+
{'series': [
696+
{
697+
'values': [
698+
['cpu_load_short,host=server01,region=us-west'],
699+
['memory_usage,host=server02,region=us-east']],
700+
'columns': ['key']
701+
}
702+
]}
703+
]}
704+
705+
with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
706+
self.assertListEqual(
707+
self.cli.get_list_series(),
708+
['cpu_load_short,host=server01,region=us-west',
709+
'memory_usage,host=server02,region=us-east'])
710+
711+
def test_get_list_series_with_measurement(self):
712+
"""Test get a list of series from the database by filter."""
713+
data = {'results': [
714+
{'series': [
715+
{
716+
'values': [
717+
['cpu_load_short,host=server01,region=us-west']],
718+
'columns': ['key']
719+
}
720+
]}
721+
]}
722+
723+
with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
724+
self.assertListEqual(
725+
self.cli.get_list_series(measurement='cpu_load_short'),
726+
['cpu_load_short,host=server01,region=us-west'])
727+
728+
def test_get_list_series_with_tags(self):
729+
"""Test get a list of series from the database by tags."""
730+
data = {'results': [
731+
{'series': [
732+
{
733+
'values': [
734+
['cpu_load_short,host=server01,region=us-west']],
735+
'columns': ['key']
736+
}
737+
]}
738+
]}
739+
740+
with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
741+
self.assertListEqual(
742+
self.cli.get_list_series(tags={'region': 'us-west'}),
743+
['cpu_load_short,host=server01,region=us-west'])
744+
745+
@raises(Exception)
746+
def test_get_list_series_fails(self):
747+
"""Test get a list of series from the database but fail."""
748+
cli = InfluxDBClient('host', 8086, 'username', 'password')
749+
with _mocked_session(cli, 'get', 401):
750+
cli.get_list_series()
751+
692752
def test_create_retention_policy_default(self):
693753
"""Test create default ret policy for TestInfluxDBClient object."""
694754
example_response = '{"results":[{}]}'

influxdb/tests/server_tests/client_test_with_server.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,64 @@ def test_query_multiple_series(self):
817817
]
818818
self.cli.write_points(pts)
819819

820+
def test_get_list_series(self):
821+
"""Test get a list of series from the database."""
822+
dummy_points = [
823+
{
824+
"measurement": "cpu_load_short",
825+
"tags": {
826+
"host": "server01",
827+
"region": "us-west"
828+
},
829+
"time": "2009-11-10T23:00:00.123456Z",
830+
"fields": {
831+
"value": 0.64
832+
}
833+
}
834+
]
835+
836+
dummy_points_2 = [
837+
{
838+
"measurement": "memory_usage",
839+
"tags": {
840+
"host": "server02",
841+
"region": "us-east"
842+
},
843+
"time": "2009-11-10T23:00:00.123456Z",
844+
"fields": {
845+
"value": 80
846+
}
847+
}
848+
]
849+
850+
self.cli.write_points(dummy_points)
851+
self.cli.write_points(dummy_points_2)
852+
853+
self.assertEquals(
854+
self.cli.get_list_series(),
855+
['cpu_load_short,host=server01,region=us-west',
856+
'memory_usage,host=server02,region=us-east']
857+
)
858+
859+
self.assertEquals(
860+
self.cli.get_list_series(measurement='memory_usage'),
861+
['memory_usage,host=server02,region=us-east']
862+
)
863+
864+
self.assertEquals(
865+
self.cli.get_list_series(measurement='memory_usage'),
866+
['memory_usage,host=server02,region=us-east']
867+
)
868+
869+
self.assertEquals(
870+
self.cli.get_list_series(tags={'host': 'server02'}),
871+
['memory_usage,host=server02,region=us-east'])
872+
873+
self.assertEquals(
874+
self.cli.get_list_series(
875+
measurement='cpu_load_short', tags={'host': 'server02'}),
876+
[])
877+
820878

821879
@skip_server_tests
822880
class UdpTests(ManyTestCasesWithServerMixin, unittest.TestCase):

0 commit comments

Comments
 (0)