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

Small CLI fixes #526

Merged
merged 9 commits into from Nov 10, 2022
16 changes: 7 additions & 9 deletions flexmeasures/cli/data_delete.py
Expand Up @@ -12,6 +12,7 @@
from flexmeasures.data.models.generic_assets import GenericAsset
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.services.users import find_user_by_email, delete_user


Expand Down Expand Up @@ -123,7 +124,7 @@ def delete_asset_and_data(asset: GenericAsset, force: bool):
Delete an asset & also its sensors and data.
"""
if not force:
prompt = f"Delete {asset}, including all its sensors and data?"
prompt = f"Delete {asset.__repr__()}, including all its sensors and data?"
click.confirm(prompt, abort=True)
db.session.delete(asset)
db.session.commit()
Expand Down Expand Up @@ -295,21 +296,18 @@ def delete_nan_beliefs(sensor_id: Optional[int] = None):
@with_appcontext
@click.option(
"--id",
"sensor_id",
type=int,
"sensor",
type=SensorIdField(),
required=True,
help="Delete a single sensor and its (time series) data. Follow up with the sensor's ID.",
)
def delete_sensor(
sensor_id: int,
sensor: Sensor,
):
"""Delete a sensor and all beliefs about it."""
sensor = Sensor.query.get(sensor_id)
n = TimedBelief.query.filter(TimedBelief.sensor_id == sensor_id).delete()
n = TimedBelief.query.filter(TimedBelief.sensor_id == sensor.id).delete()
db.session.delete(sensor)
click.confirm(
f"Really delete sensor {sensor_id}, along with {n} beliefs?", abort=True
)
click.confirm(f"Delete {sensor.__repr__()}, along with {n} beliefs?", abort=True)
db.session.commit()


Expand Down
10 changes: 2 additions & 8 deletions flexmeasures/cli/db_ops.py
Expand Up @@ -95,10 +95,7 @@ def dump():
dump_filename = f"pgbackup_{db_name}_{time_of_saving}.dump"
command_for_dumping = f"pg_dump --no-privileges --no-owner --data-only --format=c --file={dump_filename} {db_uri}"
try:
proc = subprocess.Popen(command_for_dumping, shell=True) # , env={
# 'PGPASSWORD': DB_PASSWORD
# })
proc.wait()
subprocess.check_output(command_for_dumping, shell=True)
click.echo(f"db dump successful: saved to {dump_filename}")

except Exception as e:
Expand All @@ -125,10 +122,7 @@ def restore(file: str):
click.echo(f"Restoring {db_host_and_db_name} database from file {file}")
command_for_restoring = f"pg_restore -d {db_uri} {file}"
try:
proc = subprocess.Popen(command_for_restoring, shell=True) # , env={
# 'PGPASSWORD': DB_PASSWORD
# })
proc.wait()
subprocess.check_output(command_for_restoring, shell=True)
click.echo("db restore successful")

except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/data/models/user.py
Expand Up @@ -67,7 +67,7 @@ class Account(db.Model, AuthModelMixin):
)

def __repr__(self):
return "<Account %s (ID:%s)" % (self.name, self.id)
return "<Account %s (ID:%s)>" % (self.name, self.id)

def __acl__(self):
"""
Expand Down
15 changes: 15 additions & 0 deletions flexmeasures/utils/coding_utils.py
@@ -1,4 +1,5 @@
import functools
import time


def make_registering_decorator(foreign_decorator):
Expand Down Expand Up @@ -114,3 +115,17 @@ def real_decorator(decoratee):
def sort_dict(unsorted_dict: dict) -> dict:
sorted_dict = dict(sorted(unsorted_dict.items(), key=lambda item: item[0]))
return sorted_dict


def timeit(func):
"""Decorator for printing the time it took to execute the decorated function."""

@functools.wraps(func)
def new_func(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
elapsed_time = time.time() - start_time
print(f"{func.__name__} finished in {int(elapsed_time * 1_000)} ms")
return result

return new_func