Merge branch 'dev' of https://github.com/umami-software/umami into feat/um-366-event-data-migration

This commit is contained in:
Francis Cao 2023-07-11 12:05:05 -07:00
commit 3aa7565bf4
17 changed files with 639 additions and 699 deletions

View file

@ -1,94 +0,0 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { WebsiteEventDataMetric } from 'lib/types';
import { loadWebsite } from 'lib/query';
import { DEFAULT_CREATED_AT } from 'lib/constants';
export async function getEventData(
...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
): Promise<WebsiteEventDataMetric[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
const { toUuid, rawQuery } = prisma;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
if (field) {
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
limit 100
`,
[websiteId, field, resetDate, startDate, endDate] as any,
);
}
return rawQuery(
`select
event_key as field,
count(*) as total
from event_data
where website_id = $1${toUuid()}
and created_at >= $2
and created_at between $3 and $4
group by event_key
order by 2 desc, 1 asc
limit 100
`,
[websiteId, resetDate, startDate, endDate] as any,
);
}
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
if (field) {
return rawQuery(
`select
event_key as field,
string_value as value,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and event_key = {field:String}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
limit 100
`,
{ websiteId, field },
);
}
return rawQuery(
`select
event_key as field,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by event_key
order by 2 desc, 1 asc
limit 100
`,
{ websiteId },
);
}

View file

@ -1,51 +1,96 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import { WebsiteEventDataMetric } from 'lib/types';
import { WebsiteEventDataFields } from 'lib/types';
import { loadWebsite } from 'lib/query';
import { DEFAULT_CREATED_AT } from 'lib/constants';
export async function getEventDataFields(
...args: [websiteId: string, startDate: Date, endDate: Date]
): Promise<WebsiteEventDataMetric[]> {
...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
): Promise<WebsiteEventDataFields[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websiteId: string, startDate: Date, endDate: Date) {
async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
const { toUuid, rawQuery } = prisma;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
const params: any = [websiteId, resetDate, startDate, endDate];
if (field) {
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
limit 100
`,
[websiteId, field, resetDate, startDate, endDate] as any,
);
}
return rawQuery(
`select
distinct event_key as eventKey, data_type as eventDataType
from event_data
where website_id = $1${toUuid()}
and created_at >= $2
and created_at between $3 and $4
order by event_key asc`,
params,
event_key as field,
data_type as type,
count(*) as total
from event_data
where website_id = $1${toUuid()}
and created_at >= $2
and created_at between $3 and $4
group by event_key, data_type
order by 3 desc, 2 asc, 1 asc
limit 100
`,
[websiteId, resetDate, startDate, endDate] as any,
);
}
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date) {
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
const params = { websiteId };
if (field) {
return rawQuery(
`select
event_key as field,
string_value as value,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and event_key = {field:String}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
limit 100
`,
{ websiteId, field },
);
}
return rawQuery(
`select
distinct event_key as eventKey, data_type as eventDataType
from event_data
where website_id = {websiteId:UUID}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
order by event_key asc`,
params,
event_key as field,
data_type as type,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by event_key, data_type
order by 3 desc, 2 asc, 1 asc
limit 100
`,
{ websiteId },
);
}

View file

@ -91,7 +91,7 @@ async function clickhouseQuery(
count(*) AS count
FROM (
SELECT session_id,
windowFunnel({window:UInt32})
windowFunnel({window:UInt32}, 'strict_order')
(
created_at
${columnsQuery}

View file

@ -6,7 +6,6 @@ export * from './admin/website';
export * from './analytics/event/getEventMetrics';
export * from './analytics/event/getEventUsage';
export * from './analytics/event/getEvents';
export * from './analytics/eventData/getEventData';
export * from './analytics/eventData/getEventDataFields';
export * from './analytics/eventData/getEventDataUsage';
export * from './analytics/event/saveEvent';