From 3c71fe893496207bb5dccc8ebf015c5f7ba0ae1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20H=C3=B6ning?= Date: Tue, 17 Nov 2026 17:04:11 +0100 Subject: [PATCH] fix updating/extending existing annotaitons - align annotation lookup so it plays well with its database uniqueness constraint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolas Höning --- flexmeasures/data/models/annotations.py | 6 ++- flexmeasures/data/tests/test_annotations.py | 48 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/flexmeasures/data/models/annotations.py b/flexmeasures/data/models/annotations.py index 471427aacc..d60af009fe 100644 --- a/flexmeasures/data/models/annotations.py +++ b/flexmeasures/data/models/annotations.py @@ -201,7 +201,8 @@ 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: @@ -209,7 +210,7 @@ def get_or_create_annotation( 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, ) @@ -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 diff --git a/flexmeasures/data/tests/test_annotations.py b/flexmeasures/data/tests/test_annotations.py index 40b606f0e4..384034d3ad 100644 --- a/flexmeasures/data/tests/test_annotations.py +++ b/flexmeasures/data/tests/test_annotations.py @@ -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