mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 07:37:11 +01:00
Re-write CH queries to use query params.
This commit is contained in:
parent
b4bd988e4e
commit
1eb9e10d94
12 changed files with 81 additions and 100 deletions
|
|
@ -72,14 +72,14 @@ async function clickhouseQuery(
|
|||
const { rawQuery, getBetweenDates, getEventDataColumnsQuery, getEventDataFilterQuery } =
|
||||
clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const params = { websiteId, revId: website?.revId || 0 };
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
${getEventDataColumnsQuery('event_data', columns)}
|
||||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
where website_id = {websiteId:UUID}
|
||||
and rev_id = {revId:UInt32}
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
${eventName ? `and eventName = ${eventName}` : ''}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ async function clickhouseQuery(
|
|||
) {
|
||||
const { rawQuery, getDateQuery, getBetweenDates, getFilterQuery } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const params = { websiteId, revId: website?.revId || 0 };
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
|
|
@ -93,8 +93,8 @@ async function clickhouseQuery(
|
|||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
where website_id = {websiteId:UUID}
|
||||
and rev_id = {revId:UInt32}
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${getFilterQuery(filters, params)}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,16 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
|
||||
export function getEvents(...args) {
|
||||
export function getEvents(...args: [websites: string[], startAt: Date]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
function relationalQuery(websites, startAt) {
|
||||
function relationalQuery(websites: string[], startAt: Date) {
|
||||
return prisma.client.event.findMany({
|
||||
where: {
|
||||
websiteId: {
|
||||
|
|
@ -22,8 +23,8 @@ function relationalQuery(websites, startAt) {
|
|||
});
|
||||
}
|
||||
|
||||
function clickhouseQuery(websites, startAt) {
|
||||
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;
|
||||
function clickhouseQuery(websites: string[], startAt: Date) {
|
||||
const { rawQuery } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
|
|
@ -34,12 +35,12 @@ function clickhouseQuery(websites, startAt) {
|
|||
url,
|
||||
event_name
|
||||
from event
|
||||
where event_name != ''
|
||||
and ${
|
||||
websites && websites.length > 0
|
||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||
: '0 = 0'
|
||||
}
|
||||
and created_at >= ${getDateFormat(startAt)}`,
|
||||
where event_type = ${EVENT_TYPE.customEvent}
|
||||
and ${websites && websites.length > 0 ? `website_id in {websites:Array(UUID)}` : '0 = 0'}
|
||||
and created_at >= {startAt:DateTime('UTC')}`,
|
||||
{
|
||||
websites,
|
||||
startAt,
|
||||
},
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue