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

feat(client): re-add support for 'show series' from legacy PR #357. Closes #353 #806

Merged
merged 3 commits into from
Apr 7, 2020
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
35 changes: 35 additions & 0 deletions influxdb/client.py
Original file line number Diff line number Diff line change
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,40 @@ def get_list_database(self):
"""
return list(self.query("SHOW DATABASES").get_points())

def get_list_series(self, database=None, measurement=None, tags=None):
"""
Query SHOW SERIES 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
60 changes: 60 additions & 0 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,66 @@ def test_get_list_measurements(self):
[{'name': 'cpu'}, {'name': 'disk'}]
)

def test_get_list_series(self):
"""Test get a list of series from the database."""
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):
"""Test get a list of series from the database by filter."""
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):
"""Test get a list of series from the database by tags."""
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):
"""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()

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
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,64 @@ 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",
"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
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