mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 22:57:12 +01:00
Refactored queries.
This commit is contained in:
parent
e4bd314bd6
commit
4bd3ef8e12
19 changed files with 330 additions and 408 deletions
|
|
@ -1,4 +1,3 @@
|
|||
import { buildSql } from 'lib/sql';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
|
|
@ -26,47 +25,52 @@ async function relationalQuery(
|
|||
endDate: Date,
|
||||
filters: { field?: string; event?: string },
|
||||
) {
|
||||
const { toUuid, rawQuery } = prisma;
|
||||
const { 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,
|
||||
);
|
||||
}
|
||||
|
||||
if (event) {
|
||||
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
|
||||
`
|
||||
select
|
||||
we.event_name as event,
|
||||
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 = {{websiteId:uuid}}
|
||||
and ed.event_key = {{field}}
|
||||
and ed.created_at >= {{resetDate}}
|
||||
and ed.created_at between {{startDate}} and {{endDate}}
|
||||
and we.event_name = {{event}}
|
||||
group by ed.event_key, ed.string_value
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
`,
|
||||
[websiteId, field, resetDate, startDate, endDate] as any,
|
||||
{ ...filters, websiteId, resetDate, startDate, endDate },
|
||||
);
|
||||
}
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
we.event_name as event,
|
||||
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 = {{websiteId::uuid}}
|
||||
and ed.event_key = {{field}}
|
||||
and ed.created_at >= {{resetDate}}
|
||||
and ed.created_at between {{startDate}} and {{endDate}}
|
||||
group by we.event_name, ed.event_key, ed.string_value
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
`,
|
||||
{ websiteId, field, resetDate, startDate, endDate },
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
@ -82,12 +86,13 @@ async function clickhouseQuery(
|
|||
|
||||
if (event) {
|
||||
return rawQuery(
|
||||
`select
|
||||
event_name as event,
|
||||
event_key as field,
|
||||
data_type as type,
|
||||
string_value as value,
|
||||
count(*) as total
|
||||
`
|
||||
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}
|
||||
|
|
@ -95,24 +100,26 @@ async function clickhouseQuery(
|
|||
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 },
|
||||
limit 100
|
||||
`,
|
||||
{ ...filters, websiteId, resetDate, startDate, endDate },
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
`
|
||||
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 },
|
||||
);
|
||||
|
|
|
|||
|
|
@ -15,82 +15,87 @@ export async function getEventDataFields(
|
|||
}
|
||||
|
||||
async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
|
||||
const { toUuid, rawQuery } = prisma;
|
||||
const { rawQuery } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
|
||||
|
||||
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
|
||||
`
|
||||
select
|
||||
event_key as field,
|
||||
string_value as value,
|
||||
count(*) as total
|
||||
from event_data
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and event_key = {{field}}
|
||||
and created_at >= {{resetDate}}
|
||||
and created_at between {{startDate}} and {{endDate}}
|
||||
group by event_key, string_value
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
[websiteId, field, resetDate, startDate, endDate] as any,
|
||||
{ websiteId, field, resetDate, startDate, endDate },
|
||||
);
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
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
|
||||
`
|
||||
select
|
||||
event_key as field,
|
||||
data_type as type,
|
||||
count(*) as total
|
||||
from event_data
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and created_at >= {{resetDate}}
|
||||
and created_at between {{startDate}} and {{endDate}}
|
||||
group by event_key, data_type
|
||||
order by 3 desc, 2 asc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
[websiteId, resetDate, startDate, endDate] as any,
|
||||
{ websiteId, resetDate, startDate, endDate },
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
|
||||
const { rawQuery, getDateFormat } = clickhouse;
|
||||
const { rawQuery } = clickhouse;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
|
||||
|
||||
if (field) {
|
||||
return rawQuery(
|
||||
`select
|
||||
`
|
||||
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 created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
|
||||
group by event_key, string_value
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId, field },
|
||||
from event_data
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_key = {field:String}
|
||||
and created_at >= {resetDate:DateTime}
|
||||
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
||||
group by event_key, string_value
|
||||
order by 3 desc, 2 desc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId, field, resetDate, startDate, endDate },
|
||||
);
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
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 created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
|
||||
group by event_key, data_type
|
||||
order by 3 desc, 2 asc, 1 asc
|
||||
limit 100
|
||||
`
|
||||
select
|
||||
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
|
||||
order by 3 desc, 2 asc, 1 asc
|
||||
limit 100
|
||||
`,
|
||||
{ websiteId },
|
||||
{ websiteId, resetDate, startDate, endDate },
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,26 @@
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, PRISMA, runQuery, notImplemented } from 'lib/db';
|
||||
|
||||
export function getEventDataUsage(...args: [websiteIds: string[], startDate: Date, endDate: Date]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[PRISMA]: notImplemented,
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
function relationalQuery(websiteIds: string[], startDate: Date, endDate: Date) {
|
||||
throw new Error('Not implemented.');
|
||||
}
|
||||
|
||||
function clickhouseQuery(websiteIds: string[], startDate: Date, endDate: Date) {
|
||||
const { rawQuery } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
website_id as websiteId,
|
||||
count(*) as count
|
||||
`
|
||||
select
|
||||
website_id as websiteId,
|
||||
count(*) as count
|
||||
from event_data
|
||||
where created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and website_id in {websiteIds:Array(UUID)}
|
||||
group by website_id`,
|
||||
and website_id in {websiteIds:Array(UUID)}
|
||||
group by website_id
|
||||
`,
|
||||
{
|
||||
websiteIds,
|
||||
startDate,
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ async function relationalQuery(data: {
|
|||
|
||||
const jsonKeys = flattenJSON(eventData);
|
||||
|
||||
//id, websiteEventId, eventStringValue
|
||||
// id, websiteEventId, eventStringValue
|
||||
const flattendData = jsonKeys.map(a => ({
|
||||
id: uuid(),
|
||||
websiteEventId: eventId,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue