mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 00:27:11 +01:00
convert analytics queries
This commit is contained in:
parent
4f12933f42
commit
6ea2282f82
12 changed files with 470 additions and 52 deletions
|
|
@ -1,6 +1,20 @@
|
|||
import { parseFilters, rawQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import {
|
||||
rawQueryClickhouse,
|
||||
runAnalyticsQuery,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
getBetweenDatesClickhouse,
|
||||
} from 'lib/db';
|
||||
|
||||
export function getPageviewMetrics(website_id, start_at, end_at, field, table, filters = {}) {
|
||||
export async function getPageviewMetrics(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, field, table, filters = {}) {
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
|
|
@ -24,3 +38,28 @@ export function getPageviewMetrics(website_id, start_at, end_at, field, table, f
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, start_at, end_at, field, table, filters = {}) {
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
|
||||
table,
|
||||
filters,
|
||||
params,
|
||||
);
|
||||
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select ${field} x, count(*) y
|
||||
from ${table}
|
||||
${joinSession}
|
||||
where ${table}.website_id= $1
|
||||
and ${getBetweenDatesClickhouse(table + '.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${joinSession && sessionQuery}
|
||||
${eventQuery}
|
||||
group by x
|
||||
order by y desc
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,24 @@
|
|||
import { parseFilters, rawQuery, getDateQuery, getDateStringQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import {
|
||||
rawQueryClickhouse,
|
||||
getBetweenDatesClickhouse,
|
||||
getDateQuery,
|
||||
getDateQueryClickhouse,
|
||||
getDateStringQuery,
|
||||
getDateStringQueryClickhouse,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
runAnalyticsQuery,
|
||||
} from 'lib/db';
|
||||
|
||||
export function getPageviewStats(
|
||||
export async function getPageviewStats(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
website_id,
|
||||
start_at,
|
||||
end_at,
|
||||
|
|
@ -32,3 +50,37 @@ export function getPageviewStats(
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
website_id,
|
||||
start_at,
|
||||
end_at,
|
||||
timezone = 'UTC',
|
||||
unit = 'day',
|
||||
count = '*',
|
||||
filters = {},
|
||||
) {
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
||||
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select
|
||||
${getDateStringQueryClickhouse('g.t', unit)} as t,
|
||||
g.y as y
|
||||
from
|
||||
(select
|
||||
${getDateQueryClickhouse('created_at', unit, timezone)} t,
|
||||
count(${count}) y
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id= $1
|
||||
and ${getBetweenDatesClickhouse('pageview.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by t) g
|
||||
order by t
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,20 @@
|
|||
import { prisma, runQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import {
|
||||
rawQueryClickhouse,
|
||||
getDateFormatClickhouse,
|
||||
prisma,
|
||||
runAnalyticsQuery,
|
||||
runQuery,
|
||||
} from 'lib/db';
|
||||
|
||||
export async function getPageviews(websites, start_at) {
|
||||
export async function getPageviews(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return runQuery(
|
||||
prisma.pageview.findMany({
|
||||
where: {
|
||||
|
|
@ -16,3 +30,19 @@ export async function getPageviews(websites, start_at) {
|
|||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select
|
||||
view_id,
|
||||
website_id,
|
||||
session_id,
|
||||
created_at,
|
||||
url
|
||||
from pageview
|
||||
where website_id in (${websites.join[',']}
|
||||
and created_at >= ${getDateFormatClickhouse(start_at)})
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue