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

Add optional model and version to DataSource #215

Merged
merged 6 commits into from Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,34 @@
"""add optional DataSource columns for model and version

Revision ID: 30fe2267e7d5
Revises: 96f2db5bed30
Create Date: 2021-10-11 10:54:24.348371

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "30fe2267e7d5"
down_revision = "96f2db5bed30"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"data_source", sa.Column("model", sa.String(length=80), nullable=True)
)
op.add_column(
"data_source", sa.Column("version", sa.String(length=17), nullable=True)
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("data_source", "version")
op.drop_column("data_source", "model")
# ### end Alembic commands ###
31 changes: 29 additions & 2 deletions flexmeasures/data/models/data_sources.py
Expand Up @@ -2,6 +2,7 @@

import timely_beliefs as tb
from flask import current_app
from sqlalchemy.ext.hybrid import hybrid_method

from flexmeasures.data.config import db
from flexmeasures.data.models.user import User, is_user
Expand All @@ -14,13 +15,20 @@ class DataSource(db.Model, tb.BeliefSourceDBMixin):

# The type of data source (e.g. user, forecasting script or scheduling script)
type = db.Column(db.String(80), default="")
# The id of the source (can link e.g. to fm_user table)

# The id of the user source (can link e.g. to fm_user table)
user_id = db.Column(
db.Integer, db.ForeignKey("fm_user.id"), nullable=True, unique=True
)

user = db.relationship("User", backref=db.backref("data_source", lazy=True))

# The model and version of a script source
model = db.Column(db.String(80), nullable=True)
version = db.Column(
db.String(17), # length supports up to version 999.999.999dev999
nullable=True,
)

def __init__(
self,
name: Optional[str] = None,
Expand Down Expand Up @@ -54,6 +62,25 @@ def label(self):
else:
return f"data from {self.name}"

@hybrid_method
def description(self, include_model: bool = True, include_version: bool = True):
"""Extended description

For example:

>>> DataSource("Seita", type="forecasting script", model="naive", version="1.2").description()
<<< "forecast by Seita (naive model v1.2)"

"""
descr = self.label
if include_model and self.model:
descr += f" ({self.model} model"
if include_version and self.version:
descr += f" v{self.version})"
else:
descr += ")"
return descr

def __repr__(self):
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
return "<Data source %r (%s)>" % (self.id, self.label)

Expand Down
32 changes: 24 additions & 8 deletions flexmeasures/data/utils.py
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Optional

import click

Expand All @@ -16,20 +16,36 @@ def save_to_session(objects: List[db.Model], overwrite: bool = False):


def get_data_source(
data_source_name: str, data_source_type: str = "script"
data_source_name: str,
data_source_model: Optional[str] = None,
data_source_version: Optional[str] = None,
data_source_type: str = "script",
) -> DataSource:
"""Make sure we have a data source. Create one if it doesn't exist, and add to session.
Meant for scripts that may run for the first time.
It should probably not be used in the middle of a transaction, because we commit to the session."""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no commit, just a flush.

"""

data_source = DataSource.query.filter_by(
name=data_source_name, type=data_source_type
name=data_source_name,
model=data_source_model,
version=data_source_version,
type=data_source_type,
).one_or_none()
if data_source is None:
data_source = DataSource(name=data_source_name, type=data_source_type)
data_source = DataSource(
name=data_source_name,
model=data_source_model,
version=data_source_version,
type=data_source_type,
)
db.session.add(data_source)
db.session.flush() # populate the primary key attributes (like id) without committing the transaction
click.echo(
f'Session updated with new {data_source_type} data source named "{data_source_name}".'
)
if data_source_model is None:
Flix6x marked this conversation as resolved.
Show resolved Hide resolved
click.echo(
f'Session updated with new {data_source_type} data source named "{data_source_name}".'
)
else:
click.echo(
f'Session updated with new {data_source_type} data source named "{data_source_name}" ({data_source_model} model v{data_source_version}).'
)
return data_source