1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-19 18:26:46 +01:00

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',
};
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()
export class AssetRepository implements IAssetRepository {
@ -478,25 +479,23 @@ export class AssetRepository implements IAssetRepository {
}
getTimeBuckets(options: TimeBucketOptions): Promise<TimeBucketItem[]> {
const truncateValue = truncateMap[options.size];
const truncated = dateTrunc(options);
return this.getBuilder(options)
.select(`COUNT(asset.id)::int`, 'count')
.addSelect(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC')`, 'timeBucket')
.groupBy(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC')`)
.orderBy(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC')`, 'DESC')
.addSelect(truncated, 'timeBucket')
.groupBy(truncated)
.orderBy(truncated, 'DESC')
.getRawMany();
}
getByTimeBucket(timeBucket: string, options: TimeBucketOptions): Promise<AssetEntity[]> {
const truncateValue = truncateMap[options.size];
const truncated = dateTrunc(options);
return (
this.getBuilder(options)
.andWhere(`date_trunc('${truncateValue}', "${TIME_BUCKET_COLUMN}" at time zone 'UTC') = :timeBucket`, {
timeBucket,
})
.andWhere(`${truncated} = :timeBucket`, { timeBucket })
// 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
.addOrderBy('asset.fileCreatedAt', 'DESC')
.getMany()