fix(api): publish OccurrenceCreatedEvent on face assignment + backfill embeddings #640

Merged
chris merged 3 commits from fix/occurrence-event into main 2026-07-18 20:39:02 +02:00
Owner

What

Three fixes for missing people/suggestions/thumbnails after face assignment:

1. PRIMARY: upsertGenericPeopleOccurrence now publishes OccurrenceCreatedEvent

Previously: faces assigned via new endpoint never triggered PendingEmbeddingFlushListener. Embeddings stuck in pending_face_embeddings forever (or 5 min via scheduler).
Now: ClassInstanceService injects ApplicationEventPublisher. upsertGenericPeopleOccurrence publishes event on successful INSERT. Embeddings flush immediately.

2. Auto-set representative_occurrence_id on class_instances

Previously: people with assigned faces had no representative set → no thumbnail.
Now: when first occurrence is created, representative_occurrence_id is auto-set if currently null.

3. Name-based fallback in suggestSimilarInstances returns real data

Previously: fallback hard-coded occurrenceCount=0 + thumbnailUrl=null.
Now: fallback SQL queries real occurrence_count + thumbnail_url via correlated subquery.

4. V118 migration: backfill class_occurrence_embeddings for existing data

For already-assigned faces with pending embeddings, the migration moves them to class_occurrence_embeddings and cleans up the pending table. Adds idempotent UNIQUE (occurrence_id, model_name, model_version) constraint needed for ON CONFLICT DO NOTHING.

Test

  • API tests pass
  • V118 migration is idempotent (DO block guard)

Version

1.21.4 → 1.21.5

Symptoms resolved

  • People with assigned faces appear on /discovery/domains/people
  • Suggestions show real occurrenceCount (not 0)
  • Suggestions show real thumbnail
  • Source face has embeddings for future similarity matching
## What Three fixes for missing people/suggestions/thumbnails after face assignment: ### 1. PRIMARY: `upsertGenericPeopleOccurrence` now publishes `OccurrenceCreatedEvent` Previously: faces assigned via new endpoint never triggered `PendingEmbeddingFlushListener`. Embeddings stuck in `pending_face_embeddings` forever (or 5 min via scheduler). Now: `ClassInstanceService` injects `ApplicationEventPublisher`. `upsertGenericPeopleOccurrence` publishes event on successful INSERT. Embeddings flush immediately. ### 2. Auto-set `representative_occurrence_id` on `class_instances` Previously: people with assigned faces had no representative set → no thumbnail. Now: when first occurrence is created, `representative_occurrence_id` is auto-set if currently null. ### 3. Name-based fallback in `suggestSimilarInstances` returns real data Previously: fallback hard-coded `occurrenceCount=0` + `thumbnailUrl=null`. Now: fallback SQL queries real `occurrence_count` + `thumbnail_url` via correlated subquery. ### 4. V118 migration: backfill `class_occurrence_embeddings` for existing data For already-assigned faces with pending embeddings, the migration moves them to `class_occurrence_embeddings` and cleans up the pending table. Adds idempotent `UNIQUE (occurrence_id, model_name, model_version)` constraint needed for `ON CONFLICT DO NOTHING`. ## Test - API tests pass - V118 migration is idempotent (DO block guard) ## Version 1.21.4 → 1.21.5 ## Symptoms resolved - People with assigned faces appear on /discovery/domains/people - Suggestions show real `occurrenceCount` (not 0) - Suggestions show real thumbnail - Source face has embeddings for future similarity matching
- Inject ApplicationEventPublisher into ClassInstanceService
- Publish OccurrenceCreatedEvent after new occurrence INSERT in upsertGenericPeopleOccurrence
- Auto-set representative_occurrence_id on class_instances when first occurrence created
- Fix name-based fallback in suggestSimilarInstances to query real occurrence_count and thumbnail_url
chore(db): backfill class_occurrence_embeddings from pending_face_embeddings
All checks were successful
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 2m10s
74169cdcd6
One-shot migration to flush existing pending embeddings into the
class_occurrence_embeddings table for accepted face occurrences.
Cleans up flushed pending rows.
chore: bump version 1.21.4 → 1.21.5
All checks were successful
Build and Deploy / Detect changed services and test scopes (push) Successful in 3s
Build and Deploy / Smoke API and media worker (push) Has been skipped
Build and Deploy / Smoke web (push) Has been skipped
Build and Deploy / Smoke ML worker (push) Has been skipped
Build and Deploy / Build plants worker image (push) Has been skipped
Build and Deploy / Build people worker image (push) Has been skipped
Build and Deploy / Build converter worker image (push) Has been skipped
Build and Deploy / Build API image (push) Has been skipped
Build and Deploy / Build media worker image (push) Has been skipped
Build and Deploy / Build ML worker image (push) Has been skipped
Build and Deploy / Build web image (push) Has been skipped
Build and Deploy / Deploy API (push) Has been skipped
Build and Deploy / Deploy web (push) Has been skipped
Build and Deploy / Deploy media worker (push) Has been skipped
Build and Deploy / Deploy ML worker (push) Has been skipped
Build and Deploy / Deploy plants worker (push) Has been skipped
Build and Deploy / Deploy people worker (push) Has been skipped
Build and Deploy / Deploy converter worker (push) Has been skipped
Build and Deploy / Clean Docker build artifacts (push) Has been skipped
Build and Deploy / Run scoped PR tests (push) Successful in 2m5s
26b0c12cc8
chris merged commit 7a7b8ca92b into main 2026-07-18 20:39:02 +02:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
chris/memories!640
No description provided.