Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions flexmeasures/data/models/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,16 @@ def get_or_create_annotation(
"""Add annotation to db session if it doesn't exist in the session already.

Return tuple of (annotation object, is_new boolean).
If annotation exists, return (existing_annotation, False).
Match the database identity (content, start, belief time, source, type).
If annotation exists, update its end and return (existing_annotation, False).
If annotation is new, return (new_annotation, True).
"""
with db.session.no_autoflush:
existing_annotation = db.session.execute(
select(Annotation).filter(
Annotation.content == annotation.content,
Annotation.start == annotation.start,
Annotation.end == annotation.end,
Annotation.belief_time == annotation.belief_time,
Annotation.source == annotation.source,
Annotation.type == annotation.type,
)
Expand All @@ -219,6 +220,7 @@ def get_or_create_annotation(
return annotation, True
if annotation in db.session:
db.session.expunge(annotation)
existing_annotation.end = annotation.end
return existing_annotation, False


Expand Down
48 changes: 48 additions & 0 deletions flexmeasures/data/tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,51 @@ def test_search_annotations_by_belief_time(db, setup_annotations):
all_annotations = obj.search_annotations()
all_contents = {a.content for a in all_annotations}
assert {"Dutch new year", "Recorded later", "Recorded earlier"} <= all_contents


def test_get_or_create_annotation_updates_end(db, setup_sources):
"""Extending an existing label must reuse its identity instead of violating it."""
source = db.session.scalars(select(DataSource).limit(1)).first()
deadline = pd.Timestamp("2030-01-16 08:00+01")
fields = dict(
content="Depart for work: 60% SoC by 08:00 (configured)",
start=deadline - pd.Timedelta(minutes=15),
belief_time=deadline - pd.Timedelta(days=1),
source=source,
type="label",
)
first, _ = get_or_create_annotation(Annotation(end=deadline, **fields))
db.session.flush()
original_id = first.id
new_end = deadline + pd.Timedelta(minutes=15)
updated, is_new = get_or_create_annotation(Annotation(end=new_end, **fields))
db.session.flush()
assert not is_new
assert updated.id == original_id
assert updated.end == new_end
repeated, is_new = get_or_create_annotation(Annotation(end=new_end, **fields))
db.session.flush()
assert not is_new
assert repeated.id == original_id


def test_get_or_create_annotation_preserves_distinct_belief_times(db, setup_sources):
"""Equal labels recorded at different belief times are distinct annotations."""
source = db.session.scalars(select(DataSource).limit(1)).first()
fields = dict(
content="Configured EV target",
start=pd.Timestamp("2030-01-16 07:45+01"),
end=pd.Timestamp("2030-01-16 08:15+01"),
source=source,
type="label",
)
first, _ = get_or_create_annotation(
Annotation(belief_time=pd.Timestamp("2030-01-15 08:00+01"), **fields)
)
db.session.flush()
second, is_new = get_or_create_annotation(
Annotation(belief_time=pd.Timestamp("2030-01-15 12:00+01"), **fields)
)
db.session.flush()
assert is_new
assert second.id != first.id
Loading