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): enforce range limits, refresh presets, and cap archive details

coderabbitai[bot]4c47c8e295c4
diff --git a/apps/cloud/app/pages/analytics.vue b/apps/cloud/app/pages/analytics.vue
index 55b39684..3f2697c9 100644
--- a/apps/cloud/app/pages/analytics.vue
+++ b/apps/cloud/app/pages/analytics.vue
@@ -70,6 +70,8 @@ async function load() {
if (request === generation) loading.value = false
}
}
+if (range.value.preset && !range.value.allTime)
+ range.value = relativeRange(range.value.preset)
await load()
</script>
<template>
diff --git a/apps/cloud/server/plugins/analytics-archive.ts b/apps/cloud/server/plugins/analytics-archive.ts
index 8f81b4ad..c8e935ee 100644
--- a/apps/cloud/server/plugins/analytics-archive.ts
+++ b/apps/cloud/server/plugins/analytics-archive.ts
@@ -1,5 +1,12 @@
import { archiveAnalytics } from '../../../../packages/platform/src/analytics-archive'
import type { PlatformD1 } from '../../../../packages/platform/src/index'
+const archiveErrors = new Set([
+ 'analytics_not_configured',
+ 'analytics_dataset_missing',
+ 'analytics_archive_query_failed',
+ 'analytics_archive_retention_gap',
+ 'invalid_analytics_archive_rows',
+])
export default defineNitroPlugin((app) => {
app.hooks.hook('cloudflare:scheduled', async ({ env }) => {
if (!env.DB || !env.ANALYTICS_API_TOKEN || !env.ANALYTICS_DATASET) return
@@ -9,8 +16,12 @@ export default defineNitroPlugin((app) => {
token: String(env.ANALYTICS_API_TOKEN),
dataset: String(env.ANALYTICS_DATASET),
})
- } catch {
- console.error('analytics_archive_failed')
+ } catch (error) {
+ const code =
+ error instanceof Error && archiveErrors.has(error.message)
+ ? error.message
+ : 'unknown'
+ console.error('analytics_archive_failed', code)
}
})
})
diff --git a/packages/platform/src/analytics-archive.ts b/packages/platform/src/analytics-archive.ts
index ac95fca9..4b98aeb0 100644
--- a/packages/platform/src/analytics-archive.ts
+++ b/packages/platform/src/analytics-archive.ts
@@ -9,7 +9,8 @@ import {
type AnalyticsRow,
} from './analytics.ts'
const DAY = 86400000,
- PAGE = 250
+ PAGE = 250,
+ DETAIL_LIMIT = 5000
const dimensions = [
['summary', "''"],
['pages', 'blob6'],
@@ -164,7 +165,7 @@ export async function archiveAnalytics(
const [dimension, column] = dimensions[Math.floor(state.phase / 2)]!
const perSite = state.phase % 2 === 1
const end = isoDay(Date.parse(state.cursor_day) + DAY)
- const sql = `SELECT blob2 AS kind,blob3 AS workspaceId,${perSite ? 'blob4' : "'0'"} AS siteId,${column} AS label,SUM(_sample_interval) AS pv,COUNT(DISTINCT blob7) AS uv,MAX(_sample_interval) AS sampleInterval FROM ${config.dataset} WHERE blob1='v1' AND timestamp>=toDateTime('${state.cursor_day} 00:00:00') AND timestamp<toDateTime('${end} 00:00:00')${perSite ? " AND blob2='site'" : ''} GROUP BY kind,workspaceId,siteId,label ORDER BY kind,workspaceId,siteId,label LIMIT ${PAGE} OFFSET ${state.page_offset} FORMAT JSON`
+ const sql = `SELECT * FROM (SELECT blob2 AS kind,blob3 AS workspaceId,${perSite ? 'blob4' : "'0'"} AS siteId,${column} AS label,SUM(_sample_interval) AS pv,COUNT(DISTINCT blob7) AS uv,MAX(_sample_interval) AS sampleInterval FROM ${config.dataset} WHERE blob1='v1' AND timestamp>=toDateTime('${state.cursor_day} 00:00:00') AND timestamp<toDateTime('${end} 00:00:00')${perSite ? " AND blob2='site'" : ''} GROUP BY kind,workspaceId,siteId,label ORDER BY pv DESC,kind,workspaceId,siteId,label LIMIT ${DETAIL_LIMIT}) ORDER BY kind,workspaceId,siteId,label LIMIT ${PAGE} OFFSET ${state.page_offset} FORMAT JSON`
const rows = await sqlRows(config, sql, fetcher)
if (
rows.some(
@@ -223,6 +224,8 @@ export async function historicalAnalytics(
now = Date.now(),
fetcher: typeof fetch = fetch,
): Promise<AnalyticsReport> {
+ if (!filter.allTime && (filter.from.includes('T') || filter.to.includes('T')))
+ throw new Error('analytics_range_precision_unavailable')
configuration(config)
const state = await db
.prepare('SELECT * FROM platform_analytics_archive_state WHERE dataset=?')
diff --git a/packages/saas-ui/app/components/CfAnalyticsRange.vue b/packages/saas-ui/app/components/CfAnalyticsRange.vue
index 092bfb6c..e3d1295d 100644
--- a/packages/saas-ui/app/components/CfAnalyticsRange.vue
+++ b/packages/saas-ui/app/components/CfAnalyticsRange.vue
@@ -2,6 +2,7 @@
import { parseDate, type CalendarDate } from '@internationalized/date'
import {
ANALYTICS_PRESETS,
+ analyticsYearBoundary,
inputInstant,
rangeInput,
relativeRange,
@@ -104,6 +105,7 @@ function customRange() {
: ['h', '小时'].includes(unit)
? 3600
: 86400)
+ const now = Date.now()
if (seconds < 60 || seconds > 366 * 86400) {
error.value = tr(
'Choose between one minute and one year.',
@@ -111,7 +113,15 @@ function customRange() {
)
return
}
- choose(seconds)
+ const range = relativeRange(seconds, now)
+ if (Date.parse(range.from) < analyticsYearBoundary(now)) {
+ error.value = tr(
+ 'Choose between one minute and one year.',
+ '请选择一分钟至一年之间的时间段。',
+ )
+ return
+ }
+ useRange(range)
}
function chooseAll() {
useRange({ ...relativeRange(365 * 86400), allTime: true })
@@ -131,12 +141,7 @@ function apply() {
!Number.isFinite(end) ||
start >= end ||
end > Date.now() + 60000 ||
- start <
- Date.UTC(
- new Date().getUTCFullYear() - 1,
- new Date().getUTCMonth(),
- new Date().getUTCDate(),
- )
+ start < analyticsYearBoundary()
) {
error.value = tr(
'Choose a valid range within the past year.',
diff --git a/packages/saas-ui/app/utils/analytics-range.ts b/packages/saas-ui/app/utils/analytics-range.ts
index 3f1eccb9..8cbd72ef 100644
--- a/packages/saas-ui/app/utils/analytics-range.ts
+++ b/packages/saas-ui/app/utils/analytics-range.ts
@@ -7,6 +7,14 @@ export type AnalyticsDateRange = {
export const ANALYTICS_PRESETS = [
1800, 3600, 21600, 43200, 86400, 604800, 2592000,
] as const
+export function analyticsYearBoundary(now = Date.now()) {
+ const date = new Date(now)
+ return Date.UTC(
+ date.getUTCFullYear() - 1,
+ date.getUTCMonth(),
+ date.getUTCDate(),
+ )
+}
export function relativeRange(
seconds: number,
now = Date.now(),
diff --git a/tests/analytics-archive.test.ts b/tests/analytics-archive.test.ts
index 17490d27..62b86dce 100644
--- a/tests/analytics-archive.test.ts
+++ b/tests/analytics-archive.test.ts
@@ -2,6 +2,7 @@ import test from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
import { readFile } from 'node:fs/promises'
+import { DatabaseSync } from 'node:sqlite'
import { splitSqlQuery } from './helpers/sql.ts'
import {
archiveAnalytics,
@@ -39,11 +40,13 @@ test('daily archive resumes failures, publishes complete snapshots atomically, a
},
now = Date.parse('2026-11-01T12:00:00Z')
let fail = true
+ let pageSql = ''
const fetcher: typeof fetch = async (_url, opts) => {
const sql = String(opts?.body)
if (sql.includes('MIN(timestamp)'))
return new Response(JSON.stringify({ data: [{ day: '2026-10-28' }] }))
if (sql.includes('AS workspaceId')) {
+ if (sql.includes('blob6 AS label')) pageSql = sql
if (fail && sql.includes('blob9 AS label')) {
fail = false
return new Response('Temporary failure', { status: 503 })
@@ -111,8 +114,8 @@ test('daily archive resumes failures, publishes complete snapshots atomically, a
db,
config,
{
- from: '2025-11-01',
- to: '2026-11-01',
+ from: '2025-11-01T12:00:00Z',
+ to: '2026-11-01T12:00:00Z',
allTime: true,
kind: 'site',
workspaceId: 1,
@@ -165,7 +168,84 @@ test('daily archive resumes failures, publishes complete snapshots atomically, a
)
assert.equal(historic.summary.pv, 10)
assert.equal(historic.summary.uv, 3)
+ // Execute the generated ranking and pagination over more labels than the cap.
+ const sqlite = new DatabaseSync(':memory:')
+ try {
+ sqlite.function('toDateTime', (value) => value)
+ sqlite.exec(
+ 'CREATE TABLE archive_fixture (blob1, blob2, blob3, blob4, blob6, blob7, timestamp, _sample_interval)',
+ )
+ const insert = sqlite.prepare(
+ 'INSERT INTO archive_fixture VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
+ )
+ for (let i = 0; i < 5001; i++)
+ insert.run(
+ 'v1',
+ 'site',
+ '1',
+ '11',
+ `label-${String(i).padStart(4, '0')}`,
+ `visitor-${i}`,
+ '2026-10-28 12:00:00',
+ i === 5000 ? 10 : 1,
+ )
+ const query = pageSql
+ .replace(
+ /timestamp>=toDateTime\('[^']+'\)/,
+ "timestamp>=toDateTime('2026-10-28 00:00:00')",
+ )
+ .replace(
+ /timestamp<toDateTime\('[^']+'\)/,
+ "timestamp<toDateTime('2026-10-29 00:00:00')",
+ )
+ .replace(' FORMAT JSON', '')
+ const labels: string[] = []
+ for (let offset = 0; offset < 5000; offset += 250)
+ labels.push(
+ ...sqlite
+ .prepare(query.replace(/OFFSET \d+/, `OFFSET ${offset}`))
+ .all()
+ .map((row) => String(row.label)),
+ )
+ assert.equal(labels.length, 5000)
+ assert.equal(new Set(labels).size, 5000)
+ assert.ok(labels.includes('label-5000'))
+ assert.ok(!labels.includes('label-4999'))
+ assert.deepEqual(labels, [...labels].sort())
+ } finally {
+ sqlite.close()
+ }
} finally {
await mf.dispose()
}
})
+
+test('historical timestamp bounds are rejected before archive or live queries', async () => {
+ const db = {
+ prepare: () => {
+ throw new Error('archive queried')
+ },
+ } as any
+ const config = {
+ accountId: 'a'.repeat(32),
+ token: 'fixture',
+ dataset: 'archive_fixture',
+ }
+ const fetcher: typeof fetch = async () => {
+ throw new Error('live queried')
+ }
+ for (const [from, to] of [
+ ['2026-10-01T12:00:00Z', '2026-10-02'],
+ ['2026-10-01', '2026-10-02T00:00:00Z'],
+ ])
+ await assert.rejects(
+ historicalAnalytics(
+ db,
+ config,
+ { from: from!, to: to! },
+ Date.now(),
+ fetcher,
+ ),
+ { message: 'analytics_range_precision_unavailable' },
+ )
+})
diff --git a/tests/analytics-presentation.test.ts b/tests/analytics-presentation.test.ts
index 899b7342..45bd15ee 100644
--- a/tests/analytics-presentation.test.ts
+++ b/tests/analytics-presentation.test.ts
@@ -11,6 +11,7 @@ import {
rangeInput,
inputInstant,
ANALYTICS_PRESETS,
+ analyticsYearBoundary,
} from '../packages/saas-ui/app/utils/analytics-range.ts'
test('sparse day/minute/hour reports preserve the selected UTC axis and their real values', () => {
const days = trafficDays(
@@ -32,6 +33,22 @@ test('sparse day/minute/hour reports preserve the selected UTC axis and their re
assert.equal(minutes[1]!.label, '2026-11-01T11:31:00Z')
assert.equal(trafficDays([], '2025-11-01', '2026-11-01').length, 366)
})
+test('year boundary accepts a leap year and excludes durations before the UTC calendar boundary', () => {
+ const leapNow = Date.parse('2024-12-31T00:00:00Z')
+ assert.equal(
+ Date.parse(relativeRange(366 * 86400, leapNow).from),
+ analyticsYearBoundary(leapNow),
+ )
+ const ordinaryNow = Date.parse('2025-12-31T00:00:00Z')
+ assert.ok(
+ Date.parse(relativeRange(366 * 86400, ordinaryNow).from) <
+ analyticsYearBoundary(ordinaryNow),
+ )
+ assert.equal(
+ Date.parse(relativeRange(365 * 86400, ordinaryNow).from),
+ analyticsYearBoundary(ordinaryNow),
+ )
+})
test('relative presets and local/UTC inputs retain actual instants across timezone changes', () => {
assert.deepEqual(
ANALYTICS_PRESETS,