Skip to content

High performance self-hosted photo and video management solution.

fix: time bucket grouping (#19329)

Jason Rasmussen33c9f88ba401
diff --git a/server/src/queries/asset.repository.sql b/server/src/queries/asset.repository.sql
index 9792257652..a781ea8239 100644
--- a/server/src/queries/asset.repository.sql
+++ b/server/src/queries/asset.repository.sql
@@ -224,7 +224,7 @@ limit
with
"assets" as (
select
- date_trunc('MONTH', "localDateTime" at time zone 'UTC') at time zone 'UTC' as "timeBucket"
+ date_trunc('MONTH', "localDateTime" AT TIME ZONE 'UTC') AT TIME ZONE 'UTC' as "timeBucket"
from
"assets"
where
@@ -232,7 +232,7 @@ with
and "assets"."visibility" in ('archive', 'timeline')
)
select
- "timeBucket"::date::text as "timeBucket",
+ ("timeBucket" AT TIME ZONE 'UTC')::date::text as "timeBucket",
count(*) as "count"
from
"assets"
@@ -300,7 +300,7 @@ with
where
"assets"."deletedAt" is null
and "assets"."visibility" in ('archive', 'timeline')
- and date_trunc('MONTH', "localDateTime" at time zone 'UTC') at time zone 'UTC' = $2
+ and date_trunc('MONTH', "localDateTime" AT TIME ZONE 'UTC') AT TIME ZONE 'UTC' = $2
and not exists (
select
from
diff --git a/server/src/repositories/asset.repository.ts b/server/src/repositories/asset.repository.ts
index 8cb971775c..2a3975f446 100644
--- a/server/src/repositories/asset.repository.ts
+++ b/server/src/repositories/asset.repository.ts
@@ -42,11 +42,6 @@ interface LivePhotoSearchOptions {
type: AssetType;
}
-export enum TimeBucketSize {
- DAY = 'DAY',
- MONTH = 'MONTH',
-}
-
interface AssetBuilderOptions {
isFavorite?: boolean;
isTrashed?: boolean;
@@ -490,13 +485,13 @@ export class AssetRepository {
.execute();
}
- @GenerateSql({ params: [{ size: TimeBucketSize.MONTH }] })
+ @GenerateSql({ params: [{}] })
async getTimeBuckets(options: TimeBucketOptions): Promise<TimeBucketItem[]> {
return this.db
.with('assets', (qb) =>
qb
.selectFrom('assets')
- .select(truncatedDate<Date>(TimeBucketSize.MONTH).as('timeBucket'))
+ .select(truncatedDate<Date>().as('timeBucket'))
.$if(!!options.isTrashed, (qb) => qb.where('assets.status', '!=', AssetStatus.DELETED))
.where('assets.deletedAt', options.isTrashed ? 'is not' : 'is', null)
.$if(options.visibility === undefined, withDefaultVisibility)
@@ -525,7 +520,7 @@ export class AssetRepository {
.$if(!!options.tagId, (qb) => withTagId(qb, options.tagId!)),
)
.selectFrom('assets')
- .select(sql<string>`"timeBucket"::date::text`.as('timeBucket'))
+ .select(sql<string>`("timeBucket" AT TIME ZONE 'UTC')::date::text`.as('timeBucket'))
.select((eb) => eb.fn.countAll<number>().as('count'))
.groupBy('timeBucket')
.orderBy('timeBucket', options.order ?? 'desc')
@@ -576,7 +571,7 @@ export class AssetRepository {
.where('assets.deletedAt', options.isTrashed ? 'is not' : 'is', null)
.$if(options.visibility == undefined, withDefaultVisibility)
.$if(!!options.visibility, (qb) => qb.where('assets.visibility', '=', options.visibility!))
- .where(truncatedDate(TimeBucketSize.MONTH), '=', timeBucket.replace(/^[+-]/, ''))
+ .where(truncatedDate(), '=', timeBucket.replace(/^[+-]/, ''))
.$if(!!options.albumId, (qb) =>
qb.where((eb) =>
eb.exists(
diff --git a/server/src/utils/database.ts b/server/src/utils/database.ts
index d7417becf1..54a21fe994 100644
--- a/server/src/utils/database.ts
+++ b/server/src/utils/database.ts
@@ -18,7 +18,6 @@ import postgres, { Notice } from 'postgres';
import { columns, Exif, Person } from 'src/database';
import { DB } from 'src/db';
import { AssetFileType, AssetVisibility, DatabaseExtension, DatabaseSslMode } from 'src/enum';
-import { TimeBucketSize } from 'src/repositories/asset.repository';
import { AssetSearchBuilderOptions } from 'src/repositories/search.repository';
import { DatabaseConnectionParams, VectorExtension } from 'src/types';
@@ -279,8 +278,8 @@ export function withTags(eb: ExpressionBuilder<DB, 'assets'>) {
).as('tags');
}
-export function truncatedDate<O>(size: TimeBucketSize) {
- return sql<O>`date_trunc(${sql.lit(size)}, "localDateTime" at time zone 'UTC') at time zone 'UTC'`;
+export function truncatedDate<O>() {
+ return sql<O>`date_trunc(${sql.lit('MONTH')}, "localDateTime" AT TIME ZONE 'UTC') AT TIME ZONE 'UTC'`;
}
export function withTagId<O>(qb: SelectQueryBuilder<DB, 'assets', O>, tagId: string) {