Updates to insights, event data, telemetry.

This commit is contained in:
Mike Cao 2023-07-23 13:18:01 -07:00
parent 39562d4a64
commit e4bd314bd6
44 changed files with 413 additions and 278 deletions

View File

@ -7,14 +7,6 @@ export function EventDataTable({ data = [] }) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { resolveUrl } = usePageQuery(); const { resolveUrl } = usePageQuery();
function linkToView(row, cell) {
return (
<Link href={resolveUrl({ view: row.field, event: row.event })} shallow={true}>
{cell}
</Link>
);
}
if (data.length === 0) { if (data.length === 0) {
return <Empty />; return <Empty />;
} }
@ -22,10 +14,14 @@ export function EventDataTable({ data = [] }) {
return ( return (
<GridTable data={data}> <GridTable data={data}>
<GridColumn name="event" label={formatMessage(labels.event)}> <GridColumn name="event" label={formatMessage(labels.event)}>
{row => linkToView(row, row.event)} {row => (
<Link href={resolveUrl({ event: row.event })} shallow={true}>
{row.event}
</Link>
)}
</GridColumn> </GridColumn>
<GridColumn name="field" label={formatMessage(labels.field)}> <GridColumn name="field" label={formatMessage(labels.field)}>
{row => linkToView(row, row.field)} {row => row.field}
</GridColumn> </GridColumn>
<GridColumn name="total" label={formatMessage(labels.totalRecords)}> <GridColumn name="total" label={formatMessage(labels.totalRecords)}>
{({ total }) => total.toLocaleString()} {({ total }) => total.toLocaleString()}

View File

@ -5,14 +5,14 @@ import Icons from 'components/icons';
import PageHeader from 'components/layout/PageHeader'; import PageHeader from 'components/layout/PageHeader';
import Empty from 'components/common/Empty'; import Empty from 'components/common/Empty';
export function EventDataTable({ data = [], field, event }) { export function EventDataValueTable({ data = [], event }) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { resolveUrl } = usePageQuery(); const { resolveUrl } = usePageQuery();
const Title = () => { const Title = () => {
return ( return (
<> <>
<Link href={resolveUrl({ view: undefined })}> <Link href={resolveUrl({ event: undefined })}>
<Button> <Button>
<Icon rotate={180}> <Icon rotate={180}>
<Icons.ArrowRight /> <Icons.ArrowRight />
@ -20,9 +20,7 @@ export function EventDataTable({ data = [], field, event }) {
<Text>{formatMessage(labels.back)}</Text> <Text>{formatMessage(labels.back)}</Text>
</Button> </Button>
</Link> </Link>
<Text> <Text>{event}</Text>
{event} - {field}
</Text>
</> </>
); );
}; };
@ -33,6 +31,7 @@ export function EventDataTable({ data = [], field, event }) {
{data.length <= 0 && <Empty />} {data.length <= 0 && <Empty />}
{data.length > 0 && ( {data.length > 0 && (
<GridTable data={data}> <GridTable data={data}>
<GridColumn name="field" label={formatMessage(labels.field)} />
<GridColumn name="value" label={formatMessage(labels.value)} /> <GridColumn name="value" label={formatMessage(labels.value)} />
<GridColumn name="total" label={formatMessage(labels.totalRecords)} width="200px"> <GridColumn name="total" label={formatMessage(labels.totalRecords)} width="200px">
{({ total }) => total.toLocaleString()} {({ total }) => total.toLocaleString()}
@ -43,4 +42,4 @@ export function EventDataTable({ data = [], field, event }) {
); );
} }
export default EventDataTable; export default EventDataValueTable;

View File

@ -5,20 +5,18 @@ import { EventDataMetricsBar } from 'components/pages/event-data/EventDataMetric
import { useDateRange, useApi, usePageQuery } from 'hooks'; import { useDateRange, useApi, usePageQuery } from 'hooks';
import styles from './WebsiteEventData.module.css'; import styles from './WebsiteEventData.module.css';
function useFields(websiteId, field, event) { function useData(websiteId, event) {
const [dateRange] = useDateRange(websiteId); const [dateRange] = useDateRange(websiteId);
const { startDate, endDate } = dateRange; const { startDate, endDate } = dateRange;
const { get, useQuery } = useApi(); const { get, useQuery } = useApi();
const { data, error, isLoading } = useQuery( const { data, error, isLoading } = useQuery(
['event-data:fields', { websiteId, startDate, endDate, field }], ['event-data:events', { websiteId, startDate, endDate, event }],
() => () =>
get('/event-data/fields', { get('/event-data/events', {
websiteId, websiteId,
startAt: +startDate, startAt: +startDate,
endAt: +endDate, endAt: +endDate,
field,
event, event,
withEventNames: true,
}), }),
{ enabled: !!(websiteId && startDate && endDate) }, { enabled: !!(websiteId && startDate && endDate) },
); );
@ -28,15 +26,15 @@ function useFields(websiteId, field, event) {
export default function WebsiteEventData({ websiteId }) { export default function WebsiteEventData({ websiteId }) {
const { const {
query: { view, event }, query: { event },
} = usePageQuery(); } = usePageQuery();
const { data } = useFields(websiteId, view, event); const { data } = useData(websiteId, event);
return ( return (
<Flexbox className={styles.container} direction="column" gap={20}> <Flexbox className={styles.container} direction="column" gap={20}>
<EventDataMetricsBar websiteId={websiteId} /> <EventDataMetricsBar websiteId={websiteId} />
{!view && <EventDataTable data={data} />} {!event && <EventDataTable data={data} />}
{view && <EventDataValueTable field={view} event={event} data={data} />} {event && <EventDataValueTable event={event} data={data} />}
</Flexbox> </Flexbox>
); );
} }

View File

@ -6,7 +6,7 @@ CREATE TABLE umami.website_event
website_id UUID, website_id UUID,
session_id UUID, session_id UUID,
event_id UUID, event_id UUID,
--session --sessions
hostname LowCardinality(String), hostname LowCardinality(String),
browser LowCardinality(String), browser LowCardinality(String),
os LowCardinality(String), os LowCardinality(String),
@ -17,14 +17,14 @@ CREATE TABLE umami.website_event
subdivision1 LowCardinality(String), subdivision1 LowCardinality(String),
subdivision2 LowCardinality(String), subdivision2 LowCardinality(String),
city String, city String,
--pageview --pageviews
url_path String, url_path String,
url_query String, url_query String,
referrer_path String, referrer_path String,
referrer_query String, referrer_query String,
referrer_domain String, referrer_domain String,
page_title String, page_title String,
--event --events
event_type UInt32, event_type UInt32,
event_name String, event_name String,
created_at DateTime('UTC'), created_at DateTime('UTC'),
@ -38,7 +38,7 @@ CREATE TABLE umami.website_event_queue (
website_id UUID, website_id UUID,
session_id UUID, session_id UUID,
event_id UUID, event_id UUID,
--session --sessions
hostname LowCardinality(String), hostname LowCardinality(String),
browser LowCardinality(String), browser LowCardinality(String),
os LowCardinality(String), os LowCardinality(String),
@ -49,14 +49,14 @@ CREATE TABLE umami.website_event_queue (
subdivision1 LowCardinality(String), subdivision1 LowCardinality(String),
subdivision2 LowCardinality(String), subdivision2 LowCardinality(String),
city String, city String,
--pageview --pageviews
url_path String, url_path String,
url_query String, url_query String,
referrer_path String, referrer_path String,
referrer_query String, referrer_query String,
referrer_domain String, referrer_domain String,
page_title String, page_title String,
--event --events
event_type UInt32, event_type UInt32,
event_name String, event_name String,
created_at DateTime('UTC'), created_at DateTime('UTC'),
@ -66,7 +66,7 @@ CREATE TABLE umami.website_event_queue (
) )
ENGINE = Kafka ENGINE = Kafka
SETTINGS kafka_broker_list = 'domain:9092,domain:9093,domain:9094', -- input broker list SETTINGS kafka_broker_list = 'domain:9092,domain:9093,domain:9094', -- input broker list
kafka_topic_list = 'event', kafka_topic_list = 'events',
kafka_group_name = 'event_consumer_group', kafka_group_name = 'event_consumer_group',
kafka_format = 'JSONEachRow', kafka_format = 'JSONEachRow',
kafka_max_block_size = 1048576, kafka_max_block_size = 1048576,

View File

@ -62,10 +62,6 @@ function getDateFormat(date) {
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`; return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
} }
function getBetweenDates(field, startAt, endAt) {
return `${field} between ${getDateFormat(startAt)} and ${getDateFormat(endAt)}`;
}
function getEventDataFilterQuery( function getEventDataFilterQuery(
filters: { filters: {
eventKey?: string; eventKey?: string;
@ -150,7 +146,22 @@ function parseFilters(filters: WebsiteMetricFilter = {}, params: any = {}) {
}; };
} }
async function rawQuery<T>(query, params = {}): Promise<T> { function formatField(field, type, value) {
switch (type) {
case 'date':
return getDateFormat(value);
default:
return field;
}
}
async function rawQuery<T>(sql, params = {}): Promise<T> {
const query = sql.replaceAll(/\{\{\w+:\w+}}/g, token => {
const [, field, type] = token.match(/\{\{(\w+):(\w+)}}/);
return formatField(field, type, params[field]);
});
if (process.env.LOG_QUERY) { if (process.env.LOG_QUERY) {
log('QUERY:\n', query); log('QUERY:\n', query);
log('PARAMETERS:\n', params); log('PARAMETERS:\n', params);
@ -189,7 +200,6 @@ export default {
getDateStringQuery, getDateStringQuery,
getDateQuery, getDateQuery,
getDateFormat, getDateFormat,
getBetweenDates,
getFilterQuery, getFilterQuery,
getFunnelQuery, getFunnelQuery,
getEventDataFilterQuery, getEventDataFilterQuery,

View File

@ -18,7 +18,7 @@ export const DEFAULT_THEME = 'light';
export const DEFAULT_ANIMATION_DURATION = 300; export const DEFAULT_ANIMATION_DURATION = 300;
export const DEFAULT_DATE_RANGE = '24hour'; export const DEFAULT_DATE_RANGE = '24hour';
export const DEFAULT_WEBSITE_LIMIT = 10; export const DEFAULT_WEBSITE_LIMIT = 10;
export const DEFAULT_CREATED_AT = '2000-01-01'; export const DEFAULT_RESET_DATE = '2000-01-01';
export const REALTIME_RANGE = 30; export const REALTIME_RANGE = 30;
export const REALTIME_INTERVAL = 5000; export const REALTIME_INTERVAL = 5000;

View File

@ -1,5 +1,3 @@
import crypto from 'crypto';
import { v4, v5 } from 'uuid';
import { startOfMonth } from 'date-fns'; import { startOfMonth } from 'date-fns';
import { hash } from 'next-basics'; import { hash } from 'next-basics';
@ -12,13 +10,3 @@ export function salt() {
return hash(secret(), ROTATING_SALT); return hash(secret(), ROTATING_SALT);
} }
export function uuid(...args) {
if (!args.length) return v4();
return v5(hash(...args, salt()), v5.DNS);
}
export function md5(...args) {
return crypto.createHash('md5').update(args.join('')).digest('hex');
}

View File

@ -1,5 +1,5 @@
import path from 'path'; import path from 'path';
import requestIp from 'request-ip'; import { getClientIp } from 'request-ip';
import { browserName, detectOS } from 'detect-browser'; import { browserName, detectOS } from 'detect-browser';
import isLocalhost from 'is-localhost-ip'; import isLocalhost from 'is-localhost-ip';
import maxmind from 'maxmind'; import maxmind from 'maxmind';
@ -25,7 +25,7 @@ export function getIpAddress(req) {
return req.headers['cf-connecting-ip']; return req.headers['cf-connecting-ip'];
} }
return requestIp.getClientIp(req); return getClientIp(req);
} }
export function getDevice(screen, os) { export function getDevice(screen, os) {

View File

@ -1,6 +1,6 @@
import { secret, uuid } from 'lib/crypto'; import { secret } from 'lib/crypto';
import { getClientInfo, getJsonBody } from 'lib/detect'; import { getClientInfo, getJsonBody } from 'lib/detect';
import { parseToken } from 'next-basics'; import { parseToken, uuid } from 'next-basics';
import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send'; import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
import { createSession } from 'queries'; import { createSession } from 'queries';
import { validate } from 'uuid'; import { validate } from 'uuid';
@ -30,7 +30,6 @@ export async function findSession(req: NextApiRequestCollect) {
// Verify payload // Verify payload
const { website: websiteId, hostname, screen, language } = payload; const { website: websiteId, hostname, screen, language } = payload;
// Check the hostname value for legality to eliminate dirty data // Check the hostname value for legality to eliminate dirty data
const validHostnameRegex = /^[\w-.]+$/; const validHostnameRegex = /^[\w-.]+$/;
if (!validHostnameRegex.test(hostname)) { if (!validHostnameRegex.test(hostname)) {

9
lib/sql.ts Normal file
View File

@ -0,0 +1,9 @@
export function buildSql(query: string, parameters: object) {
const params = { ...parameters };
const sql = query.replaceAll(/\$[\w_]+/g, name => {
return name;
});
return { sql, params };
}

View File

@ -90,7 +90,7 @@
"maxmind": "^4.3.6", "maxmind": "^4.3.6",
"moment-timezone": "^0.5.35", "moment-timezone": "^0.5.35",
"next": "13.3.1", "next": "13.3.1",
"next-basics": "^0.31.0", "next-basics": "^0.33.0",
"node-fetch": "^3.2.8", "node-fetch": "^3.2.8",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"react": "^18.2.0", "react": "^18.2.0",

View File

@ -0,0 +1,39 @@
import { canViewWebsite } from 'lib/auth';
import { useCors, useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getEventDataEvents } from 'queries';
export interface EventDataFieldsRequestBody {
websiteId: string;
dateRange: {
startDate: string;
endDate: string;
};
}
export default async (
req: NextApiRequestQueryBody<any, EventDataFieldsRequestBody>,
res: NextApiResponse<any>,
) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'GET') {
const { websiteId, startAt, endAt, field, event } = req.query;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getEventDataEvents(websiteId, new Date(+startAt), new Date(+endAt), {
field,
event,
});
return ok(res, data);
}
return methodNotAllowed(res);
};

View File

@ -21,20 +21,13 @@ export default async (
await useAuth(req, res); await useAuth(req, res);
if (req.method === 'GET') { if (req.method === 'GET') {
const { websiteId, startAt, endAt, field, event, withEventNames } = req.query; const { websiteId, startAt, endAt, field } = req.query;
if (!(await canViewWebsite(req.auth, websiteId))) { if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res); return unauthorized(res);
} }
const data = await getEventDataFields( const data = await getEventDataFields(websiteId, new Date(+startAt), new Date(+endAt), field);
websiteId,
new Date(+startAt),
new Date(+endAt),
field,
event,
withEventNames,
);
return ok(res, data); return ok(res, data);
} }

View File

@ -3,7 +3,7 @@ import { useCors, useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types'; import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics'; import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getPageviewFunnel } from 'queries'; import { getFunnel } from 'queries';
export interface FunnelRequestBody { export interface FunnelRequestBody {
websiteId: string; websiteId: string;
@ -41,7 +41,7 @@ export default async (
return unauthorized(res); return unauthorized(res);
} }
const data = await getPageviewFunnel(websiteId, { const data = await getFunnel(websiteId, {
startDate: new Date(startDate), startDate: new Date(startDate),
endDate: new Date(endDate), endDate: new Date(endDate),
urls, urls,

View File

@ -1,8 +1,7 @@
import { uuid } from 'lib/crypto';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types'; import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics'; import { methodNotAllowed, ok, unauthorized, uuid } from 'next-basics';
import { createReport, getReports } from 'queries'; import { createReport, getReports } from 'queries';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';

View File

@ -0,0 +1,51 @@
import { canViewWebsite } from 'lib/auth';
import { useCors, useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getInsights } from 'queries';
export interface InsightsRequestBody {
websiteId: string;
dateRange: {
startDate: string;
endDate: string;
};
fields: string[];
filters: string[];
groups: string[];
}
export default async (
req: NextApiRequestQueryBody<any, InsightsRequestBody>,
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'POST') {
const {
websiteId,
dateRange: { startDate, endDate },
fields,
filters,
groups,
} = req.body;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getInsights(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
fields,
filters,
groups,
});
return ok(res, data);
}
return methodNotAllowed(res);
};

View File

@ -1,7 +1,7 @@
import { Team } from '@prisma/client'; import { Team } from '@prisma/client';
import { NextApiRequestQueryBody } from 'lib/types'; import { NextApiRequestQueryBody } from 'lib/types';
import { canCreateTeam } from 'lib/auth'; import { canCreateTeam } from 'lib/auth';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';

View File

@ -1,6 +1,6 @@
import { canCreateUser, canViewUsers } from 'lib/auth'; import { canCreateUser, canViewUsers } from 'lib/auth';
import { ROLES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody, Role, User } from 'lib/types'; import { NextApiRequestQueryBody, Role, User } from 'lib/types';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,5 +1,5 @@
import { canCreateWebsite } from 'lib/auth'; import { canCreateWebsite } from 'lib/auth';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types'; import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,7 +1,7 @@
import { Prisma, Team, TeamWebsite } from '@prisma/client'; import { Prisma, Team, TeamWebsite } from '@prisma/client';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto';
import { ROLES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import { uuid } from 'next-basics';
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> { export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
return prisma.client.team.findFirst({ return prisma.client.team.findFirst({

View File

@ -1,5 +1,5 @@
import { Prisma, TeamUser } from '@prisma/client'; import { Prisma, TeamUser } from '@prisma/client';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
export async function getTeamUserById(teamUserId: string): Promise<TeamUser> { export async function getTeamUserById(teamUserId: string): Promise<TeamUser> {

View File

@ -1,6 +1,6 @@
import { Prisma, Team, TeamUser, TeamWebsite, Website } from '@prisma/client'; import { Prisma, Team, TeamUser, TeamWebsite, Website } from '@prisma/client';
import { ROLES } from 'lib/constants'; import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
export async function getTeamWebsite( export async function getTeamWebsite(

View File

@ -0,0 +1,119 @@
import { buildSql } from 'lib/sql';
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { WebsiteEventDataFields } from 'lib/types';
import { loadWebsite } from 'lib/query';
import { DEFAULT_RESET_DATE } from 'lib/constants';
export async function getEventDataEvents(
...args: [
websiteId: string,
startDate: Date,
endDate: Date,
filters: { field?: string; event?: string },
]
): Promise<WebsiteEventDataFields[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(
websiteId: string,
startDate: Date,
endDate: Date,
filters: { field?: string; event?: string },
) {
const { toUuid, rawQuery } = prisma;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const { field, event } = filters;
if (field) {
if (event) {
return rawQuery(
`select ed.event_key as field,
ed.string_value as value,
count(ed.*) as total
from event_data as ed
inner join website_event as we
on we.event_id = ed.website_event_id
where ed.website_id = $1${toUuid()}
and ed.event_key = $2
and ed.created_at >= $3
and ed.created_at between $4 and $5
and we.event_name = $6
group by ed.event_key, ed.string_value
order by 3 desc, 2 desc, 1 asc
`,
[websiteId, field, resetDate, startDate, endDate, event] as any,
);
}
return rawQuery(
`select event_key as field,
string_value as value,
count(*) as total
from event_data
where website_id = $1${toUuid()}
and event_key = $2
and created_at >= $3
and created_at between $4 and $5
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
`,
[websiteId, field, resetDate, startDate, endDate] as any,
);
}
}
async function clickhouseQuery(
websiteId: string,
startDate: Date,
endDate: Date,
filters: { field?: string; event?: string },
) {
const { rawQuery } = clickhouse;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const { event } = filters;
if (event) {
return rawQuery(
`select
event_name as event,
event_key as field,
data_type as type,
string_value as value,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and created_at >= {resetDate:DateTime}
and created_at between {startDate:DateTime} and {endDate:DateTime}
and event_name = {event:String}
group by event_key, data_type, string_value, event_name
order by 1 asc, 2 asc, 3 asc, 4 desc
limit 100`,
{ websiteId, resetDate, startDate, endDate, event },
);
}
return rawQuery(
`select
event_name as event,
event_key as field,
data_type as type,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and created_at >= {resetDate:DateTime}
and created_at between {startDate:DateTime} and {endDate:DateTime}
group by event_key, data_type, event_name
order by 1 asc, 2 asc
limit 100
`,
{ websiteId, resetDate, startDate, endDate },
);
}

View File

@ -3,17 +3,10 @@ import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { WebsiteEventDataFields } from 'lib/types'; import { WebsiteEventDataFields } from 'lib/types';
import { loadWebsite } from 'lib/query'; import { loadWebsite } from 'lib/query';
import { DEFAULT_CREATED_AT } from 'lib/constants'; import { DEFAULT_RESET_DATE } from 'lib/constants';
export async function getEventDataFields( export async function getEventDataFields(
...args: [ ...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
websiteId: string,
startDate: Date,
endDate: Date,
field?: string,
event?: string,
withEventNames?: boolean,
]
): Promise<WebsiteEventDataFields[]> { ): Promise<WebsiteEventDataFields[]> {
return runQuery({ return runQuery({
[PRISMA]: () => relationalQuery(...args), [PRISMA]: () => relationalQuery(...args),
@ -21,37 +14,12 @@ export async function getEventDataFields(
}); });
} }
async function relationalQuery( async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
websiteId: string,
startDate: Date,
endDate: Date,
field: string,
event: string,
withEventNames: boolean,
) {
const { toUuid, rawQuery } = prisma; const { toUuid, rawQuery } = prisma;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
if (field) { if (field) {
if (event) {
return rawQuery(
`select ed.event_key as field,
ed.string_value as value,
count(ed.*) as total
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
where ed.website_id = $1${toUuid()}
and ed.event_key = $2
and ed.created_at >= $3
and ed.created_at between $4 and $5
and e.event_name = $6
group by ed.event_key, ed.string_value
order by 3 desc, 2 desc, 1 asc
`,
[websiteId, field, resetDate, startDate, endDate, event] as any,
);
}
return rawQuery( return rawQuery(
`select event_key as field, `select event_key as field,
string_value as value, string_value as value,
@ -63,30 +31,12 @@ async function relationalQuery(
and created_at between $4 and $5 and created_at between $4 and $5
group by event_key, string_value group by event_key, string_value
order by 3 desc, 2 desc, 1 asc order by 3 desc, 2 desc, 1 asc
limit 100
`, `,
[websiteId, field, resetDate, startDate, endDate] as any, [websiteId, field, resetDate, startDate, endDate] as any,
); );
} }
if (withEventNames) {
return rawQuery(
`select
ed.event_key as field,
ed.data_type as type,
count(ed.*) as total,
e.event_name as event
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
where ed.website_id = $1${toUuid()}
and ed.created_at >= $2
and ed.created_at between $3 and $4
group by e.event_name, ed.event_key, ed.data_type
order by 3 desc, 2 asc, 1 asc
`,
[websiteId, resetDate, startDate, endDate] as any,
);
}
return rawQuery( return rawQuery(
`select `select
event_key as field, event_key as field,
@ -98,43 +48,18 @@ async function relationalQuery(
and created_at between $3 and $4 and created_at between $3 and $4
group by event_key, data_type group by event_key, data_type
order by 3 desc, 2 asc, 1 asc order by 3 desc, 2 asc, 1 asc
limit 100
`, `,
[websiteId, resetDate, startDate, endDate] as any, [websiteId, resetDate, startDate, endDate] as any,
); );
} }
async function clickhouseQuery( async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
websiteId: string, const { rawQuery, getDateFormat } = clickhouse;
startDate: Date,
endDate: Date,
field: string,
event: string,
withEventNames: boolean,
) {
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
if (field) { if (field) {
if (event) {
return rawQuery(
`select
ed.event_key as field,
ed.string_value as value,
count(ed.*) as total
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
where ed.website_id = {websiteId:UUID}
and ed.event_key = {field:String}
and ed.created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('ed.created_at', startDate, endDate)}
and e.event_name = {event:String}
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
`,
{ websiteId, field, event },
);
}
return rawQuery( return rawQuery(
`select `select
event_key as field, event_key as field,
@ -144,33 +69,15 @@ async function clickhouseQuery(
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and event_key = {field:String} and event_key = {field:String}
and created_at >= ${getDateFormat(resetDate)} and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)} and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
group by event_key, string_value group by event_key, string_value
order by 3 desc, 2 desc, 1 asc order by 3 desc, 2 desc, 1 asc
limit 100
`, `,
{ websiteId, field }, { websiteId, field },
); );
} }
if (withEventNames) {
return rawQuery(
`select
ed.event_key as field,
ed.data_type as type,
count(ed.*) as total,
e.event_name as event
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
where ed.website_id = {websiteId:UUID}
and ed.created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('ed.created_at', startDate, endDate)}
group by e.event_name, ed.event_key, ed.data_type
order by 3 desc, 2 asc, 1 asc
`,
[websiteId, resetDate, startDate, endDate] as any,
);
}
return rawQuery( return rawQuery(
`select `select
event_key as field, event_key as field,
@ -179,9 +86,10 @@ async function clickhouseQuery(
from event_data from event_data
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and created_at >= ${getDateFormat(resetDate)} and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)} and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
group by event_key, data_type group by event_key, data_type
order by 3 desc, 2 asc, 1 asc order by 3 desc, 2 asc, 1 asc
limit 100
`, `,
{ websiteId }, { websiteId },
); );

View File

@ -9,7 +9,7 @@ export function getEventDataUsage(...args: [websiteIds: string[], startDate: Dat
} }
function relationalQuery(websiteIds: string[], startDate: Date, endDate: Date) { function relationalQuery(websiteIds: string[], startDate: Date, endDate: Date) {
throw new Error('Not Implemented'); throw new Error('Not implemented.');
} }
function clickhouseQuery(websiteIds: string[], startDate: Date, endDate: Date) { function clickhouseQuery(websiteIds: string[], startDate: Date, endDate: Date) {

View File

@ -1,6 +1,6 @@
import { Prisma } from '@prisma/client'; import { Prisma } from '@prisma/client';
import { DATA_TYPE } from 'lib/constants'; import { DATA_TYPE } from 'lib/constants';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { flattenJSON } from 'lib/dynamicData'; import { flattenJSON } from 'lib/dynamicData';
import kafka from 'lib/kafka'; import kafka from 'lib/kafka';

View File

@ -2,7 +2,7 @@ import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { WebsiteEventMetric } from 'lib/types'; import { WebsiteEventMetric } from 'lib/types';
import { DEFAULT_CREATED_AT, EVENT_TYPE } from 'lib/constants'; import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
import { loadWebsite } from 'lib/query'; import { loadWebsite } from 'lib/query';
export async function getEventMetrics( export async function getEventMetrics(
@ -47,7 +47,7 @@ async function relationalQuery(
) { ) {
const { toUuid, rawQuery, getDateQuery, getFilterQuery } = prisma; const { toUuid, rawQuery, getDateQuery, getFilterQuery } = prisma;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params: any = [websiteId, resetDate, startDate, endDate]; const params: any = [websiteId, resetDate, startDate, endDate];
const filterQuery = getFilterQuery(filters, params); const filterQuery = getFilterQuery(filters, params);
@ -87,9 +87,9 @@ async function clickhouseQuery(
}; };
}, },
) { ) {
const { rawQuery, getDateQuery, getDateFormat, getBetweenDates, getFilterQuery } = clickhouse; const { rawQuery, getDateQuery, getDateFormat, getFilterQuery } = clickhouse;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params = { websiteId }; const params = { websiteId };
return rawQuery( return rawQuery(
@ -101,7 +101,7 @@ async function clickhouseQuery(
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and event_type = ${EVENT_TYPE.customEvent} and event_type = ${EVENT_TYPE.customEvent}
and created_at >= ${getDateFormat(resetDate)} and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)} and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
${getFilterQuery(filters, params)} ${getFilterQuery(filters, params)}
group by x, t group by x, t
order by t`, order by t`,

View File

@ -9,7 +9,7 @@ export function getEventUsage(...args: [websiteIds: string[], startDate: Date, e
} }
function relationalQuery(websiteIds: string[], startDate: Date, endDate: Date) { function relationalQuery(websiteIds: string[], startDate: Date, endDate: Date) {
throw new Error('Not Implemented'); throw new Error('Not implemented.');
} }
function clickhouseQuery(websiteIds: string[], startDate: Date, endDate: Date) { function clickhouseQuery(websiteIds: string[], startDate: Date, endDate: Date) {

View File

@ -2,7 +2,7 @@ import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import kafka from 'lib/kafka'; import kafka from 'lib/kafka';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import { saveEventData } from 'queries/analytics/eventData/saveEventData'; import { saveEventData } from 'queries/analytics/eventData/saveEventData';
export async function saveEvent(args: { export async function saveEvent(args: {

View File

@ -1,7 +1,7 @@
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { DEFAULT_CREATED_AT, EVENT_TYPE } from 'lib/constants'; import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
import { loadWebsite } from 'lib/query'; import { loadWebsite } from 'lib/query';
export async function getPageviewMetrics( export async function getPageviewMetrics(
@ -33,7 +33,7 @@ async function relationalQuery(
const { startDate, endDate, filters = {}, column } = criteria; const { startDate, endDate, filters = {}, column } = criteria;
const { rawQuery, parseFilters, toUuid } = prisma; const { rawQuery, parseFilters, toUuid } = prisma;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params: any = [ const params: any = [
websiteId, websiteId,
resetDate, resetDate,
@ -45,7 +45,8 @@ async function relationalQuery(
let excludeDomain = ''; let excludeDomain = '';
if (column === 'referrer_domain') { if (column === 'referrer_domain') {
excludeDomain = 'and (website_event.referrer_domain != $6 or website_event.referrer_domain is null)'; excludeDomain =
'and (website_event.referrer_domain != $6 or website_event.referrer_domain is null)';
params.push(website.domain); params.push(website.domain);
} }
@ -78,9 +79,9 @@ async function clickhouseQuery(
}, },
) { ) {
const { startDate, endDate, filters = {}, column } = criteria; const { startDate, endDate, filters = {}, column } = criteria;
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse; const { rawQuery, getDateFormat, parseFilters } = clickhouse;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params = { const params = {
websiteId, websiteId,
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView, eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
@ -102,7 +103,7 @@ async function clickhouseQuery(
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and event_type = {eventType:UInt32} and event_type = {eventType:UInt32}
and created_at >= ${getDateFormat(resetDate)} and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)} and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
${excludeDomain} ${excludeDomain}
${filterQuery} ${filterQuery}
group by x group by x

View File

@ -1,7 +1,7 @@
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { DEFAULT_CREATED_AT, EVENT_TYPE } from 'lib/constants'; import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
import { loadWebsite } from 'lib/query'; import { loadWebsite } from 'lib/query';
export async function getPageviewStats( export async function getPageviewStats(
@ -47,7 +47,7 @@ async function relationalQuery(
} = criteria; } = criteria;
const { toUuid, getDateQuery, parseFilters, rawQuery } = prisma; const { toUuid, getDateQuery, parseFilters, rawQuery } = prisma;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params: any = [websiteId, resetDate, startDate, endDate]; const params: any = [websiteId, resetDate, startDate, endDate];
const { filterQuery, joinSession } = parseFilters(filters, params); const { filterQuery, joinSession } = parseFilters(filters, params);
@ -86,16 +86,9 @@ async function clickhouseQuery(
count = '*', count = '*',
filters = {}, filters = {},
} = criteria; } = criteria;
const { const { parseFilters, getDateFormat, rawQuery, getDateStringQuery, getDateQuery } = clickhouse;
parseFilters,
getDateFormat,
rawQuery,
getDateStringQuery,
getDateQuery,
getBetweenDates,
} = clickhouse;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params = { websiteId }; const params = { websiteId };
const { filterQuery } = parseFilters(filters, params); const { filterQuery } = parseFilters(filters, params);
@ -111,7 +104,7 @@ async function clickhouseQuery(
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and event_type = ${EVENT_TYPE.pageView} and event_type = ${EVENT_TYPE.pageView}
and created_at >= ${getDateFormat(resetDate)} and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)} and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
${filterQuery} ${filterQuery}
group by t) g group by t) g
order by t`, order by t`,

View File

@ -2,7 +2,7 @@ import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
export async function getPageviewFunnel( export async function getFunnel(
...args: [ ...args: [
websiteId: string, websiteId: string,
criteria: { criteria: {
@ -76,14 +76,8 @@ async function clickhouseQuery(
}[] }[]
> { > {
const { windowMinutes, startDate, endDate, urls } = criteria; const { windowMinutes, startDate, endDate, urls } = criteria;
const { rawQuery, getBetweenDates, getFunnelQuery } = clickhouse; const { rawQuery, getFunnelQuery } = clickhouse;
const { columnsQuery, conditionQuery, urlParams } = getFunnelQuery(urls); const { columnsQuery, urlParams } = getFunnelQuery(urls);
const params = {
websiteId,
window: windowMinutes * 60,
...urlParams,
};
return rawQuery<{ level: number; count: number }[]>( return rawQuery<{ level: number; count: number }[]>(
` `
@ -98,13 +92,19 @@ async function clickhouseQuery(
) AS level ) AS level
FROM website_event FROM website_event
WHERE website_id = {websiteId:UUID} WHERE website_id = {websiteId:UUID}
and ${getBetweenDates('created_at', startDate, endDate)} AND created_at BETWEEN {startDate:DateTime} AND {endDate:DateTime}
GROUP BY 1 GROUP BY 1
) )
GROUP BY level GROUP BY level
ORDER BY level ASC; ORDER BY level ASC;
`, `,
params, {
websiteId,
startDate,
endDate,
window: windowMinutes * 60,
...urlParams,
},
).then(results => { ).then(results => {
return urls.map((a, i) => ({ return urls.map((a, i) => ({
x: a, x: a,

View File

@ -0,0 +1,42 @@
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
export interface GetInsightsCriteria {
startDate: Date;
endDate: Date;
fields: string[];
filters: string[];
groups: string[];
}
export async function getInsights(...args: [websiteId: string, criteria: GetInsightsCriteria]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(
websiteId: string,
criteria: GetInsightsCriteria,
): Promise<
{
x: string;
y: number;
}[]
> {
return null;
}
async function clickhouseQuery(
websiteId: string,
criteria: GetInsightsCriteria,
): Promise<
{
x: string;
y: number;
}[]
> {
return null;
}

View File

@ -1,7 +1,7 @@
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { DEFAULT_CREATED_AT, EVENT_TYPE } from 'lib/constants'; import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
import { loadWebsite } from 'lib/query'; import { loadWebsite } from 'lib/query';
export async function getSessionMetrics( export async function getSessionMetrics(
@ -21,7 +21,7 @@ async function relationalQuery(
criteria: { startDate: Date; endDate: Date; column: string; filters: object }, criteria: { startDate: Date; endDate: Date; column: string; filters: object },
) { ) {
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const { startDate, endDate, column, filters = {} } = criteria; const { startDate, endDate, column, filters = {} } = criteria;
const { toUuid, parseFilters, rawQuery } = prisma; const { toUuid, parseFilters, rawQuery } = prisma;
const params: any = [websiteId, resetDate, startDate, endDate]; const params: any = [websiteId, resetDate, startDate, endDate];
@ -53,9 +53,9 @@ async function clickhouseQuery(
data: { startDate: Date; endDate: Date; column: string; filters: object }, data: { startDate: Date; endDate: Date; column: string; filters: object },
) { ) {
const { startDate, endDate, column, filters = {} } = data; const { startDate, endDate, column, filters = {} } = data;
const { getDateFormat, parseFilters, getBetweenDates, rawQuery } = clickhouse; const { getDateFormat, parseFilters, rawQuery } = clickhouse;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params = { websiteId }; const params = { websiteId };
const { filterQuery } = parseFilters(filters, params); const { filterQuery } = parseFilters(filters, params);
@ -65,7 +65,7 @@ async function clickhouseQuery(
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and event_type = ${EVENT_TYPE.pageView} and event_type = ${EVENT_TYPE.pageView}
and created_at >= ${getDateFormat(resetDate)} and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)} and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
${filterQuery} ${filterQuery}
group by x group by x
order by y desc order by y desc

View File

@ -1,5 +1,5 @@
import { DATA_TYPE } from 'lib/constants'; import { DATA_TYPE } from 'lib/constants';
import { uuid } from 'lib/crypto'; import { uuid } from 'next-basics';
import { flattenJSON } from 'lib/dynamicData'; import { flattenJSON } from 'lib/dynamicData';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { DynamicData } from 'lib/types'; import { DynamicData } from 'lib/types';

View File

@ -1,6 +1,5 @@
import { md5 } from 'lib/crypto'; import { md5 } from 'next-basics';
import { getSessions } from '../session/getSessions'; import { getSessions, getEvents } from 'queries';
import { getEvents } from '../event/getEvents';
import { EVENT_TYPE } from 'lib/constants'; import { EVENT_TYPE } from 'lib/constants';
export async function getRealtimeData(websiteId, time) { export async function getRealtimeData(websiteId, time) {
@ -20,7 +19,7 @@ export async function getRealtimeData(websiteId, time) {
}; };
return { return {
pageviews: decorate('pageview', pageviews), pageviews: decorate('pageviews', pageviews),
sessions: decorate('session', sessions), sessions: decorate('session', sessions),
events: decorate('event', events), events: decorate('event', events),
timestamp: Date.now(), timestamp: Date.now(),

View File

@ -1,7 +1,7 @@
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import { DEFAULT_CREATED_AT, EVENT_TYPE } from 'lib/constants'; import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
import { loadWebsite } from 'lib/query'; import { loadWebsite } from 'lib/query';
export async function getWebsiteStats( export async function getWebsiteStats(
@ -23,7 +23,7 @@ async function relationalQuery(
const { startDate, endDate, filters = {} } = criteria; const { startDate, endDate, filters = {} } = criteria;
const { toUuid, getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma; const { toUuid, getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params: any = [websiteId, resetDate, startDate, endDate]; const params: any = [websiteId, resetDate, startDate, endDate];
const { filterQuery, joinSession } = parseFilters(filters, params); const { filterQuery, joinSession } = parseFilters(filters, params);
@ -57,9 +57,9 @@ async function clickhouseQuery(
criteria: { startDate: Date; endDate: Date; filters: object }, criteria: { startDate: Date; endDate: Date; filters: object },
) { ) {
const { startDate, endDate, filters = {} } = criteria; const { startDate, endDate, filters = {} } = criteria;
const { rawQuery, getDateFormat, getDateQuery, getBetweenDates, parseFilters } = clickhouse; const { rawQuery, getDateFormat, getDateQuery, parseFilters } = clickhouse;
const website = await loadWebsite(websiteId); const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params = { websiteId }; const params = { websiteId };
const { filterQuery } = parseFilters(filters, params); const { filterQuery } = parseFilters(filters, params);
@ -79,7 +79,7 @@ async function clickhouseQuery(
where event_type = ${EVENT_TYPE.pageView} where event_type = ${EVENT_TYPE.pageView}
and website_id = {websiteId:UUID} and website_id = {websiteId:UUID}
and created_at >= ${getDateFormat(resetDate)} and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)} and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
${filterQuery} ${filterQuery}
group by session_id, time_series group by session_id, time_series
) t;`, ) t;`,

View File

@ -3,19 +3,21 @@ export * from './admin/teamUser';
export * from './admin/user'; export * from './admin/user';
export * from './admin/report'; export * from './admin/report';
export * from './admin/website'; export * from './admin/website';
export * from './analytics/event/getEventMetrics'; export * from './analytics/events/getEventMetrics';
export * from './analytics/event/getEventUsage'; export * from './analytics/events/getEventUsage';
export * from './analytics/event/getEvents'; export * from './analytics/events/getEvents';
export * from './analytics/eventData/getEventDataEvents';
export * from './analytics/eventData/getEventDataFields'; export * from './analytics/eventData/getEventDataFields';
export * from './analytics/eventData/getEventDataUsage'; export * from './analytics/eventData/getEventDataUsage';
export * from './analytics/event/saveEvent'; export * from './analytics/events/saveEvent';
export * from './analytics/pageview/getPageviewFunnel'; export * from './analytics/reports/getFunnel';
export * from './analytics/pageview/getPageviewMetrics'; export * from './analytics/reports/getInsights';
export * from './analytics/pageview/getPageviewStats'; export * from './analytics/pageviews/getPageviewMetrics';
export * from './analytics/session/createSession'; export * from './analytics/pageviews/getPageviewStats';
export * from './analytics/session/getSession'; export * from './analytics/sessions/createSession';
export * from './analytics/session/getSessionMetrics'; export * from './analytics/sessions/getSession';
export * from './analytics/session/getSessions'; export * from './analytics/sessions/getSessionMetrics';
export * from './analytics/sessions/getSessions';
export * from './analytics/stats/getActiveVisitors'; export * from './analytics/stats/getActiveVisitors';
export * from './analytics/stats/getRealtimeData'; export * from './analytics/stats/getRealtimeData';
export * from './analytics/stats/getWebsiteStats'; export * from './analytics/stats/getWebsiteStats';

View File

@ -1,32 +1,16 @@
const fs = require('fs-extra');
const path = require('path');
const os = require('os'); const os = require('os');
const isCI = require('is-ci'); const isCI = require('is-ci');
const pkg = require('../package.json'); const pkg = require('../package.json');
const dest = path.resolve(__dirname, '../.next/cache/umami.json');
const url = 'https://api.umami.is/v1/telemetry'; const url = 'https://api.umami.is/v1/telemetry';
async function sendTelemetry(action) { async function sendTelemetry(type) {
let json = {};
try {
json = await fs.readJSON(dest);
} catch {
// Ignore
}
try {
await fs.writeJSON(dest, { version: pkg.version });
} catch {
// Ignore
}
const { default: isDocker } = await import('is-docker'); const { default: isDocker } = await import('is-docker');
const { default: fetch } = await import('node-fetch'); const { default: fetch } = await import('node-fetch');
const payload = { const data = {
action, type,
payload: {
version: pkg.version, version: pkg.version,
node: process.version, node: process.version,
platform: os.platform(), platform: os.platform(),
@ -34,7 +18,7 @@ async function sendTelemetry(action) {
os: `${os.type()} (${os.version()})`, os: `${os.type()} (${os.version()})`,
isDocker: isDocker(), isDocker: isDocker(),
isCi: isCI, isCi: isCI,
prevVersion: json.version, },
}; };
try { try {
@ -44,7 +28,7 @@ async function sendTelemetry(action) {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify(payload), body: JSON.stringify(data),
}); });
} catch { } catch {
// Ignore // Ignore

View File

@ -6260,14 +6260,15 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw== integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==
next-basics@^0.31.0: next-basics@^0.33.0:
version "0.31.0" version "0.33.0"
resolved "https://registry.npmjs.org/next-basics/-/next-basics-0.31.0.tgz" resolved "https://registry.yarnpkg.com/next-basics/-/next-basics-0.33.0.tgz#da4736dbaa6b8461ae1fe40de4cde20cbe7744e6"
integrity sha512-uAum7v4DQ37IdqKdmLTFLMyN+ir7FNMeMHQcTd1RQ14sm/fO/tntadlsXulwj/A5xs3gmnW/gfGkLX7VnNjreg== integrity sha512-pKynp6/zMboQk/mlzyOZgfFsYShKpieu3XoQM5EmTL311mP2KkcW59hiVHloLR7W6zaaf/ONWzi0L1iw05Qfag==
dependencies: dependencies:
bcryptjs "^2.4.3" bcryptjs "^2.4.3"
jsonwebtoken "^9.0.0" jsonwebtoken "^9.0.0"
pure-rand "^6.0.2" pure-rand "^6.0.2"
uuid "^9.0.0"
next@13.3.1: next@13.3.1:
version "13.3.1" version "13.3.1"
@ -8967,6 +8968,11 @@ uuid@^8.3.2:
resolved "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz" resolved "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
uuid@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5"
integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==
v8-compile-cache-lib@^3.0.1: v8-compile-cache-lib@^3.0.1:
version "3.0.1" version "3.0.1"
resolved "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz" resolved "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz"