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

637 data model visualization is broken #638

Merged
merged 8 commits into from Apr 17, 2023
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -86,4 +86,4 @@ show-data-model:
# Also try with --schema for the database model.
# With --dev, you'll see the currently experimental parts, as well.
# 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 @@ -25,6 +25,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
50 changes: 25 additions & 25 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 @@ -115,18 +112,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_DEV
kwargs = dict(
metadata=db_metadata,
show_datatypes=False, # The image would get nasty big if we'd show the datatypes
Expand All @@ -143,11 +142,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 +165,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_DEV
if DEBUG:
print(f"Relevant tables: {relevant_tables}")
print(f"Relevant models: {relevant_models}")
Expand All @@ -192,11 +191,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 +218,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 +242,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 +264,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?")