Skip to content

Commit e6a8f21

Browse files
committed
fix(transfers): handle duplicate legacy SiteNotes with date context
- Track legacy `SiteNotes` by content and date to preserve context for duplicates. - Updated insertion to prepend the date to note content for uniqueness.
1 parent a58fc75 commit e6a8f21

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

transfers/waterlevels_transfer.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,20 +399,32 @@ def _transfer_hook(self, session: Session) -> None:
399399
stats["observations_created"] += len(observation_rows)
400400

401401
# Site Notes (legacy)
402-
site_notes = {
403-
prep["row"].SiteNotes
404-
for prep in prepared_rows
405-
if hasattr(prep["row"], "SiteNotes")
406-
and prep["row"].SiteNotes
407-
and str(prep["row"].SiteNotes).strip()
408-
}
409-
for note_content in site_notes:
402+
# If there are duplicate notes for a single point ID, we only create one note.
403+
# However, if some duplicates are "time stamped" (meaning they are attached to
404+
# rows with different dates), we should ideally preserve that context.
405+
# The current implementation prepends the date to the note content
406+
# to ensure that duplicate content from different dates remains distinct.
407+
unique_notes: dict[str, datetime] = {}
408+
for prep in prepared_rows:
409+
if hasattr(prep["row"], "SiteNotes") and prep["row"].SiteNotes:
410+
content = str(prep["row"].SiteNotes).strip()
411+
if content:
412+
dt = prep["dt_utc"]
413+
# We keep all notes that have different content OR different dates
414+
# Actually, if content is same but date is different, we want to see it.
415+
# So we key by (content, date)
416+
key = (content, dt.date())
417+
if key not in unique_notes:
418+
unique_notes[key] = dt
419+
420+
for (content, _), dt in unique_notes.items():
421+
date_prefix = dt.strftime("%Y-%m-%d")
410422
session.add(
411423
Notes(
412424
target_table="thing",
413425
target_id=thing_id,
414426
note_type="Site Notes (legacy)",
415-
content=str(note_content).strip(),
427+
content=f"{date_prefix}: {content}",
416428
release_status="public",
417429
)
418430
)

0 commit comments

Comments
 (0)