fix(relational): prevent orphaned website_event sessions and keep event pagination stable. Closes #4462.

This commit is contained in:
Francis Cao committed 2026-08-18 14:16:03 -07:00
1 parent 1a91b5f1f3
commit c3a70b123d
4 files changed
+58 -9

No files matched your search

+48 -4
View File
@@ -7,7 +7,7 @@ process.env.APP_SECRET = 'route-send-test-secret';
import { isbot } from 'isbot';
import clickhouse from '@/lib/clickhouse';
import { CACHE_TOKEN_TYPE, EVENT_TYPE } from '@/lib/constants';
import { secret } from '@/lib/crypto';
import { getSalt, secret, uuid } from '@/lib/crypto';
import { getClientInfo, hasBlockedIp } from '@/lib/detect';
import { createToken, parseToken } from '@/lib/jwt';
import { fetchWebsite } from '@/lib/load';
@@ -93,6 +93,13 @@ function callPOST(
);
}
function makeComputedSessionId(sourceId: string, timestamp = 1704067200) {
const createdAt = new Date(timestamp * 1000);
const sessionSalt = getSalt(process.env.SALT_ROTATION || 'month', createdAt);
return uuid(sourceId, defaultClientInfo.ip, defaultClientInfo.userAgent, sessionSalt);
}
beforeEach(() => {
vi.clearAllMocks();
(clickhouse as any).enabled = false;
@@ -540,11 +547,12 @@ describe('cache token handling', () => {
);
}
test('a valid cache token skips website lookup and session creation', async () => {
const token = makeCacheToken();
test('a valid cache token skips website lookup and session creation when it matches the computed session', async () => {
const timestamp = 1704067200;
const token = makeCacheToken({ sessionId: makeComputedSessionId(WEBSITE_ID, timestamp) });
const response = await callPOST(
{ type: 'event', payload: { website: WEBSITE_ID, url: '/' } },
{ type: 'event', payload: { website: WEBSITE_ID, url: '/', timestamp } },
{ headers: { 'x-umami-cache': token } },
);
@@ -553,6 +561,42 @@ describe('cache token handling', () => {
await expect(response.json()).resolves.toMatchObject({ visitId: 'cached-visit' });
});
test('a valid cache token creates the computed session before event writes when the cached session differs', async () => {
const token = makeCacheToken({ sessionId: 'cached-session' });
await callPOST(
{ type: 'event', payload: { website: WEBSITE_ID, url: '/' } },
{ headers: { 'x-umami-cache': token } },
);
expect(createSessionMock).toHaveBeenCalledTimes(1);
const createdSession = createSessionMock.mock.calls[0][0] as Record<string, any>;
const savedEvent = saveEventMock.mock.calls[0][0] as Record<string, any>;
expect(createdSession.id).not.toBe('cached-session');
expect(savedEvent.sessionId).toBe(createdSession.id);
});
test('a valid cache token creates the computed session before identify writes when the cached session differs', async () => {
const token = makeCacheToken({ sessionId: 'cached-session' });
await callPOST(
{ type: 'identify', payload: { website: WEBSITE_ID, id: 'user-42', data: { plan: 'pro' } } },
{ headers: { 'x-umami-cache': token } },
);
expect(createSessionMock).toHaveBeenCalledTimes(1);
const createdSession = createSessionMock.mock.calls[0][0] as Record<string, any>;
const savedLink = saveSessionLinkMock.mock.calls[0][0] as Record<string, any>;
const updatedSession = updateSessionMock.mock.calls[0][0] as Record<string, any>;
const savedSessionData = saveSessionDataMock.mock.calls[0][0] as Record<string, any>;
expect(createdSession.id).not.toBe('cached-session');
expect(savedLink.sessionId).toBe(createdSession.id);
expect(updatedSession.sessionId).toBe(createdSession.id);
expect(savedSessionData.sessionId).toBe(createdSession.id);
});
test('an invalid cache token falls back to website lookup', async () => {
await callPOST(
{ type: 'event', payload: { website: WEBSITE_ID, url: '/' } },
+3 -1
View File
@@ -156,9 +156,11 @@ export async function POST(request: Request) {
const visitSalt = hash(startOfHour(createdAt).toUTCString());
const sessionId = uuid(sourceId, ip, userAgent, sessionSalt);
const shouldEnsureSession =
!clickhouse.enabled && !!websiteId && !!cache?.sessionId && cache.sessionId !== sessionId;
// Create a session if not found
if (!clickhouse.enabled && !cache?.sessionId) {
if ((!clickhouse.enabled && !cache?.sessionId) || shouldEnsureSession) {
await createSession({
id: sessionId,
websiteId: sourceId,
@@ -5,8 +5,7 @@ const prismaParseFiltersResult = {
filterQuery: 'and website_event.url_path = {{path}}',
dateQuery: 'and website_event.created_at between {{startDate}} and {{endDate}}',
cohortQuery: 'join cohort on cohort.session_id = website_event.session_id',
joinSessionQuery:
'inner join session on website_event.session_id = session.session_id and website_event.website_id = session.website_id',
joinSessionQuery: '',
};
const clickhouseParseFiltersResult = {
@@ -67,7 +66,7 @@ afterEach(() => {
});
describe('getWebsiteEvents', () => {
test('postgres counts from the lightweight filtered event query and enriches only the paged rows', async () => {
test('postgres counts from the lightweight filtered event query and keeps paged rows even when session enrichment is missing', async () => {
const { getWebsiteEvents, prismaRawQuery } = await loadModule({ mode: 'prisma' });
const result = await getWebsiteEvents('website-1', {
@@ -86,6 +85,7 @@ describe('getWebsiteEvents', () => {
expect(countQuery).toContain('select count(*) as num from (select 1 from (');
expect(countQuery).not.toContain('exists(');
expect(countQuery).toContain('event_name ilike {{eventSearch}}');
expect(countQuery).not.toContain('inner join session on website_event.session_id');
expect(dataQuery).toContain('with paged_events as (');
expect(dataQuery).toContain('paged_event_data as (');
expect(dataQuery).toContain(
@@ -96,6 +96,9 @@ describe('getWebsiteEvents', () => {
expect(dataQuery).toContain(
'left join paged_event_data on paged_event_data.event_id = website_event.event_id',
);
expect(dataQuery).toContain(
'left join session on session.session_id = website_event.session_id',
);
expect(dataQuery).toContain('order by paged_events.created_at desc');
expect(dataQuery).toContain('(paged_event_data.event_id is not null) as "hasData"');
expect(countParams).toMatchObject({ eventSearch: '%signup%' });
+1 -1
View File
@@ -100,7 +100,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
(paged_event_data.event_id is not null) as "hasData"
from paged_events
join website_event on website_event.event_id = paged_events.event_id
join session on session.session_id = website_event.session_id
left join session on session.session_id = website_event.session_id
and session.website_id = website_event.website_id
left join paged_event_data on paged_event_data.event_id = website_event.event_id
order by paged_events.created_at desc