Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Mike Cao 2023-08-14 22:36:39 -07:00
commit 6ef1f2e9f9
22 changed files with 411 additions and 93 deletions

View File

@ -18,6 +18,8 @@ export function NavBar() {
const links = [
{ label: formatMessage(labels.dashboard), url: '/dashboard' },
{ label: formatMessage(labels.websites), url: '/websites' },
{ label: formatMessage(labels.reports), url: '/reports' },
!cloudMode && { label: formatMessage(labels.settings), url: '/settings' },
].filter(n => n);

View File

@ -21,6 +21,8 @@ export const labels = defineMessages({
details: { id: 'label.details', defaultMessage: 'Details' },
website: { id: 'label.website', defaultMessage: 'Website' },
websites: { id: 'label.websites', defaultMessage: 'Websites' },
myWebsites: { id: 'label.my-websites', defaultMessage: 'My Websites' },
teamWebsites: { id: 'label.team-websites', defaultMessage: 'Team Websites' },
created: { id: 'label.created', defaultMessage: 'Created' },
edit: { id: 'label.edit', defaultMessage: 'Edit' },
name: { id: 'label.name', defaultMessage: 'Name' },
@ -28,6 +30,7 @@ export const labels = defineMessages({
accessCode: { id: 'label.access-code', defaultMessage: 'Access code' },
teamId: { id: 'label.team-id', defaultMessage: 'Team ID' },
team: { id: 'label.team', defaultMessage: 'Team' },
teamName: { id: 'label.team-name', defaultMessage: 'Team Name' },
regenerate: { id: 'label.regenerate', defaultMessage: 'Regenerate' },
remove: { id: 'label.remove', defaultMessage: 'Remove' },
join: { id: 'label.join', defaultMessage: 'Join' },
@ -242,16 +245,12 @@ export const messages = defineMessages({
},
noResultsFound: {
id: 'message.no-results-found',
defaultMessage: 'No results were found.',
defaultMessage: 'No results found.',
},
noWebsitesConfigured: {
id: 'message.no-websites-configured',
defaultMessage: 'You do not have any websites configured.',
},
noReportsConfigured: {
id: 'message.no-reports-configured',
defaultMessage: 'You do not have any reports configured.',
},
noTeamWebsites: {
id: 'message.no-team-websites',
defaultMessage: 'This team does not have any websites.',

View File

@ -1,13 +1,29 @@
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
import { useMessages, useReports } from 'hooks';
import Link from 'next/link';
import { Button, Icon, Icons, Text } from 'react-basics';
import { useMessages, useReports } from 'hooks';
import ReportsTable from './ReportsTable';
export function ReportsPage() {
const { formatMessage, labels } = useMessages();
const { reports, error, isLoading } = useReports();
const { formatMessage, labels, messages } = useMessages();
const {
reports,
error,
isLoading,
deleteReport,
filter,
handleFilterChange,
handlePageChange,
handlePageSizeChange,
} = useReports();
const hasData = (reports && reports?.data.length !== 0) || filter;
const handleDelete = async id => {
await deleteReport(id);
};
return (
<Page loading={isLoading} error={error}>
@ -21,7 +37,23 @@ export function ReportsPage() {
</Button>
</Link>
</PageHeader>
<ReportsTable data={reports} />
{hasData && (
<ReportsTable
data={reports}
showSearch={true}
showPaging={true}
onFilterChange={handleFilterChange}
onPageChange={handlePageChange}
onPageSizeChange={handlePageSizeChange}
onDelete={deleteReport}
filterValue={filter}
showDomain={true}
/>
)}
{!hasData && (
<EmptyPlaceholder message={formatMessage(messages.noDataAvailable)}></EmptyPlaceholder>
)}
</Page>
);
}

View File

@ -1,9 +1,10 @@
import { useState } from 'react';
import { Flexbox, Icon, Icons, Text, Button, Modal } from 'react-basics';
import ConfirmDeleteForm from 'components/common/ConfirmDeleteForm';
import LinkButton from 'components/common/LinkButton';
import SettingsTable from 'components/common/SettingsTable';
import ConfirmDeleteForm from 'components/common/ConfirmDeleteForm';
import { useMessages } from 'hooks';
import useUser from 'hooks/useUser';
import { useState } from 'react';
import { Button, Flexbox, Icon, Icons, Modal, Text } from 'react-basics';
export function ReportsTable({
data = [],
@ -12,14 +13,24 @@ export function ReportsTable({
onFilterChange,
onPageChange,
onPageSizeChange,
showDomain,
}) {
const [report, setReport] = useState(null);
const { formatMessage, labels } = useMessages();
const { user } = useUser();
const domainColumn = [
{
name: 'domain',
label: formatMessage(labels.domain),
},
];
const columns = [
{ name: 'name', label: formatMessage(labels.name) },
{ name: 'description', label: formatMessage(labels.description) },
{ name: 'type', label: formatMessage(labels.type) },
...(showDomain ? domainColumn : []),
{ name: 'action', label: ' ' },
];
@ -40,11 +51,15 @@ export function ReportsTable({
filterValue={filterValue}
>
{row => {
const { id } = row;
const { id, userId: reportOwnerId, website } = row;
if (showDomain) {
row.domain = website.domain;
}
return (
<Flexbox gap={10}>
<LinkButton href={`/reports/${id}`}>{formatMessage(labels.view)}</LinkButton>
{!showDomain || user.id === reportOwnerId || user.id === website?.userId}
<Button onClick={() => setReport(row)}>
<Icon>
<Icons.Trash />

View File

@ -10,19 +10,21 @@ import useMessages from 'hooks/useMessages';
import { ROLES } from 'lib/constants';
import useApiFilter from 'hooks/useApiFilter';
export function WebsitesList() {
export function WebsitesList({ showTeam, showHeader = true, includeTeams, onlyTeams, fetch }) {
const { formatMessage, labels, messages } = useMessages();
const { user } = useUser();
const { filter, page, pageSize, handleFilterChange, handlePageChange, handlePageSizeChange } =
useApiFilter();
const { get, useQuery } = useApi();
const { data, isLoading, error, refetch } = useQuery(
['websites', user?.id, filter, page, pageSize],
['websites', fetch, user?.id, filter, page, pageSize, includeTeams, onlyTeams],
() =>
get(`/users/${user?.id}/websites`, {
filter,
page,
pageSize,
includeTeams,
onlyTeams,
}),
{ enabled: !!user },
);
@ -54,10 +56,11 @@ export function WebsitesList() {
return (
<Page loading={isLoading} error={error}>
<PageHeader title={formatMessage(labels.websites)}>{addButton}</PageHeader>
{showHeader && <PageHeader title={formatMessage(labels.websites)}>{addButton}</PageHeader>}
{hasData && (
<WebsitesTable
data={data}
showTeam={showTeam}
onFilterChange={handleFilterChange}
onPageChange={handlePageChange}
onPageSizeChange={handlePageSizeChange}
@ -65,7 +68,7 @@ export function WebsitesList() {
/>
)}
{!hasData && (
<EmptyPlaceholder message={formatMessage(messages.noWebsitesConfigured)}>
<EmptyPlaceholder message={formatMessage(messages.noDataAvailable)}>
{addButton}
</EmptyPlaceholder>
)}

View File

@ -3,6 +3,7 @@ import { Button, Text, Icon, Icons } from 'react-basics';
import SettingsTable from 'components/common/SettingsTable';
import useMessages from 'hooks/useMessages';
import useConfig from 'hooks/useConfig';
import useUser from 'hooks/useUser';
export function WebsitesTable({
data = [],
@ -10,13 +11,21 @@ export function WebsitesTable({
onFilterChange,
onPageChange,
onPageSizeChange,
showTeam,
}) {
const { formatMessage, labels } = useMessages();
const { openExternal } = useConfig();
const { user } = useUser();
const teamColumns = [
{ name: 'teamName', label: formatMessage(labels.teamName) },
{ name: 'owner', label: formatMessage(labels.owner) },
];
const columns = [
{ name: 'name', label: formatMessage(labels.name) },
{ name: 'domain', label: formatMessage(labels.domain) },
...(showTeam ? teamColumns : []),
{ name: 'action', label: ' ' },
];
@ -32,18 +41,28 @@ export function WebsitesTable({
filterValue={filterValue}
>
{row => {
const { id } = row;
const {
id,
teamWebsite,
user: { username, id: ownerId },
} = row;
if (showTeam) {
row.teamName = teamWebsite[0]?.team.name;
row.owner = username;
}
return (
<>
<Link href={`/settings/websites/${id}`}>
<Button>
<Icon>
<Icons.Edit />
</Icon>
<Text>{formatMessage(labels.edit)}</Text>
</Button>
</Link>
{(!showTeam || ownerId === user.id) && (
<Link href={`/settings/websites/${id}`}>
<Button>
<Icon>
<Icons.Edit />
</Icon>
<Text>{formatMessage(labels.edit)}</Text>
</Button>
</Link>
)}
<Link href={`/websites/${id}`} target={openExternal ? '_blank' : null}>
<Button>
<Icon>

View File

@ -1,7 +1,7 @@
import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
import Page from 'components/layout/Page';
import ReportsTable from 'components/pages/reports/ReportsTable';
import { useMessages, useReports } from 'hooks';
import { useMessages, useWebsiteReports } from 'hooks';
import Link from 'next/link';
import { Button, Flexbox, Icon, Icons, Text } from 'react-basics';
import WebsiteHeader from './WebsiteHeader';
@ -17,9 +17,9 @@ export function WebsiteReportsPage({ websiteId }) {
handleFilterChange,
handlePageChange,
handlePageSizeChange,
} = useReports(websiteId);
} = useWebsiteReports(websiteId);
const hasData = reports && reports.data.length !== 0;
const hasData = (reports && reports.data.length !== 0) || filter;
const handleDelete = async id => {
await deleteReport(id);
@ -48,11 +48,7 @@ export function WebsiteReportsPage({ websiteId }) {
filterValue={filter}
/>
)}
{!hasData && (
<EmptyPlaceholder message={formatMessage(messages.noReportsConfigured)}>
{/* {addButton} */}
</EmptyPlaceholder>
)}
{!hasData && <EmptyPlaceholder message={formatMessage(messages.noDataAvailable)} />}
</Page>
);
}

View File

@ -0,0 +1,67 @@
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
import WebsiteAddForm from 'components/pages/settings/websites/WebsiteAddForm';
import WebsiteList from 'components/pages/settings/websites/WebsitesList';
import { useMessages } from 'hooks';
import useUser from 'hooks/useUser';
import { ROLES } from 'lib/constants';
import { useState } from 'react';
import {
Button,
Icon,
Icons,
Item,
Modal,
ModalTrigger,
Tabs,
Text,
useToasts,
} from 'react-basics';
export function WebsitesPage() {
const { formatMessage, labels, messages } = useMessages();
const [tab, setTab] = useState('my-websites');
const [fetch, setFetch] = useState(1);
const { user } = useUser();
const { showToast } = useToasts();
const handleSave = async () => {
setFetch(fetch + 1);
showToast({ message: formatMessage(messages.saved), variant: 'success' });
};
const addButton = (
<>
{user.role !== ROLES.viewOnly && (
<ModalTrigger>
<Button variant="primary">
<Icon>
<Icons.Plus />
</Icon>
<Text>{formatMessage(labels.addWebsite)}</Text>
</Button>
<Modal title={formatMessage(labels.addWebsite)}>
{close => <WebsiteAddForm onSave={handleSave} onClose={close} />}
</Modal>
</ModalTrigger>
)}
</>
);
return (
<Page>
<PageHeader title={formatMessage(labels.websites)}>{addButton}</PageHeader>
<Tabs selectedKey={tab} onSelect={setTab} style={{ marginBottom: 30 }}>
<Item key="my-websites">{formatMessage(labels.myWebsites)}</Item>
<Item key="team-webaites">{formatMessage(labels.teamWebsites)}</Item>
</Tabs>
{tab === 'my-websites' && <WebsiteList showHeader={false} fetch={fetch} />}
{tab === 'team-webaites' && (
<WebsiteList showHeader={false} fetch={fetch} showTeam={true} onlyTeams={true} />
)}
</Page>
);
}
export default WebsitesPage;

View File

@ -20,3 +20,4 @@ export * from './useTheme';
export * from './useTimezone';
export * from './useUser';
export * from './useWebsite';
export * from './useWebsiteReports';

View File

@ -2,15 +2,15 @@ import { useState } from 'react';
import useApi from './useApi';
import useApiFilter from 'hooks/useApiFilter';
export function useReports(websiteId) {
export function useReports() {
const [modified, setModified] = useState(Date.now());
const { get, useQuery, del, useMutation } = useApi();
const { mutate } = useMutation(reportId => del(`/reports/${reportId}`));
const { filter, page, pageSize, handleFilterChange, handlePageChange, handlePageSizeChange } =
useApiFilter();
const { data, error, isLoading } = useQuery(
['reports:website', { websiteId, modified, filter, page, pageSize }],
() => get(`/reports`, { websiteId, filter, page, pageSize }),
['reports', { modified, filter, page, pageSize }],
() => get(`/reports`, { filter, page, pageSize }),
);
const deleteReport = id => {

View File

@ -0,0 +1,38 @@
import { useState } from 'react';
import useApi from './useApi';
import useApiFilter from 'hooks/useApiFilter';
export function useWebsiteReports(websiteId) {
const [modified, setModified] = useState(Date.now());
const { get, useQuery, del, useMutation } = useApi();
const { mutate } = useMutation(reportId => del(`/reports/${reportId}`));
const { filter, page, pageSize, handleFilterChange, handlePageChange, handlePageSizeChange } =
useApiFilter();
const { data, error, isLoading } = useQuery(
['reports:website', { websiteId, modified, filter, page, pageSize }],
() => get(`/websites/${websiteId}/reports`, { websiteId, filter, page, pageSize }),
);
const deleteReport = id => {
mutate(id, {
onSuccess: () => {
setModified(Date.now());
},
});
};
return {
reports: data,
error,
isLoading,
deleteReport,
filter,
page,
pageSize,
handleFilterChange,
handlePageChange,
handlePageSizeChange,
};
}
export default useWebsiteReports;

View File

@ -27,6 +27,7 @@ export interface WebsiteSearchFilter extends SearchFilter<WebsiteSearchFilterTyp
userId?: string;
teamId?: string;
includeTeams?: boolean;
onlyTeams?: boolean;
}
export interface UserSearchFilter extends SearchFilter<UserSearchFilterType> {
@ -40,6 +41,7 @@ export interface TeamSearchFilter extends SearchFilter<TeamSearchFilterType> {
export interface ReportSearchFilter extends SearchFilter<ReportSearchFilterType> {
userId?: string;
websiteId?: string;
includeTeams?: boolean;
}
export interface SearchFilter<T> {

View File

@ -4,7 +4,7 @@ import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody, ReportSearchFilterType, SearchFilter } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createReport, getReportsByWebsiteId } from 'queries';
import { createReport, getReportsByUserId, getReportsByWebsiteId } from 'queries';
export interface ReportsRequestQuery extends SearchFilter<ReportSearchFilterType> {}
@ -26,20 +26,14 @@ export default async (
await useCors(req, res);
await useAuth(req, res);
const { websiteId } = req.query;
const {
user: { id: userId },
} = req.auth;
if (req.method === 'GET') {
if (!(websiteId && (await canViewWebsite(req.auth, websiteId)))) {
return unauthorized(res);
}
const { page, filter, pageSize } = req.query;
const data = await getReportsByWebsiteId(websiteId, {
const data = await getReportsByUserId(userId, {
page,
filter,
pageSize: +pageSize || null,

View File

@ -21,7 +21,7 @@ export default async (
await useAuth(req, res);
const { user } = req.auth;
const { id: userId, page, filter, pageSize, includeTeams } = req.query;
const { id: userId, page, filter, pageSize, includeTeams, onlyTeams } = req.query;
if (req.method === 'GET') {
if (!user.isAdmin && user.id !== userId) {
@ -33,6 +33,7 @@ export default async (
filter,
pageSize: +pageSize || null,
includeTeams,
onlyTeams,
});
return ok(res, websites);

View File

@ -0,0 +1,38 @@
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody, ReportSearchFilterType, SearchFilter } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getReportsByWebsiteId } from 'queries';
export interface ReportsRequestQuery extends SearchFilter<ReportSearchFilterType> {
id: string;
}
export default async (
req: NextApiRequestQueryBody<ReportsRequestQuery, any>,
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
const { id: websiteId } = req.query;
if (req.method === 'GET') {
if (!(websiteId && (await canViewWebsite(req.auth, websiteId)))) {
return unauthorized(res);
}
const { page, filter, pageSize } = req.query;
const data = await getReportsByWebsiteId(websiteId, {
page,
filter,
pageSize: +pageSize || null,
});
return ok(res, data);
}
return methodNotAllowed(res);
};

13
pages/reports/index.js Normal file
View File

@ -0,0 +1,13 @@
import AppLayout from 'components/layout/AppLayout';
import ReportsPage from 'components/pages/reports/ReportsPage';
import { useMessages } from 'hooks';
export default function () {
const { formatMessage, labels } = useMessages();
return (
<AppLayout title={formatMessage(labels.reports)}>
<ReportsPage />
</AppLayout>
);
}

13
pages/websites/index.js Normal file
View File

@ -0,0 +1,13 @@
import AppLayout from 'components/layout/AppLayout';
import WebsitesPage from 'components/pages/websites/WebsitesPage';
import useMessages from 'hooks/useMessages';
export default function () {
const { formatMessage, labels } = useMessages();
return (
<AppLayout title={formatMessage(labels.websites)}>
<WebsitesPage />
</AppLayout>
);
}

View File

@ -28,19 +28,52 @@ export async function deleteReport(reportId: string): Promise<Report> {
export async function getReports(
ReportSearchFilter: ReportSearchFilter,
options?: { include?: Prisma.ReportInclude },
): Promise<FilterResult<Report[]>> {
const { userId, websiteId, filter, filterType = REPORT_FILTER_TYPES.all } = ReportSearchFilter;
const {
userId,
websiteId,
includeTeams,
filter,
filterType = REPORT_FILTER_TYPES.all,
} = ReportSearchFilter;
const where: Prisma.ReportWhereInput = {
...(userId && { userId: userId }),
...(websiteId && { websiteId: websiteId }),
...(filter && {
AND: {
AND: [
{
OR: [
{
...(userId && { userId: userId }),
},
{
...(includeTeams && {
website: {
teamWebsite: {
some: {
team: {
teamUser: {
some: {
userId,
},
},
},
},
},
},
}),
},
],
},
{
OR: [
{
...((filterType === REPORT_FILTER_TYPES.all ||
filterType === REPORT_FILTER_TYPES.name) && {
name: {
startsWith: filter,
mode: 'insensitive',
},
}),
},
@ -49,6 +82,7 @@ export async function getReports(
filterType === REPORT_FILTER_TYPES.description) && {
description: {
startsWith: filter,
mode: 'insensitive',
},
}),
},
@ -57,6 +91,7 @@ export async function getReports(
filterType === REPORT_FILTER_TYPES.type) && {
type: {
startsWith: filter,
mode: 'insensitive',
},
}),
},
@ -66,6 +101,7 @@ export async function getReports(
user: {
username: {
startsWith: filter,
mode: 'insensitive',
},
},
}),
@ -76,6 +112,7 @@ export async function getReports(
website: {
name: {
startsWith: filter,
mode: 'insensitive',
},
},
}),
@ -86,13 +123,14 @@ export async function getReports(
website: {
domain: {
startsWith: filter,
mode: 'insensitive',
},
},
}),
},
],
},
}),
],
};
const [pageFilters, getParameters] = prisma.getPageFilters(ReportSearchFilter);
@ -100,6 +138,7 @@ export async function getReports(
const reports = await prisma.client.report.findMany({
where,
...pageFilters,
...(options?.include && { include: options.include }),
});
const count = await prisma.client.report.count({
where,
@ -116,7 +155,19 @@ export async function getReportsByUserId(
userId: string,
filter: SearchFilter<ReportSearchFilterType>,
): Promise<FilterResult<Report[]>> {
return getReports({ userId, ...filter });
return getReports(
{ userId, ...filter },
{
include: {
website: {
select: {
domain: true,
userId: true,
},
},
},
},
);
}
export async function getReportsByWebsiteId(

View File

@ -97,7 +97,7 @@ export async function getTeams(
OR: [
{
...((filterType === TEAM_FILTER_TYPES.all || filterType === TEAM_FILTER_TYPES.name) && {
name: { startsWith: filter },
name: { startsWith: filter, mode: 'insensitive' },
}),
},
{
@ -109,6 +109,7 @@ export async function getTeams(
user: {
username: {
startsWith: filter,
mode: 'insensitive',
},
},
},

View File

@ -57,6 +57,7 @@ export async function getUsers(
filterType === USER_FILTER_TYPES.username) && {
username: {
startsWith: filter,
mode: 'insensitive',
},
}),
},

View File

@ -26,29 +26,11 @@ export async function getWebsites(
userId,
teamId,
includeTeams,
onlyTeams,
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: {
@ -61,28 +43,53 @@ export async function getWebsites(
{
OR: [
{
...(userId && {
userId,
}),
...(userId &&
!onlyTeams && {
userId,
}),
},
{
...(includeTeams && {
teamWebsite: {
some: {
team: {
teamUser: {
some: {
userId,
...((includeTeams || onlyTeams) && {
AND: [
{
teamWebsite: {
some: {
team: {
teamUser: {
some: {
userId,
},
},
},
},
},
},
},
{
userId: {
not: userId,
},
},
],
}),
},
],
},
{
OR: [
{
...((filterType === WEBSITE_FILTER_TYPES.all ||
filterType === WEBSITE_FILTER_TYPES.name) && {
name: { startsWith: filter, mode: 'insensitive' },
}),
},
{
...((filterType === WEBSITE_FILTER_TYPES.all ||
filterType === WEBSITE_FILTER_TYPES.domain) && {
domain: { startsWith: filter, mode: 'insensitive' },
}),
},
],
},
{ ...(filter && filterQuery) },
],
};
@ -108,7 +115,28 @@ export async function getWebsitesByUserId(
userId: string,
filter?: WebsiteSearchFilter,
): Promise<FilterResult<Website[]>> {
return getWebsites({ userId, ...filter });
return getWebsites(
{ userId, ...filter },
{
include: {
teamWebsite: {
include: {
team: {
select: {
name: true,
},
},
},
},
user: {
select: {
username: true,
id: true,
},
},
},
},
);
}
export async function getWebsitesByTeamId(

View File

@ -25,7 +25,7 @@ async function relationalQuery(
},
): Promise<
{
date: Date;
date: string;
day: number;
visitors: number;
returnVisitors: number;
@ -33,13 +33,15 @@ async function relationalQuery(
}[]
> {
const { startDate, endDate } = dateRange;
const { rawQuery } = prisma;
const { getDateQuery, rawQuery } = prisma;
const timezone = 'utc';
const unit = 'day';
return rawQuery(
`
WITH cohort_items AS (
select session_id,
date_trunc('day', created_at)::date as cohort_date
${getDateQuery('created_at', unit, timezone)} as cohort_date
from session
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
@ -47,7 +49,7 @@ async function relationalQuery(
user_activities AS (
select distinct
w.session_id,
(date_trunc('day', w.created_at)::date - c.cohort_date::date) as day_number
(${getDateQuery('created_at', unit, timezone)}::date - c.cohort_date::date) as day_number
from website_event w
join cohort_items c
on w.session_id = c.session_id
@ -98,7 +100,7 @@ async function clickhouseQuery(
},
): Promise<
{
date: Date;
date: string;
day: number;
visitors: number;
returnVisitors: number;
@ -106,13 +108,15 @@ async function clickhouseQuery(
}[]
> {
const { startDate, endDate } = dateRange;
const { rawQuery } = clickhouse;
const { getDateQuery, getDateStringQuery, rawQuery } = clickhouse;
const timezone = 'UTC';
const unit = 'day';
return rawQuery(
`
WITH cohort_items AS (
select
min(date_trunc('day', created_at)) as cohort_date,
min(${getDateQuery('created_at', unit, timezone)}) as cohort_date,
session_id
from website_event
where website_id = {websiteId:UUID}
@ -122,7 +126,7 @@ async function clickhouseQuery(
user_activities AS (
select distinct
w.session_id,
(date_trunc('day', w.created_at) - c.cohort_date) / 86400 as day_number
(${getDateQuery('created_at', unit, timezone)} - c.cohort_date) / 86400 as day_number
from website_event w
join cohort_items c
on w.session_id = c.session_id
@ -147,7 +151,7 @@ async function clickhouseQuery(
group by 1, 2
)
select
c.cohort_date as date,
${getDateStringQuery('c.cohort_date', unit)} as date,
c.day_number as day,
s.visitors as visitors,
c.visitors returnVisitors,