1
0
mirror of https://github.com/immich-app/immich.git synced 2025-02-19 20:10:14 +02:00

Fix error with asset count

This commit is contained in:
Jonathan Jogenfors 2025-01-14 23:17:17 +01:00
parent 49eaafaabf
commit fc05608985
5 changed files with 16 additions and 12 deletions

View File

@ -172,5 +172,5 @@ export interface IAssetRepository {
upsertFiles(files: UpsertFileOptions[]): Promise<void>; upsertFiles(files: UpsertFileOptions[]): Promise<void>;
detectOfflineExternalAssets(library: LibraryEntity): Promise<UpdateResult>; detectOfflineExternalAssets(library: LibraryEntity): Promise<UpdateResult>;
filterNewExternalAssetPaths(libraryId: string, paths: string[]): Promise<string[]>; filterNewExternalAssetPaths(libraryId: string, paths: string[]): Promise<string[]>;
getAssetCount(options: AssetSearchOptions): Promise<number | undefined>; getLibraryAssetCount(options: AssetSearchOptions): Promise<number | undefined>;
} }

View File

@ -794,11 +794,15 @@ export class AssetRepository implements IAssetRepository {
return result.map((row) => row.path as string); return result.map((row) => row.path as string);
} }
async getAssetCount(options: AssetSearchOptions = {}): Promise<number | undefined> { async getLibraryAssetCount(options: AssetSearchOptions = {}): Promise<number | undefined> {
const { count } = await searchAssetBuilder(this.db, options) const { count } = await this.db
.select(sql`COUNT(*) OVER ()`.as('count')) .selectFrom('assets')
.select(sql`COUNT(*)`.as('count'))
.where('libraryId', '=', asUuid(options.libraryId!))
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow();
console.log(count);
return count as number; return count as number;
} }
} }

View File

@ -227,7 +227,7 @@ describe(LibraryService.name, () => {
libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1); libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1);
storageMock.walk.mockImplementation(async function* generator() {}); storageMock.walk.mockImplementation(async function* generator() {});
assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false }); assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
assetMock.getAssetCount.mockResolvedValue(1); assetMock.getLibraryAssetCount.mockResolvedValue(1);
assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(1) }); assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(1) });
const response = await sut.handleQueueSyncAssets({ id: libraryStub.externalLibrary1.id }); const response = await sut.handleQueueSyncAssets({ id: libraryStub.externalLibrary1.id });
@ -240,7 +240,7 @@ describe(LibraryService.name, () => {
libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1); libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1);
storageMock.walk.mockImplementation(async function* generator() {}); storageMock.walk.mockImplementation(async function* generator() {});
assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false }); assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
assetMock.getAssetCount.mockResolvedValue(0); assetMock.getLibraryAssetCount.mockResolvedValue(0);
assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(1) }); assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(1) });
const response = await sut.handleQueueSyncAssets({ id: libraryStub.externalLibrary1.id }); const response = await sut.handleQueueSyncAssets({ id: libraryStub.externalLibrary1.id });
@ -253,7 +253,7 @@ describe(LibraryService.name, () => {
libraryMock.get.mockResolvedValue(libraryStub.externalLibraryWithImportPaths1); libraryMock.get.mockResolvedValue(libraryStub.externalLibraryWithImportPaths1);
storageMock.walk.mockImplementation(async function* generator() {}); storageMock.walk.mockImplementation(async function* generator() {});
assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false }); assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
assetMock.getAssetCount.mockResolvedValue(1); assetMock.getLibraryAssetCount.mockResolvedValue(1);
assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(0) }); assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(0) });
assetMock.getAllInLibrary.mockResolvedValue({ items: [assetStub.external], hasNextPage: false }); assetMock.getAllInLibrary.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
@ -584,10 +584,10 @@ describe(LibraryService.name, () => {
describe('getStatistics', () => { describe('getStatistics', () => {
it('should return library statistics', async () => { it('should return library statistics', async () => {
assetMock.getAssetCount.mockResolvedValue(10); assetMock.getLibraryAssetCount.mockResolvedValue(10);
await expect(sut.getStatistics(libraryStub.externalLibrary1.id)).resolves.toEqual(10); await expect(sut.getStatistics(libraryStub.externalLibrary1.id)).resolves.toEqual(10);
expect(assetMock.getAssetCount).toHaveBeenCalledWith({ libraryId: libraryStub.externalLibrary1.id }); expect(assetMock.getLibraryAssetCount).toHaveBeenCalledWith({ libraryId: libraryStub.externalLibrary1.id });
}); });
}); });

View File

@ -180,7 +180,7 @@ export class LibraryService extends BaseService {
} }
async getStatistics(id: string): Promise<number> { async getStatistics(id: string): Promise<number> {
const count = await this.assetRepository.getAssetCount({ libraryId: id }); const count = await this.assetRepository.getLibraryAssetCount({ libraryId: id });
if (count == undefined) { if (count == undefined) {
throw new InternalServerErrorException(`Failed to get asset count for library ${id}`); throw new InternalServerErrorException(`Failed to get asset count for library ${id}`);
} }
@ -682,7 +682,7 @@ export class LibraryService extends BaseService {
return JobStatus.SKIPPED; return JobStatus.SKIPPED;
} }
const assetCount = await this.assetRepository.getAssetCount({ libraryId: job.id, withDeleted: true }); const assetCount = await this.assetRepository.getLibraryAssetCount({ libraryId: job.id, withDeleted: true });
if (!assetCount) { if (!assetCount) {
this.logger.log(`Library ${library.id} is empty, no need to check assets`); this.logger.log(`Library ${library.id} is empty, no need to check assets`);

View File

@ -24,7 +24,7 @@ export const newAssetRepositoryMock = (): Mocked<IAssetRepository> => {
getAllByDeviceId: vitest.fn(), getAllByDeviceId: vitest.fn(),
getLivePhotoCount: vitest.fn(), getLivePhotoCount: vitest.fn(),
getAllInLibrary: vitest.fn(), getAllInLibrary: vitest.fn(),
getAssetCount: vitest.fn(), getLibraryAssetCount: vitest.fn(),
updateAll: vitest.fn(), updateAll: vitest.fn(),
updateDuplicates: vitest.fn(), updateDuplicates: vitest.fn(),
getByLibraryIdAndOriginalPath: vitest.fn(), getByLibraryIdAndOriginalPath: vitest.fn(),