Skip to content

Commit

Permalink
637 data model visualization is broken (#638)
Browse files Browse the repository at this point in the history
* do not use a fallback viewer command, PIL makes good picks by itself and doesn't allow passing in a command anymore

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* Better UX for missing necessary param (either --uml or --schema is required)

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* switch to default choice being to show new data models, --deprecated replaces the --dev option

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* add changelog entry

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* actually reference PR Id in changelog, not issue Id

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* adapt comment which still mentioned --dev

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* no need to install dot from apt (is a dependency in the sqlalchemy_schemadisplay library), rename a variable for readability

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

---------

Signed-off-by: Nicolas Höning <nicolas@seita.nl>
  • Loading branch information
nhoening committed Apr 17, 2023
1 parent 67d2236 commit cf74312
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -84,6 +84,6 @@ upgrade-db:
show-data-model:
# This generates the data model, as currently written in code, as a PNG picture.
# Also try with --schema for the database model.
# With --dev, you'll see the currently experimental parts, as well.
# With --deprecated, you'll see the legacy models, and not their replacements.
# Use --help to learn more.
./flexmeasures/data/scripts/visualize_data_model.py --uml --dev
./flexmeasures/data/scripts/visualize_data_model.py --uml
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -27,6 +27,7 @@ Bugfixes
Infrastructure / Support
----------------------
* Sunset several API fields for `/sensors/<id>/schedules/trigger` (POST) that have moved into the ``flex-model`` or ``flex-context`` fields [see `PR #580 <https://www.github.com/FlexMeasures/flexmeasures/pull/580>`_]
* Fix broken `make show-data-model` command [see `PR #638 <https://www.github.com/FlexMeasures/flexmeasures/pull/638>`_]


v0.12.3 | February 28, 2023
Expand Down
56 changes: 27 additions & 29 deletions flexmeasures/data/scripts/visualize_data_model.py
@@ -1,6 +1,5 @@
#!/usr/bin/env python
import argparse
import os
import sys
from getpass import getpass
import inspect
Expand All @@ -27,8 +26,6 @@
"""

DEBUG = True
FALLBACK_VIEWER_CMD = "gwenview" # Use this program if none of the standard viewers
# (e.g. display) can be found. Can be overwritten as env var.

RELEVANT_MODULES = [
"task_runs",
Expand Down Expand Up @@ -60,7 +57,7 @@
"weather_sensor",
"weather_sensor_type",
]
RELEVANT_TABLES_DEV = [
RELEVANT_TABLES_NEW = [
"generic_asset_type",
"generic_asset",
"sensor",
Expand Down Expand Up @@ -104,9 +101,7 @@ def wrapper(*args, **kwargs):
except FileNotFoundError as fnfe:
if '"dot" not found in path' in str(fnfe):
print(fnfe)
print(
"Try this (on debian-based Linux): sudo apt install python3-pydot python3-pydot-ng graphviz"
)
print("Try this (on debian-based Linux): sudo apt install graphviz")
sys.exit(2)
else:
raise
Expand All @@ -115,18 +110,20 @@ def wrapper(*args, **kwargs):


@uses_dot
def create_schema_pic(pg_url, pg_user, pg_pwd, store: bool = False, dev: bool = False):
def create_schema_pic(
pg_url, pg_user, pg_pwd, store: bool = False, deprecated: bool = False
):
"""Create a picture of the SCHEMA of relevant tables."""
print("CREATING SCHEMA PICTURE ...")
print(
f"Connecting to database {pg_url} as user {pg_user} and loading schema metadata ..."
)
db_metadata = MetaData(f"postgresql://{pg_user}:{pg_pwd}@{pg_url}")
relevant_tables = RELEVANT_TABLES
if dev:
relevant_tables += RELEVANT_TABLES_DEV
else:
if deprecated:
relevant_tables += LEGACY_TABLES
else:
relevant_tables += RELEVANT_TABLES_NEW
kwargs = dict(
metadata=db_metadata,
show_datatypes=False, # The image would get nasty big if we'd show the datatypes
Expand All @@ -143,11 +140,11 @@ def create_schema_pic(pg_url, pg_user, pg_pwd, store: bool = False, dev: bool =
print("Storing as image (db_schema.png) ...")
graph.write_png("db_schema.png") # write out the file
else:
show_image(graph, fb_viewer_command=FALLBACK_VIEWER_CMD)
show_image(graph)


@uses_dot
def create_uml_pic(store: bool = False, dev: bool = False):
def create_uml_pic(store: bool = False, deprecated: bool = False):
print("CREATING UML CODE DIAGRAM ...")
print("Finding all the relevant mappers in our model...")
mappers = []
Expand All @@ -166,10 +163,10 @@ def create_uml_pic(store: bool = False, dev: bool = False):
}
)
relevant_tables = RELEVANT_TABLES
if dev:
relevant_tables += RELEVANT_TABLES_DEV
else:
if deprecated:
relevant_tables += LEGACY_TABLES
else:
relevant_tables += RELEVANT_TABLES_NEW
if DEBUG:
print(f"Relevant tables: {relevant_tables}")
print(f"Relevant models: {relevant_models}")
Expand All @@ -192,11 +189,11 @@ def create_uml_pic(store: bool = False, dev: bool = False):
print("Storing as image (uml_diagram.png) ...")
graph.write_png("uml_diagram.png") # write out the file
else:
show_image(graph, fb_viewer_command=FALLBACK_VIEWER_CMD)
show_image(graph)


@uses_dot
def show_image(graph, fb_viewer_command: str):
def show_image(graph):
"""
Show an image created through sqlalchemy_schemadisplay.
Expand All @@ -219,9 +216,7 @@ def show_image(graph, fb_viewer_command: str):
iostream = BytesIO(graph.create_png())

print("Showing image ...")
if DEBUG:
print("(fallback viewer is %s)" % fb_viewer_command)
Image.open(iostream).show(command=fb_viewer_command)
Image.open(iostream).show()


if __name__ == "__main__":
Expand All @@ -245,9 +240,9 @@ def show_image(graph, fb_viewer_command: str):
help="Visualize the relationships available in code (UML style).",
)
parser.add_argument(
"--dev",
"--deprecated",
action="store_true",
help="If given, include the parts of the new data model which are in development.",
help="If given, include the parts of the depcrecated data model, and leave out their new counterparts.",
)
parser.add_argument(
"--store",
Expand All @@ -267,20 +262,23 @@ def show_image(graph, fb_viewer_command: str):

args = parser.parse_args()

if args.store is False:
FALLBACK_VIEWER_CMD = os.environ.get("FALLBACK_VIEWER_CMD", FALLBACK_VIEWER_CMD)

if args.schema:
pg_pwd = getpass(f"Please input the postgres password for user {args.pg_user}:")
create_schema_pic(
args.pg_url, args.pg_user, pg_pwd, store=args.store, dev=args.dev
args.pg_url,
args.pg_user,
pg_pwd,
store=args.store,
deprecated=args.deprecated,
)
if args.uml:
elif args.uml:
try:
from flexmeasures.data import db as flexmeasures_db
except ImportError as ie:
print(
f"We need flexmeasures.data to be in the path, so we can read the data model. Error: '{ie}''."
)
sys.exit(0)
create_uml_pic(store=args.store, dev=args.dev)
create_uml_pic(store=args.store, deprecated=args.deprecated)
else:
print("Please specify either --uml or --schema. What do you want to see?")

0 comments on commit cf74312

Please sign in to comment.