Session/Event Property CSS fixes. Session Activity performance changes
This commit is contained in:
1 parent
92fd4b71c3
commit
d4e864e4d9
8 files changed
+161
-90
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { Box, Column, Label, Row, Text } from '@umami/react-zen';
|
||||
import { Column, Label, Row, Text } from '@umami/react-zen';
|
||||
import { Badge } from '@/components/common/Badge';
|
||||
import { Empty } from '@/components/common/Empty';
|
||||
import { LoadingPanel } from '@/components/common/LoadingPanel';
|
||||
import { useSessionDataQuery } from '@/components/hooks';
|
||||
@@ -17,11 +18,9 @@ export function SessionData({ websiteId, sessionId }: { websiteId: string; sessi
|
||||
<Label>{dataKey}</Label>
|
||||
<Row alignItems="center" gap>
|
||||
<Text>{stringValue}</Text>
|
||||
<Box paddingY="1" paddingX="2" border borderRadius borderColor="muted">
|
||||
<Text color="muted" size="xs">
|
||||
{DATA_TYPES[dataType]}
|
||||
</Text>
|
||||
</Box>
|
||||
<Badge variant="gray" dot={false}>
|
||||
{DATA_TYPES[dataType]}
|
||||
</Badge>
|
||||
</Row>
|
||||
</Column>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import { endOfMonth, startOfMonth } from 'date-fns';
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||
import { canViewWebsiteSection } from '@/permissions';
|
||||
import { getLinkedSessionIds, getSessionActivity } from '@/queries/sql';
|
||||
import { GET } from './route';
|
||||
|
||||
vi.mock('@/lib/request', () => ({
|
||||
getQueryFilters: vi.fn(),
|
||||
parseRequest: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/permissions', () => ({
|
||||
canViewWebsiteSection: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/queries/sql', () => ({
|
||||
getLinkedDistinctIds: vi.fn(),
|
||||
getLinkedSessionIds: vi.fn(),
|
||||
getSessionActivity: vi.fn(),
|
||||
}));
|
||||
|
||||
const parseRequestMock = vi.mocked(parseRequest);
|
||||
const getQueryFiltersMock = vi.mocked(getQueryFilters);
|
||||
const canViewWebsiteSectionMock = vi.mocked(canViewWebsiteSection);
|
||||
const getLinkedSessionIdsMock = vi.mocked(getLinkedSessionIds);
|
||||
const getSessionActivityMock = vi.mocked(getSessionActivity);
|
||||
|
||||
beforeEach(() => {
|
||||
parseRequestMock.mockReset();
|
||||
getQueryFiltersMock.mockReset();
|
||||
canViewWebsiteSectionMock.mockReset();
|
||||
getLinkedSessionIdsMock.mockReset();
|
||||
getSessionActivityMock.mockReset();
|
||||
});
|
||||
|
||||
test('uses linked session months to widen stitched activity without scanning event bounds', async () => {
|
||||
const query = {
|
||||
startAt: +new Date('2026-07-20T23:38:54.000Z'),
|
||||
endAt: +new Date('2026-07-21T00:08:01.000Z'),
|
||||
distinctId: 'bob@aol.com',
|
||||
};
|
||||
const linkedStart = new Date('2026-05-15T12:00:00.000Z');
|
||||
const linkedEnd = new Date('2026-08-02T12:00:00.000Z');
|
||||
const filters = {
|
||||
startDate: startOfMonth(linkedStart),
|
||||
endDate: endOfMonth(linkedEnd),
|
||||
};
|
||||
|
||||
parseRequestMock.mockResolvedValue({ auth: {}, query, error: undefined });
|
||||
canViewWebsiteSectionMock.mockResolvedValue(true);
|
||||
getLinkedSessionIdsMock.mockResolvedValue([
|
||||
{ sessionId: 'session-2', createdAt: linkedStart.toISOString() },
|
||||
{ sessionId: 'session-3', createdAt: linkedEnd.toISOString() },
|
||||
]);
|
||||
getQueryFiltersMock.mockResolvedValue(filters);
|
||||
getSessionActivityMock.mockResolvedValue([{ eventId: 'event-1' }]);
|
||||
|
||||
const response = await GET(
|
||||
new Request(
|
||||
'http://localhost/api/websites/website-1/sessions/session-1/activity?startAt=1784590734000&endAt=1784592481000&distinctId=bob%40aol.com',
|
||||
),
|
||||
{
|
||||
params: Promise.resolve({ websiteId: 'website-1', sessionId: 'session-1' }),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(getLinkedSessionIdsMock).toHaveBeenCalledWith('website-1', 'bob@aol.com');
|
||||
expect(getQueryFiltersMock).toHaveBeenCalledWith(
|
||||
{
|
||||
...query,
|
||||
startAt: +startOfMonth(linkedStart),
|
||||
endAt: +endOfMonth(linkedEnd),
|
||||
},
|
||||
'website-1',
|
||||
);
|
||||
expect(getSessionActivityMock).toHaveBeenCalledWith(
|
||||
'website-1',
|
||||
['session-1', 'session-2', 'session-3'],
|
||||
filters,
|
||||
);
|
||||
await expect(response.json()).resolves.toEqual([{ eventId: 'event-1' }]);
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { endOfMonth, startOfMonth } from 'date-fns';
|
||||
import { z } from 'zod';
|
||||
import { FIELD_LENGTH } from '@/lib/constants';
|
||||
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||
@@ -7,7 +8,6 @@ import {
|
||||
getLinkedDistinctIds,
|
||||
getLinkedSessionIds,
|
||||
getSessionActivity,
|
||||
getSessionActivityBounds,
|
||||
} from '@/queries/sql';
|
||||
|
||||
export async function GET(
|
||||
@@ -42,18 +42,19 @@ export async function GET(
|
||||
: await getLinkedDistinctIds(websiteId, sessionId);
|
||||
|
||||
if (distinctIds.length) {
|
||||
const links = await Promise.all(distinctIds.map(distinctId => getLinkedSessionIds(websiteId, distinctId)));
|
||||
const links = await Promise.all(
|
||||
distinctIds.map(distinctId => getLinkedSessionIds(websiteId, distinctId)),
|
||||
);
|
||||
const linkedIds = links.flatMap(group => group.map(link => link.sessionId));
|
||||
const linkedDates = links
|
||||
.flatMap(group => group.map(link => +new Date(link.createdAt)))
|
||||
.filter(timestamp => !Number.isNaN(timestamp));
|
||||
|
||||
sessionIds = Array.from(new Set([sessionId, ...linkedIds]));
|
||||
|
||||
if (sessionIds.length > 1) {
|
||||
const bounds = await getSessionActivityBounds(websiteId, sessionIds);
|
||||
|
||||
if (bounds?.firstAt && bounds?.lastAt) {
|
||||
startAt = Math.min(startAt, +new Date(bounds.firstAt));
|
||||
endAt = Math.max(endAt, +new Date(bounds.lastAt));
|
||||
}
|
||||
if (sessionIds.length > 1 && linkedDates.length) {
|
||||
startAt = Math.min(startAt, +startOfMonth(new Date(Math.min(...linkedDates))));
|
||||
endAt = Math.max(endAt, +endOfMonth(new Date(Math.max(...linkedDates))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
align-self: flex-start;
|
||||
flex-shrink: 0;
|
||||
gap: 6px;
|
||||
padding: 2px 8px;
|
||||
border-radius: 6px;
|
||||
|
||||
@@ -148,8 +148,18 @@ export function PropertyFilterRecord({
|
||||
};
|
||||
|
||||
return (
|
||||
<Column>
|
||||
<Label>{filter.propertyName}</Label>
|
||||
<Column minWidth="0">
|
||||
<Label
|
||||
title={filter.propertyName}
|
||||
style={{
|
||||
display: 'block',
|
||||
overflow: 'hidden',
|
||||
textOverflow: 'ellipsis',
|
||||
whiteSpace: 'nowrap',
|
||||
}}
|
||||
>
|
||||
{filter.propertyName}
|
||||
</Label>
|
||||
<Grid columns="1fr auto" gap>
|
||||
<Grid columns={{ base: '1fr', md: '200px 1fr' }} gap>
|
||||
<Select
|
||||
|
||||
@@ -13,12 +13,13 @@ import {
|
||||
Row,
|
||||
} from '@umami/react-zen';
|
||||
import { format } from 'date-fns';
|
||||
import { Badge } from '@/components/common/Badge';
|
||||
import { Empty } from '@/components/common/Empty';
|
||||
import { LoadingPanel } from '@/components/common/LoadingPanel';
|
||||
import { useMessages, useMobile, usePropertyFieldsQuery } from '@/components/hooks';
|
||||
import type { PropertyDataSource } from '@/components/hooks/queries/usePropertyFieldsQuery';
|
||||
import { Plus } from '@/components/icons';
|
||||
import { DATA_TYPE, OPERATORS } from '@/lib/constants';
|
||||
import { DATA_TYPE, DATA_TYPES, OPERATORS } from '@/lib/constants';
|
||||
import type { PropertyFilter } from '@/lib/types';
|
||||
import { PropertyFilterRecord } from './PropertyFilterRecord';
|
||||
|
||||
@@ -48,9 +49,17 @@ export function PropertyFilters({
|
||||
});
|
||||
const fields = providedFields ?? queriedFields ?? [];
|
||||
|
||||
const handleAdd = (propertyName: string) => {
|
||||
const field = (fields as any[]).find(f => f.propertyName === propertyName);
|
||||
const dataType: number = field?.dataType ?? DATA_TYPE.string;
|
||||
// The same property can be stored under multiple data types, which renders as
|
||||
// identical-looking rows. Tag only those, so names keep the full column width.
|
||||
const duplicateNames = new Set(
|
||||
(fields as any[])
|
||||
.map(f => f.propertyName)
|
||||
.filter((name, i, names) => names.indexOf(name) !== i),
|
||||
);
|
||||
|
||||
const handleAdd = (field: { propertyName: string; dataType: number }) => {
|
||||
const { propertyName } = field;
|
||||
const dataType: number = field.dataType ?? DATA_TYPE.string;
|
||||
|
||||
onChange([
|
||||
...value,
|
||||
@@ -101,10 +110,13 @@ export function PropertyFilters({
|
||||
{(fields as any[]).map(field => (
|
||||
<MenuItem
|
||||
key={`${field.propertyName}:${field.dataType}`}
|
||||
id={field.propertyName}
|
||||
onAction={key => handleAdd(key.toString())}
|
||||
id={`${field.propertyName}:${field.dataType}`}
|
||||
onAction={() => handleAdd(field)}
|
||||
>
|
||||
{field.propertyName}
|
||||
<PropertyLabel
|
||||
field={field}
|
||||
showDataType={duplicateNames.has(field.propertyName)}
|
||||
/>
|
||||
</MenuItem>
|
||||
))}
|
||||
</Menu>
|
||||
@@ -122,10 +134,13 @@ export function PropertyFilters({
|
||||
{(fields as any[]).map(field => (
|
||||
<ListItem
|
||||
key={`${field.propertyName}:${field.dataType}`}
|
||||
id={field.propertyName}
|
||||
onClick={() => handleAdd(field.propertyName)}
|
||||
id={`${field.propertyName}:${field.dataType}`}
|
||||
onClick={() => handleAdd(field)}
|
||||
>
|
||||
{field.propertyName}
|
||||
<PropertyLabel
|
||||
field={field}
|
||||
showDataType={duplicateNames.has(field.propertyName)}
|
||||
/>
|
||||
</ListItem>
|
||||
))}
|
||||
</List>
|
||||
@@ -164,3 +179,27 @@ export function PropertyFilters({
|
||||
</LoadingPanel>
|
||||
);
|
||||
}
|
||||
|
||||
function PropertyLabel({
|
||||
field,
|
||||
showDataType,
|
||||
}: {
|
||||
field: { propertyName: string; dataType: number };
|
||||
showDataType: boolean;
|
||||
}) {
|
||||
return (
|
||||
<Row alignItems="center" gap="2" minWidth="0">
|
||||
<span
|
||||
title={field.propertyName}
|
||||
style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}
|
||||
>
|
||||
{field.propertyName}
|
||||
</span>
|
||||
{showDataType && (
|
||||
<Badge variant="gray" dot={false}>
|
||||
{DATA_TYPES[field.dataType]}
|
||||
</Badge>
|
||||
)}
|
||||
</Row>
|
||||
);
|
||||
}
|
||||
@@ -46,7 +46,6 @@ export * from './sessions/createSession';
|
||||
export * from './sessions/getLinkedDistinctIds';
|
||||
export * from './sessions/getLinkedSessionIds';
|
||||
export * from './sessions/getSessionActivity';
|
||||
export * from './sessions/getSessionActivityBounds';
|
||||
export * from './sessions/getSessionData';
|
||||
export * from './sessions/getSessionDataActivityStats';
|
||||
export * from './sessions/getSessionDataArraySeries';
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
const FUNCTION_NAME = 'getSessionActivityBounds';
|
||||
|
||||
interface SessionActivityBounds {
|
||||
firstAt: string | Date;
|
||||
lastAt: string | Date;
|
||||
}
|
||||
|
||||
export async function getSessionActivityBounds(
|
||||
...args: [websiteId: string, sessionIds: string[]]
|
||||
): Promise<SessionActivityBounds | null> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
sessionIds: string[],
|
||||
): Promise<SessionActivityBounds | null> {
|
||||
const { rawQuery } = prisma;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
min(created_at) as "firstAt",
|
||||
max(created_at) as "lastAt"
|
||||
from website_event
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and session_id = any({{sessionIds}}::uuid[])
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
`,
|
||||
{ websiteId, sessionIds },
|
||||
FUNCTION_NAME,
|
||||
).then(result => (result?.[0]?.firstAt && result?.[0]?.lastAt ? result[0] : null));
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
sessionIds: string[],
|
||||
): Promise<SessionActivityBounds | null> {
|
||||
const { rawQuery, getDateStringSQL } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
${getDateStringSQL('min(created_at)')} as firstAt,
|
||||
${getDateStringSQL('max(created_at)')} as lastAt
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and session_id in {sessionIds:Array(UUID)}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
`,
|
||||
{ websiteId, sessionIds },
|
||||
FUNCTION_NAME,
|
||||
).then(result => (result?.[0]?.firstAt && result?.[0]?.lastAt ? result[0] : null));
|
||||
}
|
||||
Reference in new issue
Block a user