mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 22:57:12 +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
|
|
@ -18,7 +18,7 @@ async function relationalQuery(where: Prisma.SessionWhereUniqueInput) {
|
|||
|
||||
async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
||||
const { rawQuery, findFirst } = clickhouse;
|
||||
const params = [sessionId];
|
||||
const params = { sessionId };
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
|
|
@ -33,7 +33,7 @@ async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
|||
language,
|
||||
country
|
||||
from event
|
||||
where session_id = $1
|
||||
where session_id = {sessionId:UUID}
|
||||
limit 1`,
|
||||
params,
|
||||
).then(result => findFirst(result));
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import prisma from 'lib/prisma';
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
|
||||
export async function getSessionMetrics(
|
||||
...args: [
|
||||
|
|
@ -50,15 +51,15 @@ async function clickhouseQuery(
|
|||
const { startDate, endDate, field, filters = {} } = data;
|
||||
const { parseFilters, getBetweenDates, rawQuery } = 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(
|
||||
`select ${field} x, count(distinct session_id) y
|
||||
from event as x
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = 1
|
||||
where website_id = {websiteId:UUID}
|
||||
and rev_id = {revId:UInt32}
|
||||
and event_type = ${EVENT_TYPE.pageView}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${filterQuery}
|
||||
group by x
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@ import prisma from 'lib/prisma';
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
|
||||
|
||||
export async function getSessions(...args) {
|
||||
export async function getSessions(...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.session.findMany({
|
||||
where: {
|
||||
...(websites && websites.length > 0
|
||||
|
|
@ -26,8 +26,8 @@ async function relationalQuery(websites, startAt) {
|
|||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, startAt) {
|
||||
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat } = clickhouse;
|
||||
async function clickhouseQuery(websites: string[], startAt: Date) {
|
||||
const { rawQuery } = clickhouse;
|
||||
|
||||
return rawQuery(
|
||||
`select distinct
|
||||
|
|
@ -42,11 +42,11 @@ async function clickhouseQuery(websites, startAt) {
|
|||
language,
|
||||
country
|
||||
from event
|
||||
where ${
|
||||
websites && websites.length > 0
|
||||
? `website_id in (${getCommaSeparatedStringFormat(websites)})`
|
||||
: '0 = 0'
|
||||
}
|
||||
and created_at >= ${getDateFormat(startAt)}`,
|
||||
where ${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