Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Merge 7745110 into cb15c2e
Browse files Browse the repository at this point in the history
  • Loading branch information
sebito91 committed Apr 7, 2020
2 parents cb15c2e + 7745110 commit 59939eb
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions influxdb/client.py
Expand Up @@ -7,6 +7,7 @@
from __future__ import unicode_literals

import datetime
import itertools
import json
import random
import socket
Expand Down Expand Up @@ -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.
Expand Down
58 changes: 58 additions & 0 deletions influxdb/tests/client_test.py
Expand Up @@ -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(),
['cpu_load_short,host=server01,region=us-west',
'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":[{}]}'
Expand Down
58 changes: 58 additions & 0 deletions influxdb/tests/server_tests/client_test_with_server.py
Expand Up @@ -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):
Expand Down

0 comments on commit 59939eb

Please sign in to comment.