mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 15:47:13 +01:00
Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
commit
1b4c5c80d3
91 changed files with 1454 additions and 373 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import { Prisma, Report } from '@prisma/client';
|
||||
import { REPORT_FILTER_TYPES } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import { FilterResult, ReportSearchFilter, ReportSearchFilterType, SearchFilter } from 'lib/types';
|
||||
|
||||
export async function createReport(data: Prisma.ReportUncheckedCreateInput): Promise<Report> {
|
||||
return prisma.client.report.create({ data });
|
||||
|
|
@ -13,22 +15,6 @@ export async function getReportById(reportId: string): Promise<Report> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function getUserReports(userId: string): Promise<Report[]> {
|
||||
return prisma.client.report.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getWebsiteReports(websiteId: string): Promise<Report[]> {
|
||||
return prisma.client.report.findMany({
|
||||
where: {
|
||||
websiteId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateReport(
|
||||
reportId: string,
|
||||
data: Prisma.ReportUpdateInput,
|
||||
|
|
@ -39,3 +25,103 @@ export async function updateReport(
|
|||
export async function deleteReport(reportId: string): Promise<Report> {
|
||||
return prisma.client.report.delete({ where: { id: reportId } });
|
||||
}
|
||||
|
||||
export async function getReports(
|
||||
ReportSearchFilter: ReportSearchFilter,
|
||||
): Promise<FilterResult<Report[]>> {
|
||||
const { userId, websiteId, filter, filterType = REPORT_FILTER_TYPES.all } = ReportSearchFilter;
|
||||
const where: Prisma.ReportWhereInput = {
|
||||
...(userId && { userId: userId }),
|
||||
...(websiteId && { websiteId: websiteId }),
|
||||
...(filter && {
|
||||
AND: {
|
||||
OR: [
|
||||
{
|
||||
...((filterType === REPORT_FILTER_TYPES.all ||
|
||||
filterType === REPORT_FILTER_TYPES.name) && {
|
||||
name: {
|
||||
startsWith: filter,
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
...((filterType === REPORT_FILTER_TYPES.all ||
|
||||
filterType === REPORT_FILTER_TYPES.description) && {
|
||||
description: {
|
||||
startsWith: filter,
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
...((filterType === REPORT_FILTER_TYPES.all ||
|
||||
filterType === REPORT_FILTER_TYPES.type) && {
|
||||
type: {
|
||||
startsWith: filter,
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
...((filterType === REPORT_FILTER_TYPES.all ||
|
||||
filterType === REPORT_FILTER_TYPES['user:username']) && {
|
||||
user: {
|
||||
username: {
|
||||
startsWith: filter,
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
...((filterType === REPORT_FILTER_TYPES.all ||
|
||||
filterType === REPORT_FILTER_TYPES['website:name']) && {
|
||||
website: {
|
||||
name: {
|
||||
startsWith: filter,
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
...((filterType === REPORT_FILTER_TYPES.all ||
|
||||
filterType === REPORT_FILTER_TYPES['website:domain']) && {
|
||||
website: {
|
||||
domain: {
|
||||
startsWith: filter,
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
const [pageFilters, getParameters] = prisma.getPageFilters(ReportSearchFilter);
|
||||
|
||||
const reports = await prisma.client.report.findMany({
|
||||
where,
|
||||
...pageFilters,
|
||||
});
|
||||
const count = await prisma.client.report.count({
|
||||
where,
|
||||
});
|
||||
|
||||
return {
|
||||
data: reports,
|
||||
count,
|
||||
...getParameters,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getReportsByUserId(
|
||||
userId: string,
|
||||
filter: SearchFilter<ReportSearchFilterType>,
|
||||
): Promise<FilterResult<Report[]>> {
|
||||
return getReports({ userId, ...filter });
|
||||
}
|
||||
|
||||
export async function getReportsByWebsiteId(
|
||||
websiteId: string,
|
||||
filter: SearchFilter<ReportSearchFilterType>,
|
||||
): Promise<FilterResult<Report[]>> {
|
||||
return getReports({ websiteId, ...filter });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { Prisma, Team } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { ROLES, TEAM_FILTER_TYPES } from 'lib/constants';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { FilterResult, TeamSearchFilter, TeamSearchFilterType, SearchFilter } from 'lib/types';
|
||||
|
||||
export interface GetTeamOptions {
|
||||
includeTeamUser?: boolean;
|
||||
|
|
@ -26,12 +27,6 @@ export function getTeamByAccessCode(accessCode: string, options: GetTeamOptions
|
|||
return getTeam({ accessCode }, options);
|
||||
}
|
||||
|
||||
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
||||
return prisma.client.team.findMany({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function createTeam(data: Prisma.TeamCreateInput, userId: string): Promise<Team> {
|
||||
const { id } = data;
|
||||
|
||||
|
|
@ -85,3 +80,82 @@ export async function deleteTeam(
|
|||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
export async function getTeams(
|
||||
TeamSearchFilter: TeamSearchFilter,
|
||||
options?: { include?: Prisma.TeamInclude },
|
||||
): Promise<FilterResult<Team[]>> {
|
||||
const { userId, filter, filterType = TEAM_FILTER_TYPES.all } = TeamSearchFilter;
|
||||
const where: Prisma.TeamWhereInput = {
|
||||
...(userId && {
|
||||
teamUser: {
|
||||
some: { userId },
|
||||
},
|
||||
}),
|
||||
...(filter && {
|
||||
AND: {
|
||||
OR: [
|
||||
{
|
||||
...((filterType === TEAM_FILTER_TYPES.all || filterType === TEAM_FILTER_TYPES.name) && {
|
||||
name: { startsWith: filter },
|
||||
}),
|
||||
},
|
||||
{
|
||||
...((filterType === TEAM_FILTER_TYPES.all ||
|
||||
filterType === TEAM_FILTER_TYPES['user:username']) && {
|
||||
teamUser: {
|
||||
every: {
|
||||
role: ROLES.teamOwner,
|
||||
user: {
|
||||
username: {
|
||||
startsWith: filter,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
const [pageFilters, getParameters] = prisma.getPageFilters({
|
||||
orderBy: 'name',
|
||||
...TeamSearchFilter,
|
||||
});
|
||||
|
||||
const teams = await prisma.client.team.findMany({
|
||||
where: {
|
||||
...where,
|
||||
},
|
||||
...pageFilters,
|
||||
...(options?.include && { include: options?.include }),
|
||||
});
|
||||
const count = await prisma.client.team.count({ where });
|
||||
|
||||
return { data: teams, count, ...getParameters };
|
||||
}
|
||||
|
||||
export async function getTeamsByUserId(
|
||||
userId: string,
|
||||
filter?: SearchFilter<TeamSearchFilterType>,
|
||||
): Promise<FilterResult<Team[]>> {
|
||||
return getTeams(
|
||||
{ userId, ...filter },
|
||||
{
|
||||
include: {
|
||||
teamUser: {
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { Prisma, Team, TeamUser } from '@prisma/client';
|
||||
import { getRandomChars } from 'next-basics';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import cache from 'lib/cache';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { ROLES, USER_FILTER_TYPES } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import { Website, User, Role } from 'lib/types';
|
||||
import { FilterResult, Role, User, UserSearchFilter } from 'lib/types';
|
||||
import { getRandomChars } from 'next-basics';
|
||||
|
||||
export interface GetUserOptions {
|
||||
includePassword?: boolean;
|
||||
|
|
@ -36,125 +36,59 @@ export async function getUserByUsername(username: string, options: GetUserOption
|
|||
return getUser({ username }, options);
|
||||
}
|
||||
|
||||
export async function getUsers(): Promise<User[]> {
|
||||
return prisma.client.user.findMany({
|
||||
take: 100,
|
||||
where: {
|
||||
deletedAt: null,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
username: 'asc',
|
||||
},
|
||||
],
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
role: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserTeams(userId: string): Promise<
|
||||
(Team & {
|
||||
teamUser: (TeamUser & {
|
||||
user: { id: string; username: string };
|
||||
})[];
|
||||
})[]
|
||||
> {
|
||||
return prisma.client.team.findMany({
|
||||
where: {
|
||||
export async function getUsers(
|
||||
UserSearchFilter: UserSearchFilter = {},
|
||||
options?: { include?: Prisma.UserInclude },
|
||||
): Promise<FilterResult<User[]>> {
|
||||
const { teamId, filter, filterType = USER_FILTER_TYPES.all } = UserSearchFilter;
|
||||
const where: Prisma.UserWhereInput = {
|
||||
...(teamId && {
|
||||
teamUser: {
|
||||
some: {
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
teamUser: {
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
},
|
||||
}),
|
||||
...(filter && {
|
||||
AND: {
|
||||
OR: [
|
||||
{
|
||||
...((filterType === USER_FILTER_TYPES.all ||
|
||||
filterType === USER_FILTER_TYPES.username) && {
|
||||
username: {
|
||||
startsWith: filter,
|
||||
},
|
||||
}),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}),
|
||||
};
|
||||
const [pageFilters, getParameters] = prisma.getPageFilters({
|
||||
orderBy: 'username',
|
||||
...UserSearchFilter,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserWebsites(
|
||||
userId: string,
|
||||
options?: { includeTeams: boolean },
|
||||
): Promise<Website[]> {
|
||||
const { rawQuery } = prisma;
|
||||
|
||||
if (options?.includeTeams) {
|
||||
const websites = await rawQuery(
|
||||
`
|
||||
select
|
||||
website_id as "id",
|
||||
name,
|
||||
domain,
|
||||
share_id as "shareId",
|
||||
reset_at as "resetAt",
|
||||
user_id as "userId",
|
||||
created_at as "createdAt",
|
||||
updated_at as "updatedAt",
|
||||
deleted_at as "deletedAt",
|
||||
null as "teamId",
|
||||
null as "teamName"
|
||||
from website
|
||||
where user_id = {{userId::uuid}}
|
||||
and deleted_at is null
|
||||
union
|
||||
select
|
||||
w.website_id as "id",
|
||||
w.name,
|
||||
w.domain,
|
||||
w.share_id as "shareId",
|
||||
w.reset_at as "resetAt",
|
||||
w.user_id as "userId",
|
||||
w.created_at as "createdAt",
|
||||
w.updated_at as "updatedAt",
|
||||
w.deleted_at as "deletedAt",
|
||||
t.team_id as "teamId",
|
||||
t.name as "teamName"
|
||||
from website w
|
||||
inner join team_website tw
|
||||
on tw.website_id = w.website_id
|
||||
inner join team t
|
||||
on t.team_id = tw.team_id
|
||||
inner join team_user tu
|
||||
on tu.team_id = tw.team_id
|
||||
where tu.user_id = {{userId::uuid}}
|
||||
and w.deleted_at is null
|
||||
`,
|
||||
{ userId },
|
||||
);
|
||||
|
||||
return websites.reduce((arr, item) => {
|
||||
if (!arr.find(({ id }) => id === item.id)) {
|
||||
return arr.concat(item);
|
||||
}
|
||||
return arr;
|
||||
}, []);
|
||||
}
|
||||
|
||||
return prisma.client.website.findMany({
|
||||
const users = await prisma.client.user.findMany({
|
||||
where: {
|
||||
userId,
|
||||
...where,
|
||||
deletedAt: null,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
...pageFilters,
|
||||
...(options?.include && { include: options.include }),
|
||||
});
|
||||
const count = await prisma.client.user.count({
|
||||
where: {
|
||||
...where,
|
||||
deletedAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
return { data: users as any, count, ...getParameters };
|
||||
}
|
||||
|
||||
export async function getUsersByTeamId(teamId: string, filter?: UserSearchFilter) {
|
||||
return getUsers({ teamId, ...filter });
|
||||
}
|
||||
|
||||
export async function createUser(data: {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { Prisma, Website } from '@prisma/client';
|
||||
import cache from 'lib/cache';
|
||||
import { ROLES, WEBSITE_FILTER_TYPES } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import { FilterResult, WebsiteSearchFilter } from 'lib/types';
|
||||
|
||||
async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
|
||||
return prisma.client.website.findUnique({
|
||||
|
|
@ -16,11 +18,199 @@ export async function getWebsiteByShareId(shareId: string) {
|
|||
return getWebsite({ shareId });
|
||||
}
|
||||
|
||||
export async function getWebsites(): Promise<Website[]> {
|
||||
return prisma.client.website.findMany({
|
||||
orderBy: {
|
||||
name: 'asc',
|
||||
export async function getWebsites(
|
||||
WebsiteSearchFilter: WebsiteSearchFilter,
|
||||
options?: { include?: Prisma.WebsiteInclude },
|
||||
): Promise<FilterResult<Website[]>> {
|
||||
const {
|
||||
userId,
|
||||
teamId,
|
||||
includeTeams,
|
||||
filter,
|
||||
filterType = WEBSITE_FILTER_TYPES.all,
|
||||
} = WebsiteSearchFilter;
|
||||
|
||||
const filterQuery = {
|
||||
AND: {
|
||||
OR: [
|
||||
{
|
||||
...((filterType === WEBSITE_FILTER_TYPES.all ||
|
||||
filterType === WEBSITE_FILTER_TYPES.name) && {
|
||||
name: { startsWith: filter },
|
||||
}),
|
||||
},
|
||||
{
|
||||
...((filterType === WEBSITE_FILTER_TYPES.all ||
|
||||
filterType === WEBSITE_FILTER_TYPES.domain) && {
|
||||
domain: { startsWith: filter },
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const where: Prisma.WebsiteWhereInput = {
|
||||
...(teamId && {
|
||||
teamWebsite: {
|
||||
some: {
|
||||
teamId,
|
||||
},
|
||||
},
|
||||
}),
|
||||
AND: {
|
||||
OR: [
|
||||
{
|
||||
...(userId && {
|
||||
userId,
|
||||
}),
|
||||
},
|
||||
{
|
||||
...(includeTeams && {
|
||||
teamWebsite: {
|
||||
some: {
|
||||
team: {
|
||||
teamUser: {
|
||||
some: {
|
||||
userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
...(filter && filterQuery),
|
||||
};
|
||||
|
||||
const [pageFilters, getParameters] = prisma.getPageFilters({
|
||||
orderBy: 'name',
|
||||
...WebsiteSearchFilter,
|
||||
});
|
||||
|
||||
const websites = await prisma.client.website.findMany({
|
||||
where: {
|
||||
...where,
|
||||
deletedAt: null,
|
||||
},
|
||||
...pageFilters,
|
||||
...(options?.include && { include: options.include }),
|
||||
});
|
||||
const count = await prisma.client.website.count({ where });
|
||||
|
||||
return { data: websites, count, ...getParameters };
|
||||
}
|
||||
|
||||
export async function getWebsitesByUserId(
|
||||
userId: string,
|
||||
filter?: WebsiteSearchFilter,
|
||||
): Promise<FilterResult<Website[]>> {
|
||||
return getWebsites({ userId, ...filter });
|
||||
}
|
||||
|
||||
export async function getWebsitesByTeamId(
|
||||
teamId: string,
|
||||
filter?: WebsiteSearchFilter,
|
||||
): Promise<FilterResult<Website[]>> {
|
||||
return getWebsites(
|
||||
{
|
||||
teamId,
|
||||
...filter,
|
||||
includeTeams: true,
|
||||
},
|
||||
{
|
||||
include: {
|
||||
teamWebsite: {
|
||||
include: {
|
||||
team: {
|
||||
include: {
|
||||
teamUser: {
|
||||
where: { role: ROLES.teamOwner },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export async function getUserWebsites(
|
||||
userId: string,
|
||||
options?: { includeTeams: boolean },
|
||||
): Promise<Website[]> {
|
||||
const { rawQuery } = prisma;
|
||||
|
||||
if (options?.includeTeams) {
|
||||
const websites = await rawQuery(
|
||||
`
|
||||
select
|
||||
website_id as "id",
|
||||
name,
|
||||
domain,
|
||||
share_id as "shareId",
|
||||
reset_at as "resetAt",
|
||||
user_id as "userId",
|
||||
created_at as "createdAt",
|
||||
updated_at as "updatedAt",
|
||||
deleted_at as "deletedAt",
|
||||
null as "teamId",
|
||||
null as "teamName"
|
||||
from website
|
||||
where user_id = {{userId::uuid}}
|
||||
and deleted_at is null
|
||||
union
|
||||
select
|
||||
w.website_id as "id",
|
||||
w.name,
|
||||
w.domain,
|
||||
w.share_id as "shareId",
|
||||
w.reset_at as "resetAt",
|
||||
w.user_id as "userId",
|
||||
w.created_at as "createdAt",
|
||||
w.updated_at as "updatedAt",
|
||||
w.deleted_at as "deletedAt",
|
||||
t.team_id as "teamId",
|
||||
t.name as "teamName"
|
||||
from website w
|
||||
inner join team_website tw
|
||||
on tw.website_id = w.website_id
|
||||
inner join team t
|
||||
on t.team_id = tw.team_id
|
||||
inner join team_user tu
|
||||
on tu.team_id = tw.team_id
|
||||
where tu.user_id = {{userId::uuid}}
|
||||
and w.deleted_at is null
|
||||
`,
|
||||
{ userId },
|
||||
);
|
||||
|
||||
return websites.reduce((arr, item) => {
|
||||
if (!arr.find(({ id }) => id === item.id)) {
|
||||
return arr.concat(item);
|
||||
}
|
||||
return arr;
|
||||
}, []);
|
||||
}
|
||||
|
||||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
deletedAt: null,
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
name: 'asc',
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,10 +21,10 @@ async function relationalQuery(websiteId: string, filters: QueryFilters & { fiel
|
|||
return rawQuery(
|
||||
`
|
||||
select
|
||||
event_key as fieldName,
|
||||
data_type as dataType,
|
||||
string_value as fieldValue,
|
||||
count(*) as total
|
||||
event_key as "fieldName",
|
||||
data_type as "dataType",
|
||||
string_value as "fieldValue",
|
||||
count(*) as "total"
|
||||
from event_data
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and created_at between {{startDate}} and {{endDate}}
|
||||
|
|
|
|||
166
queries/analytics/reports/getRetention.ts
Normal file
166
queries/analytics/reports/getRetention.ts
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function getRetention(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
dateRange: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
},
|
||||
]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
dateRange: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
},
|
||||
): Promise<
|
||||
{
|
||||
date: Date;
|
||||
day: number;
|
||||
visitors: number;
|
||||
returnVisitors: number;
|
||||
percentage: number;
|
||||
}[]
|
||||
> {
|
||||
const { startDate, endDate } = dateRange;
|
||||
const { rawQuery } = prisma;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
WITH cohort_items AS (
|
||||
select session_id,
|
||||
date_trunc('day', created_at)::date as cohort_date
|
||||
from session
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and created_at between {{startDate}} and {{endDate}}
|
||||
),
|
||||
user_activities AS (
|
||||
select distinct
|
||||
w.session_id,
|
||||
(date_trunc('day', w.created_at)::date - c.cohort_date::date) as day_number
|
||||
from website_event w
|
||||
join cohort_items c
|
||||
on w.session_id = c.session_id
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and created_at between {{startDate}} and {{endDate}}
|
||||
),
|
||||
cohort_size as (
|
||||
select cohort_date,
|
||||
count(*) as visitors
|
||||
from cohort_items
|
||||
group by 1
|
||||
order by 1
|
||||
),
|
||||
cohort_date as (
|
||||
select
|
||||
c.cohort_date,
|
||||
a.day_number,
|
||||
count(*) as visitors
|
||||
from user_activities a
|
||||
join cohort_items c
|
||||
on a.session_id = c.session_id
|
||||
where a.day_number IN (0,1,2,3,4,5,6,7,14,21,30)
|
||||
group by 1, 2
|
||||
)
|
||||
select
|
||||
c.cohort_date as date,
|
||||
c.day_number as day,
|
||||
s.visitors,
|
||||
c.visitors as "returnVisitors",
|
||||
c.visitors::float * 100 / s.visitors as percentage
|
||||
from cohort_date c
|
||||
join cohort_size s
|
||||
on c.cohort_date = s.cohort_date
|
||||
order by 1, 2`,
|
||||
{
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
dateRange: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
},
|
||||
): Promise<
|
||||
{
|
||||
date: Date;
|
||||
day: number;
|
||||
visitors: number;
|
||||
returnVisitors: number;
|
||||
percentage: number;
|
||||
}[]
|
||||
> {
|
||||
const { startDate, endDate } = dateRange;
|
||||
const { rawQuery } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
WITH cohort_items AS (
|
||||
select
|
||||
min(date_trunc('day', created_at)) as cohort_date,
|
||||
session_id
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
group by session_id
|
||||
),
|
||||
user_activities AS (
|
||||
select distinct
|
||||
w.session_id,
|
||||
(date_trunc('day', w.created_at) - c.cohort_date) / 86400 as day_number
|
||||
from website_event w
|
||||
join cohort_items c
|
||||
on w.session_id = c.session_id
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
),
|
||||
cohort_size as (
|
||||
select cohort_date,
|
||||
count(*) as visitors
|
||||
from cohort_items
|
||||
group by 1
|
||||
order by 1
|
||||
),
|
||||
cohort_date as (
|
||||
select
|
||||
c.cohort_date,
|
||||
a.day_number,
|
||||
count(*) as visitors
|
||||
from user_activities a
|
||||
join cohort_items c
|
||||
on a.session_id = c.session_id
|
||||
where a.day_number IN (0,1,2,3,4,5,6,7,14,21,30)
|
||||
group by 1, 2
|
||||
)
|
||||
select
|
||||
c.cohort_date as date,
|
||||
c.day_number as day,
|
||||
s.visitors as visitors,
|
||||
c.visitors returnVisitors,
|
||||
c.visitors * 100 / s.visitors as percentage
|
||||
from cohort_date c
|
||||
join cohort_size s
|
||||
on c.cohort_date = s.cohort_date
|
||||
order by 1, 2`,
|
||||
{
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ export * from './analytics/eventData/getEventDataFields';
|
|||
export * from './analytics/eventData/getEventDataUsage';
|
||||
export * from './analytics/events/saveEvent';
|
||||
export * from './analytics/reports/getFunnel';
|
||||
export * from './analytics/reports/getRetention';
|
||||
export * from './analytics/reports/getInsights';
|
||||
export * from './analytics/pageviews/getPageviewMetrics';
|
||||
export * from './analytics/pageviews/getPageviewStats';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue