mirror of
https://github.com/samsonjs/immich.git
synced 2026-04-27 15:07:45 +00:00
fix: timezone in timeline bucketing (#25894)
Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
parent
a42c08ed84
commit
2dd3a764ae
3 changed files with 21 additions and 16 deletions
|
|
@ -85,14 +85,14 @@ LIMIT $limit;
|
||||||
mergedBucket(:group_by AS INTEGER):
|
mergedBucket(:group_by AS INTEGER):
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(*) as asset_count,
|
COUNT(*) as asset_count,
|
||||||
CASE
|
bucket_date
|
||||||
WHEN :group_by = 0 THEN STRFTIME('%Y-%m-%d', created_at, 'localtime') -- day
|
|
||||||
WHEN :group_by = 1 THEN STRFTIME('%Y-%m', created_at, 'localtime') -- month
|
|
||||||
END AS bucket_date
|
|
||||||
FROM
|
FROM
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
rae.created_at
|
CASE
|
||||||
|
WHEN :group_by = 0 THEN STRFTIME('%Y-%m-%d', rae.local_date_time)
|
||||||
|
WHEN :group_by = 1 THEN STRFTIME('%Y-%m', rae.local_date_time)
|
||||||
|
END as bucket_date
|
||||||
FROM
|
FROM
|
||||||
remote_asset_entity rae
|
remote_asset_entity rae
|
||||||
LEFT JOIN
|
LEFT JOIN
|
||||||
|
|
@ -107,7 +107,10 @@ FROM
|
||||||
)
|
)
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT
|
SELECT
|
||||||
lae.created_at
|
CASE
|
||||||
|
WHEN :group_by = 0 THEN STRFTIME('%Y-%m-%d', lae.created_at, 'localtime')
|
||||||
|
WHEN :group_by = 1 THEN STRFTIME('%Y-%m', lae.created_at, 'localtime')
|
||||||
|
END as bucket_date
|
||||||
FROM
|
FROM
|
||||||
local_asset_entity lae
|
local_asset_entity lae
|
||||||
WHERE NOT EXISTS (
|
WHERE NOT EXISTS (
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -126,7 +126,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
final assetCountExp = _db.localAssetEntity.id.count();
|
final assetCountExp = _db.localAssetEntity.id.count();
|
||||||
final dateExp = _db.localAssetEntity.createdAt.dateFmt(groupBy);
|
final dateExp = _db.localAssetEntity.createdAt.dateFmt(groupBy, toLocal: true);
|
||||||
|
|
||||||
final query =
|
final query =
|
||||||
_db.localAssetEntity.selectOnly().join([
|
_db.localAssetEntity.selectOnly().join([
|
||||||
|
|
@ -203,7 +203,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
||||||
final album = albums.first;
|
final album = albums.first;
|
||||||
final isAscending = album.order == AlbumAssetOrder.asc;
|
final isAscending = album.order == AlbumAssetOrder.asc;
|
||||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||||
|
|
||||||
final query = _db.remoteAssetEntity.selectOnly()
|
final query = _db.remoteAssetEntity.selectOnly()
|
||||||
..addColumns([assetCountExp, dateExp])
|
..addColumns([assetCountExp, dateExp])
|
||||||
|
|
@ -280,7 +280,8 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
||||||
final sorted = List<BaseAsset>.from(assets)..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
final sorted = List<BaseAsset>.from(assets)..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||||
final Map<DateTime, int> bucketCounts = {};
|
final Map<DateTime, int> bucketCounts = {};
|
||||||
for (final asset in sorted) {
|
for (final asset in sorted) {
|
||||||
final date = DateTime(asset.createdAt.year, asset.createdAt.month, asset.createdAt.day);
|
final localTime = asset.createdAt.toLocal();
|
||||||
|
final date = DateTime(localTime.year, localTime.month, localTime.day);
|
||||||
bucketCounts[date] = (bucketCounts[date] ?? 0) + 1;
|
bucketCounts[date] = (bucketCounts[date] ?? 0) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -360,7 +361,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||||
|
|
||||||
final query = _db.remoteAssetEntity.selectOnly()
|
final query = _db.remoteAssetEntity.selectOnly()
|
||||||
..addColumns([assetCountExp, dateExp])
|
..addColumns([assetCountExp, dateExp])
|
||||||
|
|
@ -430,7 +431,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||||
|
|
||||||
final query = _db.remoteAssetEntity.selectOnly()
|
final query = _db.remoteAssetEntity.selectOnly()
|
||||||
..addColumns([assetCountExp, dateExp])
|
..addColumns([assetCountExp, dateExp])
|
||||||
|
|
@ -500,7 +501,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||||
|
|
||||||
final query = _db.remoteAssetEntity.selectOnly()
|
final query = _db.remoteAssetEntity.selectOnly()
|
||||||
..addColumns([assetCountExp, dateExp])
|
..addColumns([assetCountExp, dateExp])
|
||||||
|
|
@ -602,7 +603,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||||
|
|
||||||
final query = _db.remoteAssetEntity.selectOnly()
|
final query = _db.remoteAssetEntity.selectOnly()
|
||||||
..addColumns([assetCountExp, dateExp])
|
..addColumns([assetCountExp, dateExp])
|
||||||
|
|
@ -664,10 +665,11 @@ List<Bucket> _generateBuckets(int count) {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension on Expression<DateTime> {
|
extension on Expression<DateTime> {
|
||||||
Expression<String> dateFmt(GroupAssetsBy groupBy) {
|
Expression<String> dateFmt(GroupAssetsBy groupBy, {bool toLocal = false}) {
|
||||||
// DateTimes are stored in UTC, so we need to convert them to local time inside the query before formatting
|
// DateTimes are stored in UTC, so we need to convert them to local time inside the query before formatting
|
||||||
// to create the correct time bucket
|
// to create the correct time bucket when toLocal is true
|
||||||
final localTimeExp = modify(const DateTimeModifier.localTime());
|
// toLocal is false for remote assets where localDateTime is already in the correct timezone
|
||||||
|
final localTimeExp = toLocal ? modify(const DateTimeModifier.localTime()) : this;
|
||||||
return switch (groupBy) {
|
return switch (groupBy) {
|
||||||
GroupAssetsBy.day || GroupAssetsBy.auto => localTimeExp.date,
|
GroupAssetsBy.day || GroupAssetsBy.auto => localTimeExp.date,
|
||||||
GroupAssetsBy.month => localTimeExp.strftime("%Y-%m"),
|
GroupAssetsBy.month => localTimeExp.strftime("%Y-%m"),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue