Skip to content
This repository has been archived by the owner on Feb 15, 2018. It is now read-only.

Commit

Permalink
Add live timeseries support for label timeseries
Browse files Browse the repository at this point in the history
  • Loading branch information
madninja committed Dec 15, 2016
1 parent 854ee68 commit fff573b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 46 deletions.
5 changes: 5 additions & 0 deletions helium_commander/commands/label.py
Expand Up @@ -7,6 +7,8 @@
ResourceParamType
)
from helium_commander.commands import metadata
from helium_commander.commands import timeseries


pass_client = click.make_pass_decorator(Client)

Expand Down Expand Up @@ -36,6 +38,9 @@ def list(client, label, **kwargs):
Label.display(client, labels, include=include)


cli.add_command(timeseries.cli(Label, history=False, writable=False))


@cli.command()
@click.argument('label')
@device_sort_option
Expand Down
93 changes: 48 additions & 45 deletions helium_commander/commands/timeseries.py
Expand Up @@ -8,7 +8,7 @@
pass_client = click.make_pass_decorator(Client)


def cli(cls, singleton=False):
def cli(cls, singleton=False, history=True, writable=True, live=True):
group = click.Group(name='timeseries',
short_help="Commands on timeseries readings.")
resource_type = cls._resource_type()
Expand All @@ -28,50 +28,53 @@ def wrapper(func):
return func
return wrapper

@group.command('list')
@_id_argument()
@list_options()
@click.option('--count', default=20,
help="the number of readings to fetch. Use -1 for all")
@pass_client
def _list(client, id=None, **kwargs):
"""Get timeseries readings."""
count = kwargs.pop('count', 20)
resource = _fetch_resource(client, id, **kwargs)
timeseries = resource.timeseries(**kwargs)
if count >= 0:
timeseries = islice(timeseries, count)
DataPoint.display(client, timeseries, **kwargs)

@group.command('create')
@_id_argument()
@post_options()
@pass_client
def _post(client, id=None, **kwargs):
"""Post timeseries readings."""
resource = _fetch_resource(client, id, **kwargs)
timeseries = resource.timeseries()
point = timeseries.create(**kwargs)
DataPoint.display(client, [point], **kwargs)

@group.command('live')
@_id_argument()
@list_options()
@pass_client
def _live(client, id=None, **kwargs):
"""Get live timeseries readings"""
output_format = kwargs.get('format', client.format)
if output_format == "tabular":
raise ValueError("Tabular format is not supported for live " +
"readings. Try --format csv " +
"as the first argument")
resource = _fetch_resource(client, id, **kwargs)
timeseries = resource.timeseries(**kwargs)
mapping = DataPoint.display_map(client)
with cls.display_writer(client, mapping, **kwargs) as writer:
with closing(timeseries.live()) as live:
for data_point in live:
writer.write_resources([data_point], mapping)
if history:
@group.command('list')
@_id_argument()
@list_options()
@click.option('--count', default=20,
help="the number of readings to fetch. Use -1 for all")
@pass_client
def _list(client, id=None, **kwargs):
"""Get timeseries readings."""
count = kwargs.pop('count', 20)
resource = _fetch_resource(client, id, **kwargs)
timeseries = resource.timeseries(**kwargs)
if count >= 0:
timeseries = islice(timeseries, count)
DataPoint.display(client, timeseries, **kwargs)

if writable:
@group.command('create')
@_id_argument()
@post_options()
@pass_client
def _post(client, id=None, **kwargs):
"""Post timeseries readings."""
resource = _fetch_resource(client, id, **kwargs)
timeseries = resource.timeseries()
point = timeseries.create(**kwargs)
DataPoint.display(client, [point], **kwargs)

if live:
@group.command('live')
@_id_argument()
@list_options()
@pass_client
def _live(client, id=None, **kwargs):
"""Get live timeseries readings."""
output_format = kwargs.get('format', client.format)
if output_format == "tabular":
raise ValueError("Tabular format is not supported for live " +
"readings. Try --format csv " +
"as the first argument")
resource = _fetch_resource(client, id, **kwargs)
timeseries = resource.timeseries(**kwargs)
mapping = DataPoint.display_map(client)
with cls.display_writer(client, mapping, **kwargs) as writer:
with closing(timeseries.live()) as live:
for data_point in live:
writer.write_resources([data_point], mapping)

return group

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -3,7 +3,7 @@
author = 'Helium'
author_email = 'hello@helium.com'
install_requires = [
'helium-python>=0.6.0',
'helium-python>=0.6.3',
'future>=0.15',
'futures>=3.0',
'terminaltables>=2.1.0',
Expand Down

0 comments on commit fff573b

Please sign in to comment.