/
data_show.py
321 lines (292 loc) · 9.59 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""CLI Tasks for listing database contents - most useful in development"""
from typing import Optional, List
from datetime import datetime, timedelta
import click
from flask import current_app as app
from flask.cli import with_appcontext
from tabulate import tabulate
from humanize import naturaldelta, naturaltime
import uniplot
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, TimedBelief
from flexmeasures.data.schemas.generic_assets import GenericAssetIdField
from flexmeasures.data.schemas.sensors import SensorIdField
from flexmeasures.data.schemas.account import AccountIdField
from flexmeasures.data.schemas.sources import DataSourceIdField
from flexmeasures.data.schemas.times import AwareDateTimeField, DurationField
@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", type=AccountIdField(), required=True)
def show_account(account):
"""
Show information about an account, including users and assets.
"""
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", type=GenericAssetIdField(), required=True)
def show_generic_asset(asset):
"""
Show asset info and list sensors
"""
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"],
)
)
@fm_show_data.command("beliefs")
@with_appcontext
@click.option(
"--sensor-id",
"sensors",
required=True,
multiple=True,
type=SensorIdField(),
help="ID of sensor(s). This argument can be given multiple times.",
)
@click.option(
"--start",
"start",
type=AwareDateTimeField(),
required=True,
help="Plot starting at this datetime. Follow up with a timezone-aware datetime in ISO 6801 format.",
)
@click.option(
"--duration",
"duration",
type=DurationField(),
required=True,
help="Duration of the plot, after --start. Follow up with a duration in ISO 6801 format, e.g. PT1H (1 hour) or PT45M (45 minutes).",
)
@click.option(
"--belief-time-before",
"belief_time_before",
type=AwareDateTimeField(),
required=False,
help="Time at which beliefs had been known. Follow up with a timezone-aware datetime in ISO 6801 format.",
)
@click.option(
"--source-id",
"source",
required=False,
type=DataSourceIdField(),
help="Source of the beliefs (an existing source id).",
)
def plot_beliefs(
sensors: List[Sensor],
start: datetime,
duration: timedelta,
belief_time_before: Optional[datetime],
source: Optional[DataSource],
):
"""
Show a simple plot of belief data directly in the terminal.
"""
sensors = list(sensors)
min_resolution = min([s.event_resolution for s in sensors])
# query data
beliefs_by_sensor = TimedBelief.search(
sensors=sensors,
event_starts_after=start,
event_ends_before=start + duration,
beliefs_before=belief_time_before,
source=source,
one_deterministic_belief_per_event=True,
resolution=min_resolution,
sum_multiple=False,
)
# only keep non-empty
for s in sensors:
if beliefs_by_sensor[s.name].empty:
click.echo(f"No data found for sensor '{s.name}' (Id: {s.id})")
beliefs_by_sensor.pop(s.name)
sensors.remove(s)
if len(beliefs_by_sensor.keys()) == 0:
click.echo("No data found!")
raise click.Abort()
first_df = beliefs_by_sensor[sensors[0].name]
# Build title
if len(sensors) == 1:
title = f"Beliefs for Sensor '{sensors[0].name}' (Id {sensors[0].id}).\n"
else:
title = f"Beliefs for Sensor(s) [{','.join([s.name for s in sensors])}], (Id(s): [{','.join([str(s.id) for s in sensors])}]).\n"
title += f"Data spans {naturaldelta(duration)} and starts at {start}."
if belief_time_before:
title += f"\nOnly beliefs made before: {belief_time_before}."
if source:
title += f"\nSource: {source.description}"
title += f"\nThe time resolution (x-axis) is {naturaldelta(min_resolution)}."
uniplot.plot(
[
beliefs.event_value
for beliefs in [beliefs_by_sensor[sn] for sn in [s.name for s in sensors]]
],
title=title,
color=True,
lines=True,
y_unit=first_df.sensor.unit
if len(beliefs_by_sensor) == 1
or all(sensor.unit == first_df.sensor.unit for sensor in sensors)
else "",
legend_labels=[s.name for s in sensors],
)
app.cli.add_command(fm_show_data)