Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flexmeasures show CLI commands #339

Merged
merged 6 commits into from Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion documentation/cli/change_log.rst
Expand Up @@ -7,7 +7,7 @@ FlexMeasures CLI Changelog
since v0.9.0 | January 26, 2022
=====================

* add CLI comands for showing data ``flexmeasures show accounts``, ``flexmeasures show account``, ``flexmeasures show roles``, ``flexmeasures show asset-types`` and ``flexmeasures show asset``.
* add CLI comands for showing data ``flexmeasures show accounts``, ``flexmeasures show account``, ``flexmeasures show roles``, ``flexmeasures show asset-types``, ``flexmeasures show asset`` and ``flexmeasures show data-sources``.


since v0.6.0 | April 2, 2021
Expand Down
1 change: 1 addition & 0 deletions documentation/cli/commands.rst
Expand Up @@ -44,6 +44,7 @@ of which some are referred to in this documentation.
``flexmeasures show asset-types` List available asset types.
``flexmeasures show asset`` Show an asset and its sensors.
``flexmeasures show roles`` List available account- and user roles.
``flexmeasures show data-sources`` List available data sources.
================================================= =======================================


Expand Down
39 changes: 32 additions & 7 deletions flexmeasures/cli/data_show.py
Expand Up @@ -6,6 +6,7 @@
from tabulate import tabulate

from flexmeasures.data.models.user import Account, AccountRole, User, Role
from flexmeasures.data.models.data_sources import DataSource
from flexmeasures.data.models.generic_assets import GenericAsset, GenericAssetType
from flexmeasures.data.models.time_series import Sensor

Expand All @@ -21,7 +22,7 @@ def list_accounts():
"""
List all accounts on this FlexMeasures instance.
"""
accounts = Account.query.all()
accounts = Account.query.order_by(Account.id).all()
nhoening marked this conversation as resolved.
Show resolved Hide resolved
if not accounts:
click.echo("No accounts created yet.")
return
Expand All @@ -43,7 +44,7 @@ def list_roles():
"""
Show available account an user roles
"""
account_roles = AccountRole.query.all()
account_roles = AccountRole.query.order_by(AccountRole.id).all()
if not account_roles:
click.echo("No account roles created yet.")
return
Expand All @@ -55,7 +56,7 @@ def list_roles():
)
)
click.echo()
user_roles = Role.query.all()
user_roles = Role.query.order_by(Role.id).all()
if not user_roles:
click.echo("No user roles created yet, not even admin.")
return
Expand Down Expand Up @@ -92,7 +93,7 @@ def show_account(account_id):
click.echo("Account has no roles.")
click.echo()

users = User.query.filter_by(account_id=account_id).all()
users = User.query.filter_by(account_id=account_id).order_by(User.id).all()
if not users:
click.echo("No users in account ...")
else:
Expand All @@ -112,7 +113,11 @@ def show_account(account_id):
)

click.echo()
assets = GenericAsset.query.filter_by(account_id=account_id).all()
assets = (
GenericAsset.query.filter_by(account_id=account_id)
.order_by(GenericAsset.id)
.all()
)
if not assets:
click.echo("No assets in account ...")
else:
Expand All @@ -130,7 +135,7 @@ def list_asset_types():
"""
Show available asset types
"""
asset_types = GenericAssetType.query.all()
asset_types = GenericAssetType.query.order_by(GenericAssetType.id).all()
if not asset_types:
click.echo("No asset types created yet.")
return
Expand Down Expand Up @@ -168,7 +173,9 @@ def show_generic_asset(asset_id):
click.echo(tabulate(asset_data, headers=["Type", "Location", "Attributes"]))

click.echo()
sensors = Sensor.query.filter_by(generic_asset_id=asset_id).all()
sensors = (
Sensor.query.filter_by(generic_asset_id=asset_id).order_by(Sensor.id).all()
)
if not sensors:
click.echo("No sensors in asset ...")
return
Expand All @@ -192,4 +199,22 @@ def show_generic_asset(asset_id):
)


@fm_show_data.command("data-sources")
@with_appcontext
def list_data_sources():
"""
Show available data sources
"""
sources = DataSource.query.order_by(DataSource.id).all()
if not sources:
click.echo("No data sources created yet.")
return
click.echo(
tabulate(
[(s.id, s.name, s.type, s.user_id, s.model, s.version) for s in sources],
headers=["Id", "Name", "Type", "User Id", "Model", "Version"],
)
)


app.cli.add_command(fm_show_data)