mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 15:47:13 +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
|
|
@ -70,19 +70,19 @@ async function clickhouseQuery(
|
|||
const { startDate, endDate, column, filters = {}, type } = data;
|
||||
const { rawQuery, parseFilters, getBetweenDates } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [
|
||||
const params = {
|
||||
websiteId,
|
||||
website?.revId || 0,
|
||||
type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
];
|
||||
revId: website?.revId || 0,
|
||||
eventType: type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
};
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from event
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = $3
|
||||
where website_id = {websiteId:UUID}
|
||||
and rev_id = {revId:UInt32}
|
||||
and event_type = {eventType:UInt32}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${filterQuery}
|
||||
group by x
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ async function clickhouseQuery(
|
|||
const { startDate, endDate, timezone = 'UTC', unit = 'day', count = '*', filters = {} } = data;
|
||||
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery, getBetweenDates } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const params = [websiteId, website?.revId || 0];
|
||||
const params = { websiteId, revId: website?.revId || 0 };
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
|
|
@ -90,8 +90,8 @@ async function clickhouseQuery(
|
|||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? 'distinct session_id' : 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.pageView}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${filterQuery}
|
||||
|
|
|
|||
|
|
@ -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 async function getPageviews(...args) {
|
||||
export async function getPageviews(...args: [websites: string[], startAt: Date]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, startAt) {
|
||||
async function relationalQuery(websites: string[], startAt: Date) {
|
||||
return prisma.client.pageview.findMany({
|
||||
where: {
|
||||
websiteId: {
|
||||
|
|
@ -22,8 +23,8 @@ async function relationalQuery(websites, startAt) {
|
|||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, startAt) {
|
||||
const { rawQuery, getCommaSeparatedStringFormat } = clickhouse;
|
||||
async function clickhouseQuery(websites: string[], startAt: Date) {
|
||||
const { rawQuery } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
|
|
@ -32,12 +33,12 @@ async function clickhouseQuery(websites, startAt) {
|
|||
created_at,
|
||||
url
|
||||
from event
|
||||
where event_type = 1
|
||||
and ${
|
||||
websites && websites.length > 0
|
||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||
: '0 = 0'
|
||||
}
|
||||
and created_at >= ${clickhouse.getDateFormat(startAt)}`,
|
||||
where event_type = ${EVENT_TYPE.pageView}
|
||||
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