mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 00:27:11 +01:00
Database refactoring.
This commit is contained in:
parent
bb184dc2cc
commit
467c7f289f
37 changed files with 566 additions and 591 deletions
|
|
@ -1,16 +1,16 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { parseFilters, rawQuery } from 'lib/relational';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
|
||||
export async function getSessionMetrics(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[RELATIONAL]: () => relationalQuery(...args),
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, field, filters = {}) {
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
|
|
@ -20,8 +20,7 @@ async function relationalQuery(website_id, start_at, end_at, field, filters = {}
|
|||
);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${field} x, count(*) y
|
||||
`select ${field} x, count(*) y
|
||||
from session as x
|
||||
where x.session_id in (
|
||||
select pageview.session_id
|
||||
|
|
@ -33,15 +32,15 @@ async function relationalQuery(website_id, start_at, end_at, field, filters = {}
|
|||
${sessionQuery}
|
||||
)
|
||||
group by 1
|
||||
order by 2 desc
|
||||
`,
|
||||
order by 2 desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, start_at, end_at, field, filters = {}) {
|
||||
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = clickhouse.parseFilters(
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters(
|
||||
'pageview',
|
||||
null,
|
||||
filters,
|
||||
|
|
@ -49,22 +48,20 @@ async function clickhouseQuery(website_id, start_at, end_at, field, filters = {}
|
|||
'session_uuid',
|
||||
);
|
||||
|
||||
return clickhouse.rawQuery(
|
||||
`
|
||||
select ${field} x, count(*) y
|
||||
return rawQuery(
|
||||
`select ${field} x, count(*) y
|
||||
from session as x
|
||||
where x.session_uuid in (
|
||||
select pageview.session_uuid
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and ${clickhouse.getBetweenDates('pageview.created_at', start_at, end_at)}
|
||||
and ${getBetweenDates('pageview.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
)
|
||||
group by x
|
||||
order by y desc
|
||||
`,
|
||||
order by y desc`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue