Skip to content

Commit

Permalink
refactor(server): move filters to getByDayOfYear query (#14628)
Browse files Browse the repository at this point in the history
move filters to getByDayOfYear query
  • Loading branch information
mertalev authored Dec 10, 2024
1 parent 25ca3b1 commit 9eff1c4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
7 changes: 6 additions & 1 deletion server/src/interfaces/asset.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export interface UpsertFileOptions {

export type AssetPathEntity = Pick<AssetEntity, 'id' | 'originalPath' | 'isOffline'>;

export interface DayOfYearAssets {
yearsAgo: number;
assets: AssetEntity[];
}

export const IAssetRepository = 'IAssetRepository';

export interface IAssetRepository {
Expand All @@ -156,7 +161,7 @@ export interface IAssetRepository {
select?: FindOptionsSelect<AssetEntity>,
): Promise<AssetEntity[]>;
getByIdsWithAllRelations(ids: string[]): Promise<AssetEntity[]>;
getByDayOfYear(ownerIds: string[], monthDay: MonthDay): Promise<AssetEntity[]>;
getByDayOfYear(ownerIds: string[], monthDay: MonthDay): Promise<DayOfYearAssets[]>;
getByChecksum(options: { ownerId: string; checksum: Buffer; libraryId?: string }): Promise<AssetEntity | null>;
getByChecksums(userId: string, checksums: Buffer[]): Promise<AssetEntity[]>;
getUploadAssetIdByChecksum(ownerId: string, checksum: Buffer): Promise<string | undefined>;
Expand Down
17 changes: 7 additions & 10 deletions server/src/queries/asset.repository.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,19 @@ SELECT
FROM
"assets" "entity"
LEFT JOIN "exif" "exifInfo" ON "exifInfo"."assetId" = "entity"."id"
LEFT JOIN "asset_files" "files" ON "files"."assetId" = "entity"."id"
INNER JOIN "asset_files" "files" ON "files"."assetId" = "entity"."id"
WHERE
(
"entity"."ownerId" IN ($1)
AND "entity"."isVisible" = true
AND "entity"."isArchived" = false
"files"."type" = $1
AND EXTRACT(
DAY
YEAR
FROM
"entity"."localDateTime" AT TIME ZONE 'UTC'
) = $2
AND EXTRACT(
MONTH
CURRENT_DATE AT TIME ZONE 'UTC'
) - EXTRACT(
YEAR
FROM
"entity"."localDateTime" AT TIME ZONE 'UTC'
) = $3
) > 0
)
AND ("entity"."deletedAt" IS NULL)
ORDER BY
Expand Down
23 changes: 20 additions & 3 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AssetUpdateAllOptions,
AssetUpdateDuplicateOptions,
AssetUpdateOptions,
DayOfYearAssets,
IAssetRepository,
LivePhotoSearchOptions,
MonthDay,
Expand Down Expand Up @@ -74,8 +75,8 @@ export class AssetRepository implements IAssetRepository {
}

@GenerateSql({ params: [[DummyValue.UUID], { day: 1, month: 1 }] })
getByDayOfYear(ownerIds: string[], { day, month }: MonthDay): Promise<AssetEntity[]> {
return this.repository
async getByDayOfYear(ownerIds: string[], { day, month }: MonthDay): Promise<DayOfYearAssets[]> {
const assets = await this.repository
.createQueryBuilder('entity')
.where(
`entity.ownerId IN (:...ownerIds)
Expand All @@ -90,9 +91,25 @@ export class AssetRepository implements IAssetRepository {
},
)
.leftJoinAndSelect('entity.exifInfo', 'exifInfo')
.leftJoinAndSelect('entity.files', 'files')
.innerJoinAndSelect('entity.files', 'files')
.where('files.type = :type', { type: AssetFileType.THUMBNAIL })
.andWhere(
`EXTRACT(YEAR FROM CURRENT_DATE AT TIME ZONE 'UTC') - EXTRACT(YEAR FROM entity.localDateTime AT TIME ZONE 'UTC') > 0`,
)
.orderBy('entity.fileCreatedAt', 'ASC')
.getMany();

const groups: Record<number, DayOfYearAssets> = {};
const currentYear = new Date().getFullYear();
for (const asset of assets) {
const yearsAgo = currentYear - asset.localDateTime.getFullYear();
if (!groups[yearsAgo]) {
groups[yearsAgo] = { yearsAgo, assets: [] };
}
groups[yearsAgo].assets.push(asset);
}

return Object.values(groups);
}

@GenerateSql({ params: [[DummyValue.UUID]] })
Expand Down
15 changes: 14 additions & 1 deletion server/src/services/asset.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ describe(AssetService.name, () => {
const image4 = { ...assetStub.image, localDateTime: new Date(2009, 1, 15) };

partnerMock.getAll.mockResolvedValue([]);
assetMock.getByDayOfYear.mockResolvedValue([image1, image2, image3, image4]);
assetMock.getByDayOfYear.mockResolvedValue([
{
yearsAgo: 1,
assets: [image1, image2],
},
{
yearsAgo: 9,
assets: [image3],
},
{
yearsAgo: 15,
assets: [image4],
},
]);

await expect(sut.getMemoryLane(authStub.admin, { day: 15, month: 1 })).resolves.toEqual([
{ yearsAgo: 1, title: '1 year ago', assets: [mapAsset(image1), mapAsset(image2)] },
Expand Down
29 changes: 7 additions & 22 deletions server/src/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,13 @@ export class AssetService extends BaseService {
});
const userIds = [auth.user.id, ...partnerIds];

const assets = await this.assetRepository.getByDayOfYear(userIds, dto);
const assetsWithThumbnails = assets.filter(({ files }) => !!getAssetFiles(files).thumbnailFile);
const groups: Record<number, AssetEntity[]> = {};
const currentYear = new Date().getFullYear();
for (const asset of assetsWithThumbnails) {
const yearsAgo = currentYear - asset.localDateTime.getFullYear();
if (!groups[yearsAgo]) {
groups[yearsAgo] = [];
}
groups[yearsAgo].push(asset);
}

return Object.keys(groups)
.map(Number)
.sort((a, b) => a - b)
.filter((yearsAgo) => yearsAgo > 0)
.map((yearsAgo) => ({
yearsAgo,
// TODO move this to clients
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`,
assets: groups[yearsAgo].map((asset) => mapAsset(asset, { auth })),
}));
const groups = await this.assetRepository.getByDayOfYear(userIds, dto);
return groups.map(({ yearsAgo, assets }) => ({
yearsAgo,
// TODO move this to clients
title: `${yearsAgo} year${yearsAgo > 1 ? 's' : ''} ago`,
assets: assets.map((asset) => mapAsset(asset, { auth })),
}));
}

async getStatistics(auth: AuthDto, dto: AssetStatsDto) {
Expand Down

0 comments on commit 9eff1c4

Please sign in to comment.