1
0
forked from Cutlery/immich

fix(server): time buckets (#4498)

This commit is contained in:
Jason Rasmussen 2023-10-16 14:11:50 -04:00 committed by GitHub
parent a78e08bac1
commit 1890c0ab6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,8 @@ const truncateMap: Record<TimeBucketSize, string> = {
[TimeBucketSize.MONTH]: 'month', [TimeBucketSize.MONTH]: 'month',
}; };
const TIME_BUCKET_COLUMN = 'localDateTime'; const dateTrunc = (options: TimeBucketOptions) =>
`(date_trunc('${truncateMap[options.size]}', ("localDateTime" at time zone 'UTC')) at time zone 'UTC')::timestamptz`;
@Injectable() @Injectable()
export class AssetRepository implements IAssetRepository { export class AssetRepository implements IAssetRepository {
@ -478,25 +479,23 @@ export class AssetRepository implements IAssetRepository {
} }
getTimeBuckets(options: TimeBucketOptions): Promise<TimeBucketItem[]> { getTimeBuckets(options: TimeBucketOptions): Promise<TimeBucketItem[]> {
const truncateValue = truncateMap[options.size]; const truncated = dateTrunc(options);
return this.getBuilder(options) return this.getBuilder(options)
.select(`COUNT(asset.id)::int`, 'count') .select(`COUNT(asset.id)::int`, 'count')
.addSelect(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC')`, 'timeBucket') .addSelect(truncated, 'timeBucket')
.groupBy(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC')`) .groupBy(truncated)
.orderBy(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC')`, 'DESC') .orderBy(truncated, 'DESC')
.getRawMany(); .getRawMany();
} }
getByTimeBucket(timeBucket: string, options: TimeBucketOptions): Promise<AssetEntity[]> { getByTimeBucket(timeBucket: string, options: TimeBucketOptions): Promise<AssetEntity[]> {
const truncateValue = truncateMap[options.size]; const truncated = dateTrunc(options);
return ( return (
this.getBuilder(options) this.getBuilder(options)
.andWhere(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC') = :timeBucket`, { .andWhere(`${truncated} = :timeBucket`, { timeBucket })
timeBucket,
})
// First sort by the day in localtime (put it in the right bucket) // First sort by the day in localtime (put it in the right bucket)
.orderBy(`date_trunc('day', "${TIME_BUCKET_COLUMN}" at time zone 'UTC')`, 'DESC') .orderBy(truncated, 'DESC')
// and then sort by the actual time // and then sort by the actual time
.addOrderBy('asset.fileCreatedAt', 'DESC') .addOrderBy('asset.fileCreatedAt', 'DESC')
.getMany() .getMany()