Merge branch 'dev' of https://github.com/umami-software/umami into dev
This commit is contained in:
commit
6c2155d496
157 files changed
+1389
-643
No files matched your search
@@ -1,8 +1,11 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import { BOARD_TYPES } from '@/lib/boards';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const BOARD_SORT_FIELDS = ['name', 'description', 'type', 'createdAt'] as const;
|
||||
|
||||
export async function findBoard(criteria: Prisma.BoardFindUniqueArgs) {
|
||||
return prisma.client.board.findUnique(criteria);
|
||||
}
|
||||
@@ -16,7 +19,8 @@ export async function getBoard(boardId: string) {
|
||||
}
|
||||
|
||||
export async function getBoards(criteria: Prisma.BoardFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, BOARD_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where: Prisma.BoardWhereInput = {
|
||||
@@ -24,7 +28,7 @@ export async function getBoards(criteria: Prisma.BoardFindManyArgs, filters: Que
|
||||
...getSearchParameters(search, [{ name: 'contains' }, { description: 'contains' }]),
|
||||
};
|
||||
|
||||
return pagedQuery('board', { ...criteria, where }, filters);
|
||||
return pagedQuery('board', { ...criteria, where }, sortFilters);
|
||||
}
|
||||
|
||||
export async function getUserBoards(userId: string, filters?: QueryFilters) {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const LINK_SORT_FIELDS = ['name', 'slug', 'url', 'createdAt'] as const;
|
||||
|
||||
export async function findLink(criteria: Prisma.LinkFindUniqueArgs) {
|
||||
return prisma.client.link.findUnique(criteria);
|
||||
}
|
||||
@@ -15,7 +18,8 @@ export async function getLink(linkId: string) {
|
||||
}
|
||||
|
||||
export async function getLinks(criteria: Prisma.LinkFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, LINK_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where: Prisma.LinkWhereInput = {
|
||||
@@ -27,7 +31,7 @@ export async function getLinks(criteria: Prisma.LinkFindManyArgs, filters: Query
|
||||
]),
|
||||
};
|
||||
|
||||
return pagedQuery('link', { ...criteria, where }, filters);
|
||||
return pagedQuery('link', { ...criteria, where }, sortFilters);
|
||||
}
|
||||
|
||||
export async function getUserLinks(userId: string, filters?: QueryFilters) {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const PIXEL_SORT_FIELDS = ['name', 'slug', 'createdAt'] as const;
|
||||
|
||||
export async function findPixel(criteria: Prisma.PixelFindUniqueArgs) {
|
||||
return prisma.client.pixel.findUnique(criteria);
|
||||
}
|
||||
@@ -15,14 +18,15 @@ export async function getPixel(pixelId: string) {
|
||||
}
|
||||
|
||||
export async function getPixels(criteria: Prisma.PixelFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, PIXEL_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
|
||||
const where: Prisma.PixelWhereInput = {
|
||||
...criteria.where,
|
||||
...prisma.getSearchParameters(search, [{ name: 'contains' }, { slug: 'contains' }]),
|
||||
};
|
||||
|
||||
return prisma.pagedQuery('pixel', { ...criteria, where }, filters);
|
||||
return prisma.pagedQuery('pixel', { ...criteria, where }, sortFilters);
|
||||
}
|
||||
|
||||
export async function getUserPixels(userId: string, filters?: QueryFilters) {
|
||||
|
||||
@@ -2,10 +2,14 @@ import { Prisma, type Team } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import prisma from '@/lib/prisma';
|
||||
import redis from '@/lib/redis';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { PageResult, QueryFilters } from '@/lib/types';
|
||||
|
||||
import TeamFindManyArgs = Prisma.TeamFindManyArgs;
|
||||
|
||||
const TEAM_SORT_FIELDS = ['name', 'createdAt'] as const;
|
||||
|
||||
export async function findTeam(criteria: Prisma.TeamFindUniqueArgs): Promise<Team> {
|
||||
return prisma.client.team.findUnique(criteria);
|
||||
}
|
||||
@@ -29,7 +33,8 @@ export async function getTeams(
|
||||
filters: QueryFilters,
|
||||
): Promise<PageResult<Team[]>> {
|
||||
const { getSearchParameters } = prisma;
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, TEAM_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
|
||||
const where: Prisma.TeamWhereInput = {
|
||||
...criteria.where,
|
||||
@@ -42,7 +47,7 @@ export async function getTeams(
|
||||
...criteria,
|
||||
where,
|
||||
},
|
||||
filters,
|
||||
sortFilters,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -144,6 +149,31 @@ export async function deleteTeam(teamId: string) {
|
||||
const { client, transaction } = prisma;
|
||||
const cloudMode = !!process.env.CLOUD_MODE;
|
||||
|
||||
const [links, pixels, boards] = await Promise.all([
|
||||
client.link.findMany({
|
||||
where: { teamId },
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.pixel.findMany({
|
||||
where: { teamId },
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.board.findMany({ where: { teamId }, select: { id: true } }),
|
||||
]);
|
||||
const entityIds = [...links.map(l => l.id), ...pixels.map(p => p.id), ...boards.map(b => b.id)];
|
||||
// Only invalidate Redis cache for slugs that are still live (not already soft-deleted).
|
||||
const linkSlugs = links.filter(l => !l.deletedAt).map(l => l.slug);
|
||||
const pixelSlugs = pixels.filter(p => !p.deletedAt).map(p => p.slug);
|
||||
|
||||
const invalidateRedis = async () => {
|
||||
if (redis.enabled && (linkSlugs.length || pixelSlugs.length)) {
|
||||
await Promise.all([
|
||||
...linkSlugs.map(slug => redis.client.del(`link:${slug}`)),
|
||||
...pixelSlugs.map(slug => redis.client.del(`pixel:${slug}`)),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
if (cloudMode) {
|
||||
return transaction([
|
||||
client.team.update({
|
||||
@@ -154,7 +184,21 @@ export async function deleteTeam(teamId: string) {
|
||||
id: teamId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
// deletedAt: null avoids restamping rows that were already soft-deleted earlier.
|
||||
client.link.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { teamId, deletedAt: null },
|
||||
}),
|
||||
client.pixel.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { teamId, deletedAt: null },
|
||||
}),
|
||||
client.board.deleteMany({ where: { teamId } }),
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
return transaction([
|
||||
@@ -163,10 +207,17 @@ export async function deleteTeam(teamId: string) {
|
||||
teamId,
|
||||
},
|
||||
}),
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
client.link.deleteMany({ where: { teamId } }),
|
||||
client.pixel.deleteMany({ where: { teamId } }),
|
||||
client.board.deleteMany({ where: { teamId } }),
|
||||
client.team.delete({
|
||||
where: {
|
||||
id: teamId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
@@ -2,10 +2,14 @@ import { Prisma } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import { getRandomChars } from '@/lib/generate';
|
||||
import prisma from '@/lib/prisma';
|
||||
import redis from '@/lib/redis';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters, Role } from '@/lib/types';
|
||||
|
||||
import UserFindManyArgs = Prisma.UserFindManyArgs;
|
||||
|
||||
const USER_SORT_FIELDS = ['username', 'role', 'createdAt'] as const;
|
||||
|
||||
export interface GetUserOptions {
|
||||
includePassword?: boolean;
|
||||
showDeleted?: boolean;
|
||||
@@ -46,7 +50,11 @@ export async function getUserByUsername(username: string, options: GetUserOption
|
||||
}
|
||||
|
||||
export async function getUsers(criteria: UserFindManyArgs, filters: QueryFilters = {}) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, USER_SORT_FIELDS, {
|
||||
orderBy: 'createdAt',
|
||||
sortDescending: true,
|
||||
});
|
||||
const { search } = sortFilters;
|
||||
|
||||
const where: Prisma.UserWhereInput = {
|
||||
...criteria.where,
|
||||
@@ -60,11 +68,7 @@ export async function getUsers(criteria: UserFindManyArgs, filters: QueryFilters
|
||||
...criteria,
|
||||
where,
|
||||
},
|
||||
{
|
||||
orderBy: 'createdAt',
|
||||
sortDescending: true,
|
||||
...filters,
|
||||
},
|
||||
sortFilters,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -126,6 +130,38 @@ export async function deleteUser(userId: string) {
|
||||
|
||||
const teamIds = teams.map(a => a.id);
|
||||
|
||||
// Cloud mode keeps owned teams (and their team-owned content), so cleanup
|
||||
// only covers user-direct rows. Non-cloud hard-deletes owned teams below,
|
||||
// so we must also clean up team-owned content.
|
||||
const ownedFilter = cloudMode
|
||||
? { userId }
|
||||
: { OR: [{ userId }, { teamId: { in: teamIds } }] };
|
||||
|
||||
const [links, pixels, boards] = await Promise.all([
|
||||
client.link.findMany({
|
||||
where: ownedFilter,
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.pixel.findMany({
|
||||
where: ownedFilter,
|
||||
select: { id: true, slug: true, deletedAt: true },
|
||||
}),
|
||||
client.board.findMany({ where: ownedFilter, select: { id: true } }),
|
||||
]);
|
||||
const entityIds = [...links.map(l => l.id), ...pixels.map(p => p.id), ...boards.map(b => b.id)];
|
||||
// Only invalidate Redis cache for slugs that are still live (not already soft-deleted).
|
||||
const linkSlugs = links.filter(l => !l.deletedAt).map(l => l.slug);
|
||||
const pixelSlugs = pixels.filter(p => !p.deletedAt).map(p => p.slug);
|
||||
|
||||
const invalidateRedis = async () => {
|
||||
if (redis.enabled && (linkSlugs.length || pixelSlugs.length)) {
|
||||
await Promise.all([
|
||||
...linkSlugs.map(slug => redis.client.del(`link:${slug}`)),
|
||||
...pixelSlugs.map(slug => redis.client.del(`pixel:${slug}`)),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
if (cloudMode) {
|
||||
return transaction([
|
||||
client.website.updateMany({
|
||||
@@ -143,7 +179,21 @@ export async function deleteUser(userId: string) {
|
||||
id: userId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
// deletedAt: null avoids restamping rows that were already soft-deleted earlier.
|
||||
client.link.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { userId, deletedAt: null },
|
||||
}),
|
||||
client.pixel.updateMany({
|
||||
data: { deletedAt: new Date() },
|
||||
where: { userId, deletedAt: null },
|
||||
}),
|
||||
client.board.deleteMany({ where: { userId } }),
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
return transaction([
|
||||
@@ -194,6 +244,10 @@ export async function deleteUser(userId: string) {
|
||||
],
|
||||
},
|
||||
}),
|
||||
client.share.deleteMany({ where: { entityId: { in: entityIds } } }),
|
||||
client.link.deleteMany({ where: ownedFilter }),
|
||||
client.pixel.deleteMany({ where: ownedFilter }),
|
||||
client.board.deleteMany({ where: ownedFilter }),
|
||||
client.website.deleteMany({
|
||||
where: { id: { in: websiteIds } },
|
||||
}),
|
||||
@@ -202,5 +256,8 @@ export async function deleteUser(userId: string) {
|
||||
id: userId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
]).then(async result => {
|
||||
await invalidateRedis();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
@@ -2,8 +2,11 @@ import type { Prisma, Website } from '@/generated/prisma/client';
|
||||
import { ROLES } from '@/lib/constants';
|
||||
import prisma from '@/lib/prisma';
|
||||
import redis from '@/lib/redis';
|
||||
import { sanitizeSortFilters } from '@/lib/sort';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const WEBSITE_SORT_FIELDS = ['name', 'domain', 'createdAt'] as const;
|
||||
|
||||
export async function findWebsite(criteria: Prisma.WebsiteFindUniqueArgs) {
|
||||
return prisma.client.website.findUnique(criteria);
|
||||
}
|
||||
@@ -23,7 +26,8 @@ export async function getWebsite(websiteId: string) {
|
||||
}
|
||||
|
||||
export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters: QueryFilters) {
|
||||
const { search } = filters;
|
||||
const sortFilters = sanitizeSortFilters(filters, WEBSITE_SORT_FIELDS);
|
||||
const { search } = sortFilters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where: Prisma.WebsiteWhereInput = {
|
||||
@@ -37,7 +41,7 @@ export async function getWebsites(criteria: Prisma.WebsiteFindManyArgs, filters:
|
||||
deletedAt: null,
|
||||
};
|
||||
|
||||
const websites = await pagedQuery('website', { ...criteria, where }, filters);
|
||||
const websites = await pagedQuery('website', { ...criteria, where }, sortFilters);
|
||||
|
||||
return attachShareIdToWebsites(websites);
|
||||
}
|
||||
@@ -62,10 +66,7 @@ export async function getAllUserWebsitesIncludingTeamAccess(userId: string, filt
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
orderBy: 'name',
|
||||
...filters,
|
||||
},
|
||||
sanitizeSortFilters(filters, WEBSITE_SORT_FIELDS, { orderBy: 'name' }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -84,10 +85,7 @@ export async function getUserWebsites(userId: string, filters?: QueryFilters) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
orderBy: 'name',
|
||||
...filters,
|
||||
},
|
||||
sanitizeSortFilters(filters, WEBSITE_SORT_FIELDS, { orderBy: 'name' }),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { DEFAULT_PAGE_SIZE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { EventPropertyFilter, QueryFilters } from '@/lib/types';
|
||||
@@ -27,10 +26,7 @@ async function relationalQuery(
|
||||
eventFilters: EventPropertyFilter[] = [],
|
||||
) {
|
||||
const { timezone = 'utc' } = filters;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
|
||||
const { page = 1, pageSize } = filters;
|
||||
const size = +pageSize || DEFAULT_PAGE_SIZE;
|
||||
const offset = +size * (+page - 1);
|
||||
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
|
||||
|
||||
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
@@ -39,30 +35,10 @@ async function relationalQuery(
|
||||
});
|
||||
const { sql: pfSQL, params: pfParams } = getPropertyFilterQuery(eventFilters, 'event', timezone);
|
||||
|
||||
const countResult = await rawQuery(
|
||||
`
|
||||
select count(distinct website_event.event_id) as num
|
||||
from website_event
|
||||
join event_data on event_data.website_event_id = website_event.event_id
|
||||
and event_data.website_id = {{websiteId::uuid}}
|
||||
and event_data.created_at between {{startDate}} and {{endDate}}
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_name = {{eventName}}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
`,
|
||||
{ ...queryParams, eventName, ...pfParams },
|
||||
);
|
||||
|
||||
const count = countResult[0].num;
|
||||
|
||||
const rows = await rawQuery(
|
||||
return pagedRawQuery(
|
||||
`
|
||||
with paged_events as (
|
||||
select website_event.event_id
|
||||
select website_event.event_id, max(website_event.created_at) as sort_created_at
|
||||
from website_event
|
||||
join event_data on event_data.website_event_id = website_event.event_id
|
||||
and event_data.website_id = {{websiteId::uuid}}
|
||||
@@ -75,68 +51,43 @@ async function relationalQuery(
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
group by website_event.event_id
|
||||
order by max(website_event.created_at) desc
|
||||
limit ${size} offset ${offset}
|
||||
)
|
||||
select
|
||||
website_event.event_id as "eventId",
|
||||
website_event.session_id as "sessionId",
|
||||
website_event.event_name as "eventName",
|
||||
website_event.url_path as "urlPath",
|
||||
website_event.created_at as "createdAt",
|
||||
event_data.data_key as "dataKey",
|
||||
coalesce(
|
||||
case when event_data.data_type = 1 then event_data.string_value end,
|
||||
case when event_data.data_type = 2 then cast(event_data.number_value as varchar) end,
|
||||
case when event_data.data_type = 3 then event_data.string_value end,
|
||||
case when event_data.data_type = 4 then ${getDateStringSQL('event_data.date_value', 'second', timezone)} end,
|
||||
case when event_data.data_type = 5 then event_data.string_value end,
|
||||
''
|
||||
) as "value",
|
||||
event_data.data_type as "dataType"
|
||||
max(website_event.created_at) as "createdAt",
|
||||
array_agg(event_data.data_key order by event_data.data_key asc) as "propertyKeys",
|
||||
array_agg(
|
||||
coalesce(
|
||||
case when event_data.data_type = 1 then event_data.string_value end,
|
||||
case when event_data.data_type = 2 then cast(event_data.number_value as varchar) end,
|
||||
case when event_data.data_type = 3 then event_data.string_value end,
|
||||
case when event_data.data_type = 4 then ${getDateStringSQL('event_data.date_value', 'second', timezone)} end,
|
||||
case when event_data.data_type = 5 then event_data.string_value end,
|
||||
''
|
||||
)
|
||||
order by event_data.data_key asc
|
||||
) as "propertyValues"
|
||||
from event_data
|
||||
join website_event on website_event.event_id = event_data.website_event_id
|
||||
and website_event.website_id = {{websiteId::uuid}}
|
||||
join paged_events on paged_events.event_id = event_data.website_event_id
|
||||
where event_data.website_id = {{websiteId::uuid}}
|
||||
and event_data.created_at between {{startDate}} and {{endDate}}
|
||||
order by website_event.created_at desc
|
||||
group by
|
||||
website_event.event_id,
|
||||
website_event.session_id,
|
||||
website_event.event_name,
|
||||
website_event.url_path,
|
||||
paged_events.sort_created_at
|
||||
order by paged_events.sort_created_at desc
|
||||
`,
|
||||
{ ...queryParams, eventName, ...pfParams },
|
||||
filters,
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
|
||||
// Pivot flat rows into one record per event
|
||||
const eventMap = new Map<
|
||||
string,
|
||||
{
|
||||
eventId: string;
|
||||
sessionId: string;
|
||||
eventName: string;
|
||||
urlPath: string;
|
||||
createdAt: Date;
|
||||
propertyKeys: string[];
|
||||
propertyValues: string[];
|
||||
}
|
||||
>();
|
||||
for (const { eventId, sessionId, eventName: name, urlPath, createdAt, dataKey, value } of rows) {
|
||||
if (!eventMap.has(eventId)) {
|
||||
eventMap.set(eventId, {
|
||||
eventId,
|
||||
sessionId,
|
||||
eventName: name,
|
||||
urlPath,
|
||||
createdAt,
|
||||
propertyKeys: [],
|
||||
propertyValues: [],
|
||||
});
|
||||
}
|
||||
const entry = eventMap.get(eventId);
|
||||
entry.propertyKeys.push(dataKey);
|
||||
entry.propertyValues.push(value ?? '');
|
||||
}
|
||||
|
||||
return { data: [...eventMap.values()], count, page: +page, pageSize: size };
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
@@ -146,10 +97,7 @@ async function clickhouseQuery(
|
||||
eventFilters: EventPropertyFilter[] = [],
|
||||
) {
|
||||
const { timezone = 'UTC' } = filters;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
|
||||
const { page = 1, pageSize } = filters;
|
||||
const size = +pageSize || DEFAULT_PAGE_SIZE;
|
||||
const offset = +size * (+page - 1);
|
||||
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
|
||||
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
@@ -158,31 +106,7 @@ async function clickhouseQuery(
|
||||
});
|
||||
const { sql: pfSQL, params: pfParams } = getPropertyFilterQuery(eventFilters, 'event', timezone);
|
||||
|
||||
const count = await rawQuery(
|
||||
`
|
||||
select count() as num
|
||||
from umami.event_data_pivot
|
||||
any left join (
|
||||
select *
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = 2
|
||||
and event_name = {eventName:String}) website_event
|
||||
on website_event.event_id = event_data_pivot.event_id
|
||||
and website_event.session_id = event_data_pivot.session_id
|
||||
and website_event.website_id = event_data_pivot.website_id
|
||||
${cohortQuery}
|
||||
where event_data_pivot.website_id = {websiteId:UUID}
|
||||
and event_data_pivot.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_data_pivot.event_name = {eventName:String}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
`,
|
||||
{ ...queryParams, eventName, ...pfParams },
|
||||
).then((res: any) => res[0].num);
|
||||
|
||||
const data = await rawQuery(
|
||||
return pagedRawQuery(
|
||||
`
|
||||
select
|
||||
event_data_pivot.event_id as eventId,
|
||||
@@ -224,11 +148,9 @@ async function clickhouseQuery(
|
||||
event_data_pivot.url_path,
|
||||
event_data_pivot.created_at
|
||||
order by event_data_pivot.created_at desc
|
||||
limit ${size} offset ${offset}
|
||||
`,
|
||||
{ ...queryParams, eventName, ...pfParams },
|
||||
filters,
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
|
||||
return { data, count, page: +page, pageSize: size };
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -55,6 +56,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
join session on session.session_id = website_event.session_id
|
||||
and session.website_id = website_event.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
${searchQuery}
|
||||
@@ -107,6 +109,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
${searchQuery}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -38,6 +39,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
on session.session_id = website_event.session_id
|
||||
and session.website_id = website_event.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${dateQuery}
|
||||
order by website_event.created_at desc
|
||||
@@ -71,6 +73,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters): Promis
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${dateQuery}
|
||||
order by createdAt desc
|
||||
|
||||
@@ -96,10 +96,6 @@ async function clickhouseQuery(websiteId: string, column: string, filters: Query
|
||||
excludeDomain = `and referrer_domain != hostname and referrer_domain != ''`;
|
||||
}
|
||||
|
||||
if (search) {
|
||||
searchQuery = `and positionCaseInsensitive(${column}, {search:String}) > 0`;
|
||||
}
|
||||
|
||||
if (search) {
|
||||
if (decodeURIComponent(search).includes(',')) {
|
||||
searchQuery = `AND (${decodeURIComponent(search)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -64,6 +65,7 @@ async function relationalQuery(websiteId: string, currency: string, filters: Que
|
||||
and upper(currency) = {{currency}}
|
||||
) rev on rev.session_id = website_event.session_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
${searchQuery}
|
||||
@@ -126,6 +128,7 @@ async function clickhouseQuery(websiteId: string, currency: string, filters: Que
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
${searchQuery}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -37,6 +38,7 @@ async function relationalQuery(websiteId: string, sessionId: string, filters: Qu
|
||||
from website_event
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and session_id = {{sessionId::uuid}}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
and created_at between {{startDate}} and {{endDate}}
|
||||
order by created_at desc
|
||||
limit 500
|
||||
@@ -70,6 +72,7 @@ async function clickhouseQuery(websiteId: string, sessionId: string, filters: Qu
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and session_id = {sessionId:UUID}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
order by created_at desc
|
||||
limit 500
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { DATA_TYPE } from '@/lib/constants';
|
||||
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { PropertyFilter, PropertyLeaderboardRow, QueryFilters } from '@/lib/types';
|
||||
@@ -22,7 +22,7 @@ async function relationalQuery(
|
||||
propertyFilters: PropertyFilter[] = [],
|
||||
) {
|
||||
const { timezone = 'utc' } = filters;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery, getTimestampDiffSQL } = prisma;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery } = prisma;
|
||||
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
@@ -39,6 +39,7 @@ async function relationalQuery(
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
@@ -57,6 +58,7 @@ async function relationalQuery(
|
||||
and filtered_sessions.website_id = website_event.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
group by website_event.session_id, website_event.visit_id
|
||||
),
|
||||
session_stats as (
|
||||
@@ -65,8 +67,7 @@ async function relationalQuery(
|
||||
count(*) as visits,
|
||||
sum(activity) as activity,
|
||||
sum(views) as views,
|
||||
sum(events) as events,
|
||||
sum(${getTimestampDiffSQL('min_time', 'max_time')}) as totaltime
|
||||
sum(events) as events
|
||||
from session_rollup
|
||||
group by session_id
|
||||
),
|
||||
@@ -89,8 +90,7 @@ async function relationalQuery(
|
||||
count(distinct property_values.session_id) as sessions,
|
||||
coalesce(sum(session_stats.visits), 0) as visits,
|
||||
coalesce(sum(session_stats.views), 0) as views,
|
||||
coalesce(sum(session_stats.events), 0) as events,
|
||||
coalesce(sum(session_stats.totaltime), 0) as totaltime
|
||||
coalesce(sum(session_stats.events), 0) as events
|
||||
from property_values
|
||||
join session_stats on session_stats.session_id = property_values.session_id
|
||||
group by property_values.value
|
||||
@@ -121,6 +121,7 @@ async function clickhouseQuery(
|
||||
${cohortQuery}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
@@ -137,6 +138,7 @@ async function clickhouseQuery(
|
||||
join filtered_sessions on filtered_sessions.session_id = website_event.session_id
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
group by website_event.session_id, website_event.visit_id
|
||||
),
|
||||
session_stats as (
|
||||
@@ -145,8 +147,7 @@ async function clickhouseQuery(
|
||||
count() as visits,
|
||||
sum(activity) as activity,
|
||||
sum(views) as views,
|
||||
sum(events) as events,
|
||||
sum(max_time - min_time) as totaltime
|
||||
sum(events) as events
|
||||
from session_rollup
|
||||
group by session_id
|
||||
),
|
||||
@@ -167,8 +168,7 @@ async function clickhouseQuery(
|
||||
uniq(property_values.session_id) as sessions,
|
||||
ifNull(sum(session_stats.visits), 0) as visits,
|
||||
ifNull(sum(session_stats.views), 0) as views,
|
||||
ifNull(sum(session_stats.events), 0) as events,
|
||||
ifNull(sum(session_stats.totaltime), 0) as totaltime
|
||||
ifNull(sum(session_stats.events), 0) as events
|
||||
from property_values
|
||||
join session_stats on session_stats.session_id = property_values.session_id
|
||||
group by property_values.value
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { DATA_TYPE } from '@/lib/constants';
|
||||
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { EventDataSeriesPoint, PropertyFilter, QueryFilters } from '@/lib/types';
|
||||
@@ -50,6 +50,7 @@ async function relationalQuery(
|
||||
cross join lateral jsonb_array_elements_text(coalesce(session_data.string_value, '[]')::jsonb) as array_item(value)
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {{propertyName}}
|
||||
and session_data.data_type = ${DATA_TYPE.array}
|
||||
${filterQuery}
|
||||
@@ -86,6 +87,7 @@ async function clickhouseQuery(
|
||||
and session_data.website_id = {websiteId:UUID}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {propertyName:String}
|
||||
and session_data.data_type = ${DATA_TYPE.array}
|
||||
${filterQuery}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { DATA_TYPE } from '@/lib/constants';
|
||||
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { EventDataDateSeriesPoint, PropertyFilter, QueryFilters } from '@/lib/types';
|
||||
@@ -48,6 +48,7 @@ async function relationalQuery(
|
||||
and session_data.website_id = website_event.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {{propertyName}}
|
||||
and session_data.data_type = ${DATA_TYPE.date}
|
||||
${filterQuery}
|
||||
@@ -83,6 +84,7 @@ async function clickhouseQuery(
|
||||
and session_data.website_id = {websiteId:UUID}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {propertyName:String}
|
||||
and session_data.data_type = ${DATA_TYPE.date}
|
||||
${filterQuery}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { PropertyFilter, QueryFilters } from '@/lib/types';
|
||||
@@ -51,6 +52,7 @@ async function relationalQuery(
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
)
|
||||
@@ -98,6 +100,7 @@ async function clickhouseQuery(
|
||||
${cohortQuery}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { EventDataNumericStats, PropertyFilter, QueryFilters } from '@/lib/types';
|
||||
@@ -43,6 +44,7 @@ async function relationalQuery(
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
)
|
||||
@@ -84,6 +86,7 @@ async function clickhouseQuery(
|
||||
${cohortQuery}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { DEFAULT_PAGE_SIZE } from '@/lib/constants';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { PageResult, PropertyFilter, QueryFilters, SessionDataPivotRow } from '@/lib/types';
|
||||
@@ -25,12 +25,9 @@ async function relationalQuery(
|
||||
propertyName: string,
|
||||
filters: QueryFilters,
|
||||
propertyFilters: PropertyFilter[] = [],
|
||||
): Promise<PageResult<SessionDataPivotRow[]>> {
|
||||
) {
|
||||
const { timezone = 'utc' } = filters;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
|
||||
const { page = 1, pageSize } = filters;
|
||||
const size = +pageSize || DEFAULT_PAGE_SIZE;
|
||||
const offset = +size * (+page - 1);
|
||||
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
|
||||
|
||||
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
@@ -43,47 +40,7 @@ async function relationalQuery(
|
||||
timezone,
|
||||
);
|
||||
|
||||
const countResult = (await rawQuery(
|
||||
`
|
||||
with filtered_sessions as (
|
||||
select distinct website_event.session_id
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
latest_session_properties as (
|
||||
select
|
||||
ranked.session_id,
|
||||
ranked.data_key
|
||||
from (
|
||||
select
|
||||
session_data.session_id,
|
||||
session_data.data_key,
|
||||
row_number() over (
|
||||
partition by session_data.session_id, session_data.data_key
|
||||
order by session_data.created_at desc, session_data.session_data_id desc
|
||||
) as row_num
|
||||
from session_data
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {{websiteId::uuid}}
|
||||
) ranked
|
||||
where ranked.row_num = 1
|
||||
)
|
||||
select count(*) as num
|
||||
from latest_session_properties
|
||||
where latest_session_properties.data_key = {{propertyName}}
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
)) as { num: number }[];
|
||||
|
||||
const count = countResult[0].num;
|
||||
|
||||
const rows = (await rawQuery(
|
||||
return pagedRawQuery(
|
||||
`
|
||||
with filtered_sessions as (
|
||||
select distinct website_event.session_id
|
||||
@@ -92,6 +49,7 @@ async function relationalQuery(
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
@@ -132,8 +90,6 @@ async function relationalQuery(
|
||||
latest_session_properties.created_at as sort_created_at
|
||||
from latest_session_properties
|
||||
where latest_session_properties.data_key = {{propertyName}}
|
||||
order by latest_session_properties.created_at desc
|
||||
limit ${size} offset ${offset}
|
||||
)
|
||||
select
|
||||
latest_session_properties.session_id as "sessionId",
|
||||
@@ -158,10 +114,9 @@ async function relationalQuery(
|
||||
order by paged_sessions.sort_created_at desc
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
filters,
|
||||
FUNCTION_NAME,
|
||||
)) as SessionDataPivotRow[];
|
||||
|
||||
return { data: rows, count, page: +page, pageSize: size };
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
@@ -169,12 +124,9 @@ async function clickhouseQuery(
|
||||
propertyName: string,
|
||||
filters: QueryFilters,
|
||||
propertyFilters: PropertyFilter[] = [],
|
||||
): Promise<PageResult<SessionDataPivotRow[]>> {
|
||||
) {
|
||||
const { timezone = 'UTC' } = filters;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
|
||||
const { page = 1, pageSize } = filters;
|
||||
const size = +pageSize || DEFAULT_PAGE_SIZE;
|
||||
const offset = +size * (+page - 1);
|
||||
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
|
||||
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId, timezone });
|
||||
const { sql: pfSQL, params: pfParams } = getPropertyFilterQuery(
|
||||
@@ -183,38 +135,7 @@ async function clickhouseQuery(
|
||||
timezone,
|
||||
);
|
||||
|
||||
const countResult = (await rawQuery(
|
||||
`
|
||||
with filtered_sessions as (
|
||||
select distinct website_event.session_id
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
latest_session_properties as (
|
||||
select
|
||||
session_data.session_id as session_id,
|
||||
session_data.data_key as data_key
|
||||
from session_data final
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {websiteId:UUID}
|
||||
group by
|
||||
session_data.session_id,
|
||||
session_data.data_key
|
||||
)
|
||||
select count() as num
|
||||
from latest_session_properties
|
||||
where latest_session_properties.data_key = {propertyName:String}
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
)) as { num: number }[];
|
||||
const count = countResult[0].num;
|
||||
|
||||
const data = (await rawQuery(
|
||||
return pagedRawQuery(
|
||||
`
|
||||
with filtered_sessions as (
|
||||
select distinct website_event.session_id
|
||||
@@ -222,6 +143,7 @@ async function clickhouseQuery(
|
||||
${cohortQuery}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
@@ -249,8 +171,6 @@ async function clickhouseQuery(
|
||||
latest_session_properties.created_at as sort_created_at
|
||||
from latest_session_properties
|
||||
where latest_session_properties.data_key = {propertyName:String}
|
||||
order by latest_session_properties.created_at desc
|
||||
limit ${size} offset ${offset}
|
||||
)
|
||||
select
|
||||
latest_session_properties.session_id as sessionId,
|
||||
@@ -274,8 +194,7 @@ async function clickhouseQuery(
|
||||
order by paged_sessions.sort_created_at desc
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
filters,
|
||||
FUNCTION_NAME,
|
||||
)) as SessionDataPivotRow[];
|
||||
|
||||
return { data, count, page: +page, pageSize: size };
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { PropertyFilter, QueryFilters } from '@/lib/types';
|
||||
@@ -10,7 +11,7 @@ export async function getSessionDataProperties(
|
||||
websiteId: string,
|
||||
filters: QueryFilters,
|
||||
propertyFilters?: PropertyFilter[],
|
||||
selectedPropertyName?: string,
|
||||
propertyName?: string,
|
||||
]
|
||||
) {
|
||||
return runQuery({
|
||||
@@ -23,7 +24,7 @@ async function relationalQuery(
|
||||
websiteId: string,
|
||||
filters: QueryFilters,
|
||||
propertyFilters: PropertyFilter[] = [],
|
||||
selectedPropertyName?: string,
|
||||
propertyName?: string,
|
||||
) {
|
||||
const { timezone = 'utc' } = filters;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery } = prisma;
|
||||
@@ -47,6 +48,7 @@ async function relationalQuery(
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
@@ -56,7 +58,7 @@ async function relationalQuery(
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
and filtered_sessions.website_id = session_data.website_id
|
||||
${selectedPropertyName ? 'where session_data.data_key = {{selectedPropertyName}}' : ''}
|
||||
${propertyName ? 'where session_data.data_key = {{propertyName}}' : ''}
|
||||
)
|
||||
select
|
||||
data_key as "propertyName",
|
||||
@@ -70,7 +72,7 @@ async function relationalQuery(
|
||||
order by 3 desc, 1 asc
|
||||
limit 500
|
||||
`,
|
||||
{ ...queryParams, websiteId, selectedPropertyName, ...pfParams },
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
}
|
||||
@@ -79,7 +81,7 @@ async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
filters: QueryFilters,
|
||||
propertyFilters: PropertyFilter[] = [],
|
||||
selectedPropertyName?: string,
|
||||
propertyName?: string,
|
||||
): Promise<{ propertyName: string; dataType: number; total: number }[]> {
|
||||
const { timezone = 'UTC' } = filters;
|
||||
const { rawQuery, parseFilters, getPropertyFilterQuery } = clickhouse;
|
||||
@@ -98,6 +100,7 @@ async function clickhouseQuery(
|
||||
${cohortQuery}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
@@ -107,7 +110,7 @@ async function clickhouseQuery(
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
and filtered_sessions.website_id = session_data.website_id
|
||||
${selectedPropertyName ? 'where session_data.data_key = {selectedPropertyName:String}' : ''}
|
||||
${propertyName ? 'where session_data.data_key = {propertyName:String}' : ''}
|
||||
)
|
||||
select
|
||||
data_key as propertyName,
|
||||
@@ -123,7 +126,7 @@ async function clickhouseQuery(
|
||||
order by 3 desc, 1 asc
|
||||
limit 500
|
||||
`,
|
||||
{ ...queryParams, websiteId, selectedPropertyName, ...pfParams },
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
FUNCTION_NAME,
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { DATA_TYPE } from '@/lib/constants';
|
||||
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { EventDataSeriesPoint, PropertyFilter, QueryFilters } from '@/lib/types';
|
||||
@@ -49,6 +49,7 @@ async function relationalQuery(
|
||||
and session_data.website_id = website_event.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {{propertyName}}
|
||||
and session_data.data_type in (${DATA_TYPE.string}, ${DATA_TYPE.boolean})
|
||||
${filterQuery}
|
||||
@@ -85,6 +86,7 @@ async function clickhouseQuery(
|
||||
and session_data.website_id = {websiteId:UUID}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {propertyName:String}
|
||||
and session_data.data_type in (${DATA_TYPE.string}, ${DATA_TYPE.boolean})
|
||||
${filterQuery}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { DATA_TYPE } from '@/lib/constants';
|
||||
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -41,6 +41,7 @@ async function relationalQuery(
|
||||
cross join lateral jsonb_array_elements_text(coalesce(session_data.string_value, '[]')::jsonb) as array_item(value)
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {{propertyName}}
|
||||
and session_data.data_type = ${DATA_TYPE.array}
|
||||
${filterQuery}
|
||||
@@ -70,6 +71,7 @@ async function relationalQuery(
|
||||
and session_data.website_id = website_event.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {{propertyName}}
|
||||
${dataType ? `and session_data.data_type = ${dataType}` : ''}
|
||||
${filterQuery}
|
||||
@@ -103,6 +105,7 @@ async function clickhouseQuery(
|
||||
and session_data.website_id = {websiteId:UUID}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {propertyName:String}
|
||||
and session_data.data_type = ${DATA_TYPE.array}
|
||||
${filterQuery}
|
||||
@@ -129,6 +132,7 @@ async function clickhouseQuery(
|
||||
and session_data.website_id = {websiteId:UUID}
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
and session_data.data_key = {propertyName:String}
|
||||
${dataType ? `and session_data.data_type = ${dataType}` : ''}
|
||||
${filterQuery}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
@@ -54,6 +55,7 @@ async function relationalQuery(websiteId: string, sessionId: string) {
|
||||
join website_event on website_event.session_id = session.session_id
|
||||
where session.website_id = {{websiteId::uuid}}
|
||||
and session.session_id = {{sessionId::uuid}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
group by session.session_id, session.distinct_id, visit_id, session.website_id, session.browser, session.os, session.device, session.screen, session.language, session.country, session.region, session.city) t
|
||||
group by id, distinct_id, website_id, browser, os, device, screen, language, country, region, city;
|
||||
`,
|
||||
@@ -104,6 +106,7 @@ async function clickhouseQuery(websiteId: string, sessionId: string) {
|
||||
from website_event_stats_hourly
|
||||
where website_id = {websiteId:UUID}
|
||||
and session_id = {sessionId:UUID}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
group by session_id, distinct_id, visit_id, website_id, browser, os, device, screen, language, country, region, city) t
|
||||
group by id, websiteId, distinctId, browser, os, device, screen, language, country, region, city;
|
||||
`,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_COLUMNS } from '@/lib/constants';
|
||||
import { EVENT_COLUMNS, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -36,7 +36,7 @@ async function relationalQuery(
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
count(*) as "pageviews",
|
||||
sum(case when website_event.event_type = 1 then 1 else 0 end) as "pageviews",
|
||||
count(distinct website_event.session_id) as "visitors",
|
||||
count(distinct website_event.visit_id) as "visits",
|
||||
count(distinct session.country) as "countries",
|
||||
@@ -47,6 +47,7 @@ async function relationalQuery(
|
||||
and website_event.website_id = session.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
`,
|
||||
queryParams,
|
||||
@@ -75,6 +76,7 @@ async function clickhouseQuery(
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
`;
|
||||
} else {
|
||||
@@ -89,6 +91,7 @@ async function clickhouseQuery(
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
${filterQuery}
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_COLUMNS } from '@/lib/constants';
|
||||
import { EVENT_COLUMNS, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -55,6 +55,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
join session on session.session_id = website_event.session_id
|
||||
and session.website_id = website_event.website_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.event_type != ${EVENT_TYPE.performance}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
${searchQuery}
|
||||
@@ -118,6 +119,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
${searchQuery}
|
||||
@@ -147,6 +149,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||
from website_event_stats_hourly as website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type != ${EVENT_TYPE.performance}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
${searchQuery}
|
||||
|
||||
Reference in new issue
Block a user