Skip to content

Commit

Permalink
Small CLI fixes (#526)
Browse files Browse the repository at this point in the history
Fix ``flexmeasures db-ops dump`` and ``flexmeasures db-ops restore`` incorrectly reporting a success when `pg_dump` and `pg_restore` are not installed.


* Add missing closing tag

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Improve data input validation for CLI command flexmeasures delete sensor

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Subprocess should actually raise exception

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add util decorator for timing function execution

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Also fix db-ops restore

Signed-off-by: F.N. Claessen <felix@seita.nl>

* black

Signed-off-by: F.N. Claessen <felix@seita.nl>

* changelog entries

Signed-off-by: F.N. Claessen <felix@seita.nl>

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Nov 10, 2022
1 parent 296c6b7 commit bfcdb98
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -24,6 +24,7 @@ Infrastructure / Support

* Reduce size of Docker image (from 2GB to 1.4GB) [see `PR #512 <http://www.github.com/FlexMeasures/flexmeasures/pull/512>`_]
* Remove bokeh dependency and obsolete UI views [see `PR #476 <http://www.github.com/FlexMeasures/flexmeasures/pull/476>`_]
* Fix ``flexmeasures db-ops dump`` and ``flexmeasures db-ops restore`` incorrectly reporting a success when `pg_dump` and `pg_restore` are not installed [see `PR #526 <http://www.github.com/FlexMeasures/flexmeasures/pull/526>`_]
* Plugins can save BeliefsSeries, too, instead of just BeliefsDataFrames [see `PR #523 <http://www.github.com/FlexMeasures/flexmeasures/pull/523>`_]


Expand Down
5 changes: 5 additions & 0 deletions documentation/cli/change_log.rst
Expand Up @@ -4,6 +4,11 @@
FlexMeasures CLI Changelog
**********************

since v0.12.0 | November xx, 2022
=================================

* Fix ``flexmeasures db-ops dump`` and ``flexmeasures db-ops restore`` incorrectly reporting a success when `pg_dump` and `pg_restore` are not installed.

since v0.11.0 | August 28, 2022
==============================

Expand Down
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

0 comments on commit bfcdb98

Please sign in to comment.