Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(analytics): normalize historical query ranges to full UTC days

coderabbitai[bot]f66345334faf
diff --git a/apps/cloud/app/pages/analytics.vue b/apps/cloud/app/pages/analytics.vue
index 9fecc35a..53caaabd 100644
--- a/apps/cloud/app/pages/analytics.vue
+++ b/apps/cloud/app/pages/analytics.vue
@@ -130,8 +130,8 @@ await load()
<p class="cf-field-hint">
{{
tr(
- 'Custom ranges support the past year; ranges over 31 days use full UTC days.',
- '自定义范围支持最近一年;超过 31 天的范围使用完整的 UTC 日期。',
+ 'Custom ranges support the past year; ranges over 31 days or starting more than about 90 days ago use full UTC days.',
+ '自定义范围支持最近一年;超过 31 天或开始于约 90 天以前的范围使用完整的 UTC 日期。',
)
}}
</p>
diff --git a/apps/shared/analytics/query.ts b/apps/shared/analytics/query.ts
index 3130b380..007ba738 100644
--- a/apps/shared/analytics/query.ts
+++ b/apps/shared/analytics/query.ts
@@ -19,7 +19,11 @@ export async function analyticsReport(event: H3Event, filter: AnalyticsFilter) {
window.end - window.start > 31 * 86400000 ||
window.start < Date.now() - 89 * 86400000
)
- return await historicalAnalytics(platformDatabase(event), config, filter)
+ return await historicalAnalytics(platformDatabase(event), config, {
+ ...filter,
+ from: new Date(window.start).toISOString().slice(0, 10),
+ to: new Date(window.end - 1).toISOString().slice(0, 10),
+ })
return await queryAnalytics(config, filter)
} catch (error) {
throw createError({
diff --git a/packages/saas-ui/app/utils/analytics-range.ts b/packages/saas-ui/app/utils/analytics-range.ts
index f1813d19..d3402ed3 100644
--- a/packages/saas-ui/app/utils/analytics-range.ts
+++ b/packages/saas-ui/app/utils/analytics-range.ts
@@ -27,10 +27,17 @@ export function relativeRange(
}
}
// Daily archives cannot represent partial days; preserve half-open end semantics.
-export function archiveRange(range: AnalyticsDateRange): AnalyticsDateRange {
+export function archiveRange(
+ range: AnalyticsDateRange,
+ now = Date.now(),
+): AnalyticsDateRange {
const start = Date.parse(range.from),
end = Date.parse(range.to)
- if (range.allTime || end - start <= 31 * 86400000) return range
+ if (
+ range.allTime ||
+ (end - start <= 31 * 86400000 && start >= now - 89 * 86400000)
+ )
+ return range
return {
...range,
from: new Date(start).toISOString().slice(0, 10),
diff --git a/tests/analytics-presentation.test.ts b/tests/analytics-presentation.test.ts
index f2d2a337..3e52a86c 100644
--- a/tests/analytics-presentation.test.ts
+++ b/tests/analytics-presentation.test.ts
@@ -73,9 +73,10 @@ test('relative presets and local/UTC inputs retain actual instants across timezo
else process.env.TZ = previous
}
})
-test('long picker ranges request full UTC archive days while short ranges stay precise', () => {
- const year = relativeRange(365 * 86400, Date.parse('2026-11-01T12:00:00Z'))
- assert.deepEqual(archiveRange(year), {
+test('long or old picker ranges request full UTC archive days while recent short ranges stay precise', () => {
+ const now = Date.parse('2026-11-01T12:00:00Z')
+ const year = relativeRange(365 * 86400, now)
+ assert.deepEqual(archiveRange(year, now), {
...year,
from: '2025-11-01',
to: '2026-11-01',
@@ -84,13 +85,26 @@ test('long picker ranges request full UTC archive days while short ranges stay p
from: '2026-08-01T23:30:00.000Z',
to: '2026-10-01T00:00:00.000Z',
}
- assert.deepEqual(archiveRange(custom), {
+ assert.deepEqual(archiveRange(custom, now), {
from: '2026-08-01',
to: '2026-09-30',
})
- const short = relativeRange(30 * 86400, Date.parse('2026-11-01T12:00:00Z'))
- assert.deepEqual(archiveRange(short), short)
- assert.deepEqual(archiveRange({ ...year, allTime: true }), {
+ const short = relativeRange(30 * 86400, now)
+ assert.deepEqual(archiveRange(short, now), short)
+ const cutoff = now - 89 * 86400000
+ const atCutoff = {
+ from: new Date(cutoff).toISOString(),
+ to: new Date(cutoff + 86400000).toISOString(),
+ }
+ assert.deepEqual(archiveRange(atCutoff, now), atCutoff)
+ assert.deepEqual(
+ archiveRange(
+ { from: '2026-07-01T23:30:00Z', to: '2026-07-03T00:00:00Z' },
+ now,
+ ),
+ { from: '2026-07-01', to: '2026-07-02' },
+ )
+ assert.deepEqual(archiveRange({ ...year, allTime: true }, now), {
...year,
allTime: true,
})
diff --git a/tests/visit-analytics.test.ts b/tests/visit-analytics.test.ts
index 97a355ff..a0ae6651 100644
--- a/tests/visit-analytics.test.ts
+++ b/tests/visit-analytics.test.ts
@@ -4,6 +4,7 @@ import { readFile } from 'node:fs/promises'
import { createRequire } from 'node:module'
import {
analyticsFilter,
+ analyticsWindow,
analyticsQueries,
visitPath,
visitReferrer,
@@ -16,6 +17,7 @@ import {
validatePolicy,
} from '../packages/platform/src/entitlements.ts'
import { configureAnalyticsDeployment } from '../scripts/analytics-deployment.mjs'
+import { archiveRange } from '../packages/saas-ui/app/utils/analytics-range.ts'
const require = createRequire(import.meta.url),
ts = require('typescript')
async function module(file: string, deps: Record<string, unknown>) {
@@ -258,6 +260,49 @@ test('portal report checks membership, live entitlement and selected-site owners
await assert.rejects(report({}), { statusCode: 403 })
assert.equal(reports, 1)
})
+test('archive routing sends UTC dates even when a short range crosses the cutoff in transit', async () => {
+ const now = Date.now()
+ const range = {
+ from: new Date(now - 89 * 86400000 - 60000).toISOString(),
+ to: new Date(now - 88 * 86400000).toISOString(),
+ workspaceId: 7,
+ }
+ const sent = archiveRange(range, now - 120000)
+ assert.deepEqual(sent, range)
+ const { analyticsReport } = await module(
+ '../apps/shared/analytics/query.ts',
+ {
+ analyticsWindow,
+ platformDatabase: () => ({}),
+ historicalAnalytics: async (
+ _db: unknown,
+ _config: unknown,
+ filter: unknown,
+ ) => filter,
+ queryAnalytics: async (_config: unknown, filter: unknown) => filter,
+ createError: (value: any) => Object.assign(new Error(), value),
+ },
+ )
+ const event = { context: { cloudflare: { env: {} } } }
+ assert.deepEqual(await analyticsReport(event, sent), {
+ ...range,
+ from: new Date(Date.parse(range.from)).toISOString().slice(0, 10),
+ to: new Date(Date.parse(range.to) - 1).toISOString().slice(0, 10),
+ })
+ const dates = { from: '2025-01-01', to: '2025-01-02', workspaceId: 7 }
+ assert.deepEqual(await analyticsReport(event, dates), dates)
+ const recent = {
+ from: new Date(now - 3600000).toISOString(),
+ to: new Date(now).toISOString(),
+ }
+ assert.deepEqual(await analyticsReport(event, recent), recent)
+ const allTime = { ...recent, allTime: true }
+ assert.deepEqual(await analyticsReport(event, allTime), {
+ ...allTime,
+ from: new Date(Date.parse(allTime.from)).toISOString().slice(0, 10),
+ to: new Date(Date.parse(allTime.to) - 1).toISOString().slice(0, 10),
+ })
+})
test('SQL API failures are not empty reports; sampled results preserve weighted PV and observed UV', async () => {
const accountId = 'a'.repeat(32),
filter = analyticsFilter({ workspaceId: 91, kind: 'site' })