From 15d1b5b1bfff2b91e50251f67b6bdbbc74f369db Mon Sep 17 00:00:00 2001 From: mertalev <101130780+mertalev@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:36:06 -0400 Subject: [PATCH] fix metadata face handling --- server/src/repositories/person.repository.ts | 34 +++++++++----------- server/src/services/person.service.ts | 11 ++++--- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/server/src/repositories/person.repository.ts b/server/src/repositories/person.repository.ts index 50c4df7107f2f..de4fd2e1b8106 100644 --- a/server/src/repositories/person.repository.ts +++ b/server/src/repositories/person.repository.ts @@ -303,30 +303,26 @@ export class PersonRepository implements IPersonRepository { faceIdsToRemove: string[], embeddingsToAdd?: FaceSearchEntity[], ): Promise { - const faceDeleteQuery = this.assetFaceRepository - .createQueryBuilder() - .delete() - .where('id = any(:faceIdsToRemove)', { faceIdsToRemove }); + const query = this.faceSearchRepository.createQueryBuilder().select('1'); + if (facesToAdd.length > 0) { + const insertCte = this.assetFaceRepository.createQueryBuilder().insert().values(facesToAdd); + query.addCommonTableExpression(insertCte, 'added'); + } - if (facesToAdd.length === 0) { - await faceDeleteQuery.execute(); - return; + if (faceIdsToRemove.length > 0) { + const deleteCte = this.assetFaceRepository + .createQueryBuilder() + .delete() + .where('id = any(:faceIdsToRemove)', { faceIdsToRemove }); + query.addCommonTableExpression(deleteCte, 'deleted'); } - const faceInsertQuery = this.assetFaceRepository.createQueryBuilder().insert().values(facesToAdd); - if (!embeddingsToAdd || embeddingsToAdd.length === 0) { - await faceInsertQuery.addCommonTableExpression(faceDeleteQuery, 'deleted').execute(); - return; + if (embeddingsToAdd?.length) { + const embeddingCte = this.faceSearchRepository.createQueryBuilder().insert().values(embeddingsToAdd).orIgnore(); + query.addCommonTableExpression(embeddingCte, 'embeddings'); } - await this.faceSearchRepository - .createQueryBuilder() - .addCommonTableExpression(faceDeleteQuery, 'deleted') - .addCommonTableExpression(faceInsertQuery, 'added') - .insert() - .values(embeddingsToAdd) - .orIgnore() - .execute(); + await query.execute(); } async update(person: Partial): Promise { diff --git a/server/src/services/person.service.ts b/server/src/services/person.service.ts index 2dd8cb02eaf74..09dd2bbb2c6b6 100644 --- a/server/src/services/person.service.ts +++ b/server/src/services/person.service.ts @@ -351,7 +351,7 @@ export class PersonService extends BaseService { } const faceIdsToRemove = [...facesToRemove.values()].map((face) => face.id); - if (facesToAdd.length > 0 || faceIdsToRemove.length > 0) { + if (facesToAdd.length > 0 || faceIdsToRemove.length > 0 || embeddingsToAdd.length > 0) { await this.personRepository.refreshFaces(facesToAdd, faceIdsToRemove, embeddingsToAdd); } @@ -367,10 +367,11 @@ export class PersonService extends BaseService { ]); } - await this.assetRepository.upsertJobStatus({ - assetId: asset.id, - facesRecognizedAt: new Date(), - }); + if (embeddingsToAdd.length > 0) { + this.logger.log(`Added ${embeddingsToAdd.length} face embeddings for asset ${id}`); + } + + await this.assetRepository.upsertJobStatus({ assetId: asset.id, facesRecognizedAt: new Date() }); return JobStatus.SUCCESS; }