0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-28 00:59:18 -05: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>;
detectOfflineExternalAssets(library: LibraryEntity): Promise<UpdateResult>;
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);
}
async getAssetCount(options: AssetSearchOptions = {}): Promise<number | undefined> {
const { count } = await searchAssetBuilder(this.db, options)
.select(sql`COUNT(*) OVER ()`.as('count'))
async getLibraryAssetCount(options: AssetSearchOptions = {}): Promise<number | undefined> {
const { count } = await this.db
.selectFrom('assets')
.select(sql`COUNT(*)`.as('count'))
.where('libraryId', '=', asUuid(options.libraryId!))
.executeTakeFirstOrThrow();
console.log(count);
return count as number;
}
}

View file

@ -227,7 +227,7 @@ describe(LibraryService.name, () => {
libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1);
storageMock.walk.mockImplementation(async function* generator() {});
assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
assetMock.getAssetCount.mockResolvedValue(1);
assetMock.getLibraryAssetCount.mockResolvedValue(1);
assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(1) });
const response = await sut.handleQueueSyncAssets({ id: libraryStub.externalLibrary1.id });
@ -240,7 +240,7 @@ describe(LibraryService.name, () => {
libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1);
storageMock.walk.mockImplementation(async function* generator() {});
assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
assetMock.getAssetCount.mockResolvedValue(0);
assetMock.getLibraryAssetCount.mockResolvedValue(0);
assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(1) });
const response = await sut.handleQueueSyncAssets({ id: libraryStub.externalLibrary1.id });
@ -253,7 +253,7 @@ describe(LibraryService.name, () => {
libraryMock.get.mockResolvedValue(libraryStub.externalLibraryWithImportPaths1);
storageMock.walk.mockImplementation(async function* generator() {});
assetMock.getAll.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
assetMock.getAssetCount.mockResolvedValue(1);
assetMock.getLibraryAssetCount.mockResolvedValue(1);
assetMock.detectOfflineExternalAssets.mockResolvedValue({ numUpdatedRows: BigInt(0) });
assetMock.getAllInLibrary.mockResolvedValue({ items: [assetStub.external], hasNextPage: false });
@ -584,10 +584,10 @@ describe(LibraryService.name, () => {
describe('getStatistics', () => {
it('should return library statistics', async () => {
assetMock.getAssetCount.mockResolvedValue(10);
assetMock.getLibraryAssetCount.mockResolvedValue(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> {
const count = await this.assetRepository.getAssetCount({ libraryId: id });
const count = await this.assetRepository.getLibraryAssetCount({ libraryId: id });
if (count == undefined) {
throw new InternalServerErrorException(`Failed to get asset count for library ${id}`);
}
@ -682,7 +682,7 @@ export class LibraryService extends BaseService {
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) {
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(),
getLivePhotoCount: vitest.fn(),
getAllInLibrary: vitest.fn(),
getAssetCount: vitest.fn(),
getLibraryAssetCount: vitest.fn(),
updateAll: vitest.fn(),
updateDuplicates: vitest.fn(),
getByLibraryIdAndOriginalPath: vitest.fn(),