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

Let save_to_db support saving BeliefsSeries, too #523

Merged
merged 5 commits into from Nov 10, 2022
Merged
Changes from 2 commits
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
16 changes: 10 additions & 6 deletions flexmeasures/data/utils.py
@@ -1,15 +1,15 @@
from typing import List, Optional, Union
from __future__ import annotations

from flask import current_app
from timely_beliefs import BeliefsDataFrame
from timely_beliefs import BeliefsDataFrame, BeliefsSeries

from flexmeasures.data import db
from flexmeasures.data.models.data_sources import DataSource
from flexmeasures.data.models.time_series import TimedBelief
from flexmeasures.data.services.time_series import drop_unchanged_beliefs


def save_to_session(objects: List[db.Model], overwrite: bool = False):
def save_to_session(objects: list[db.Model], overwrite: bool = False):
"""Utility function to save to database, either efficiently with a bulk save, or inefficiently with a merge save."""
if not overwrite:
db.session.bulk_save_objects(objects)
Expand All @@ -20,8 +20,8 @@ def save_to_session(objects: List[db.Model], overwrite: bool = False):

def get_data_source(
data_source_name: str,
data_source_model: Optional[str] = None,
data_source_version: Optional[str] = None,
data_source_model: str | None = None,
data_source_version: str | None = 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.
Expand Down Expand Up @@ -50,7 +50,7 @@ def get_data_source(


def save_to_db(
data: Union[BeliefsDataFrame, List[BeliefsDataFrame]],
data: BeliefsDataFrame | BeliefsSeries | list[BeliefsDataFrame | BeliefsSeries],
bulk_save_objects: bool = False,
save_changed_beliefs_only: bool = True,
) -> str:
Expand Down Expand Up @@ -101,6 +101,10 @@ def save_to_db(
# Nothing to save
continue

# Convert series to frame if needed
if isinstance(timed_values, BeliefsSeries):
timed_values = timed_values.rename("event_value").to_frame()

len_before = len(timed_values)
if save_changed_beliefs_only:

Expand Down