/
data_show.py
221 lines (196 loc) · 6.33 KB
/
data_show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""CLI Tasks for listing database contents - most useful in development"""
import click
from flask import current_app as app
from flask.cli import with_appcontext
from tabulate import tabulate
from humanize import naturaldelta, naturaltime
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
@click.group("show")
def fm_show_data():
"""FlexMeasures: Show data."""
@fm_show_data.command("accounts")
@with_appcontext
def list_accounts():
"""
List all accounts on this FlexMeasures instance.
"""
accounts = Account.query.order_by(Account.name).all()
if not accounts:
click.echo("No accounts created yet.")
return
click.echo("All accounts on this FlexMeasures instance:\n ")
account_data = [
(
account.id,
account.name,
GenericAsset.query.filter(GenericAsset.account_id == account.id).count(),
)
for account in accounts
]
click.echo(tabulate(account_data, headers=["Id", "Name", "Assets"]))
@fm_show_data.command("roles")
@with_appcontext
def list_roles():
"""
Show available account an user roles
"""
account_roles = AccountRole.query.order_by(AccountRole.name).all()
if not account_roles:
click.echo("No account roles created yet.")
return
click.echo("Account roles:\n")
click.echo(
tabulate(
[(r.id, r.name, r.description) for r in account_roles],
headers=["Id", "Name", "Description"],
)
)
click.echo()
user_roles = Role.query.order_by(Role.name).all()
if not user_roles:
click.echo("No user roles created yet, not even admin.")
return
click.echo("User roles:\n")
click.echo(
tabulate(
[(r.id, r.name, r.description) for r in user_roles],
headers=["Id", "Name", "Description"],
)
)
@fm_show_data.command("account")
@with_appcontext
@click.option("--id", "account_id", type=int, required=True)
def show_account(account_id):
"""
Show information about an account, including users and assets.
"""
account: Account = Account.query.get(account_id)
if not account:
click.echo(f"No account with id {account_id} known.")
raise click.Abort
click.echo(f"========{len(account.name) * '='}==========")
click.echo(f"Account {account.name} (ID:{account.id}):")
click.echo(f"========{len(account.name) * '='}==========\n")
if account.account_roles:
click.echo(
f"Account role(s): {','.join([role.name for role in account.account_roles])}"
)
else:
click.echo("Account has no roles.")
click.echo()
users = User.query.filter_by(account_id=account_id).order_by(User.username).all()
if not users:
click.echo("No users in account ...")
else:
click.echo("All users:\n ")
user_data = [
(
user.id,
user.username,
user.email,
naturaltime(user.last_login_at),
"".join([role.name for role in user.roles]),
)
for user in users
]
click.echo(
tabulate(user_data, headers=["Id", "Name", "Email", "Last Login", "Roles"])
)
click.echo()
assets = (
GenericAsset.query.filter_by(account_id=account_id)
.order_by(GenericAsset.name)
.all()
)
if not assets:
click.echo("No assets in account ...")
else:
click.echo("All assets:\n ")
asset_data = [
(asset.id, asset.name, asset.generic_asset_type.name, asset.location)
for asset in assets
]
click.echo(tabulate(asset_data, headers=["Id", "Name", "Type", "Location"]))
@fm_show_data.command("asset-types")
@with_appcontext
def list_asset_types():
"""
Show available asset types
"""
asset_types = GenericAssetType.query.order_by(GenericAssetType.name).all()
if not asset_types:
click.echo("No asset types created yet.")
return
click.echo(
tabulate(
[(t.id, t.name, t.description) for t in asset_types],
headers=["Id", "Name", "Description"],
)
)
@fm_show_data.command("asset")
@with_appcontext
@click.option("--id", "asset_id", type=int, required=True)
def show_generic_asset(asset_id):
"""
Show asset info and list sensors
"""
asset = GenericAsset.query.get(asset_id)
if not asset:
click.echo(f"No asset with id {asset_id} known.")
raise click.Abort
click.echo(f"======{len(asset.name) * '='}==========")
click.echo(f"Asset {asset.name} (ID:{asset.id}):")
click.echo(f"======{len(asset.name) * '='}==========\n")
asset_data = [
(
asset.generic_asset_type.name,
asset.location,
"".join([f"{k}:{v}\n" for k, v in asset.attributes.items()]),
)
]
click.echo(tabulate(asset_data, headers=["Type", "Location", "Attributes"]))
click.echo()
sensors = (
Sensor.query.filter_by(generic_asset_id=asset_id).order_by(Sensor.name).all()
)
if not sensors:
click.echo("No sensors in asset ...")
return
click.echo("All sensors in asset:\n ")
sensor_data = [
(
sensor.id,
sensor.name,
sensor.unit,
naturaldelta(sensor.event_resolution),
sensor.timezone,
"".join([f"{k}:{v}\n" for k, v in sensor.attributes.items()]),
)
for sensor in sensors
]
click.echo(
tabulate(
sensor_data,
headers=["Id", "Name", "Unit", "Resolution", "Timezone", "Attributes"],
)
)
@fm_show_data.command("data-sources")
@with_appcontext
def list_data_sources():
"""
Show available data sources
"""
sources = DataSource.query.order_by(DataSource.name).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)