mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37: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,17 +1,23 @@
|
|||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import {
|
||||
rawQueryClickhouse,
|
||||
getBetweenDatesClickhouse,
|
||||
getDateQuery,
|
||||
getDateQueryClickhouse,
|
||||
getDateStringQuery,
|
||||
getFilterQuery,
|
||||
rawQuery,
|
||||
runAnalyticsQuery,
|
||||
clickhouse,
|
||||
} from 'lib/db';
|
||||
|
||||
export function getEventMetrics(...args) {
|
||||
return runAnalyticsQuery(relationalQuery(...args), clickhouseQuery(...args));
|
||||
export async function getEventMetrics(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
function relationalQuery(
|
||||
async function relationalQuery(
|
||||
website_id,
|
||||
start_at,
|
||||
end_at,
|
||||
|
|
@ -38,28 +44,28 @@ function relationalQuery(
|
|||
);
|
||||
}
|
||||
|
||||
function clickhouseQuery(
|
||||
async function clickhouseQuery(
|
||||
website_id,
|
||||
start_at,
|
||||
end_at,
|
||||
timezone = 'utc',
|
||||
timezone = 'UTC',
|
||||
unit = 'day',
|
||||
filters = {},
|
||||
) {
|
||||
const params = [website_id, start_at, end_at];
|
||||
const params = [website_id];
|
||||
|
||||
return clickhouse.query(
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select
|
||||
event_value x,
|
||||
${getDateStringQuery(getDateQuery('created_at', unit, timezone), unit)} t,
|
||||
${getDateQueryClickhouse('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from event
|
||||
where website_id=$1
|
||||
and created_at between $2 and $3
|
||||
${getFilterQuery('event', filters, params)}
|
||||
group by 1, 2
|
||||
order by 2
|
||||
where website_id= $1
|
||||
and ${getBetweenDatesClickhouse('created_at', start_at, end_at)}
|
||||
${getFilterQuery('event', filters, params)}
|
||||
group by x, t
|
||||
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 getEvents(websites, start_at) {
|
||||
export function getEvents(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
function relationalQuery(websites, start_at) {
|
||||
return runQuery(
|
||||
prisma.event.findMany({
|
||||
where: {
|
||||
|
|
@ -16,3 +30,20 @@ export async function getEvents(websites, start_at) {
|
|||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function clickhouseQuery(websites, start_at) {
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select
|
||||
event_id,
|
||||
website_id,
|
||||
session_id,
|
||||
created_at,
|
||||
url,
|
||||
event_type
|
||||
from event
|
||||
where website_id in (${websites.join[',']}
|
||||
and created_at >= ${getDateFormatClickhouse(start_at)})
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)})
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,14 @@
|
|||
import { prisma, runQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { rawQueryClickhouse, prisma, runAnalyticsQuery, runQuery } from 'lib/db';
|
||||
|
||||
export async function getSessionByUuid(session_uuid) {
|
||||
export async function getSessionByUuid(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(session_uuid) {
|
||||
return runQuery(
|
||||
prisma.session.findUnique({
|
||||
where: {
|
||||
|
|
@ -9,3 +17,27 @@ export async function getSessionByUuid(session_uuid) {
|
|||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(session_uuid) {
|
||||
const params = [session_uuid];
|
||||
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select
|
||||
session_id,
|
||||
session_uuid,
|
||||
website_id,
|
||||
created_at,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
"language",
|
||||
country
|
||||
from session
|
||||
where session_id = $1
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,20 @@
|
|||
import { parseFilters, rawQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import {
|
||||
getBetweenDatesClickhouse,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
rawQueryClickhouse,
|
||||
runAnalyticsQuery,
|
||||
} from 'lib/db';
|
||||
|
||||
export function getSessionMetrics(website_id, start_at, end_at, field, filters = {}) {
|
||||
export async function getSessionMetrics(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, field, filters = {}) {
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
||||
|
||||
|
|
@ -23,3 +37,27 @@ export function getSessionMetrics(website_id, start_at, end_at, field, filters =
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, start_at, end_at, field, filters = {}) {
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
||||
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select ${field} x, count(*) y
|
||||
from session as x
|
||||
where x.session_id in (
|
||||
select pageview.session_id
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and ${getBetweenDatesClickhouse('pageview.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
)
|
||||
group by x
|
||||
order by y desc
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,20 @@
|
|||
import { prisma, runQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import {
|
||||
getDateFormatClickhouse,
|
||||
prisma,
|
||||
rawQueryClickhouse,
|
||||
runAnalyticsQuery,
|
||||
runQuery,
|
||||
} from 'lib/db';
|
||||
|
||||
export async function getSessions(websites, start_at) {
|
||||
export async function getSessions(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websites, start_at) {
|
||||
return runQuery(
|
||||
prisma.session.findMany({
|
||||
where: {
|
||||
|
|
@ -16,3 +30,25 @@ export async function getSessions(websites, start_at) {
|
|||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websites, start_at) {
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select
|
||||
session_id,
|
||||
session_uuid,
|
||||
website_id,
|
||||
created_at,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
"language",
|
||||
country
|
||||
from session
|
||||
where website_id in (${websites.join[',']}
|
||||
and created_at >= ${getDateFormatClickhouse(start_at)})
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,15 @@
|
|||
import { rawQuery } from 'lib/db';
|
||||
import { subMinutes } from 'date-fns';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import { getDateFormatClickhouse, rawQuery, rawQueryClickhouse, runAnalyticsQuery } from 'lib/db';
|
||||
|
||||
export function getActiveVisitors(website_id) {
|
||||
export async function getActiveVisitors(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id) {
|
||||
const date = subMinutes(new Date(), 5);
|
||||
const params = [website_id, date];
|
||||
|
||||
|
|
@ -9,9 +17,23 @@ export function getActiveVisitors(website_id) {
|
|||
`
|
||||
select count(distinct session_id) x
|
||||
from pageview
|
||||
where website_id=$1
|
||||
where website_id = $1
|
||||
and created_at >= $2
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id) {
|
||||
const params = [website_id];
|
||||
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select count(distinct session_id) x
|
||||
from pageview
|
||||
where website_id = $1
|
||||
and created_at >= ${getDateFormatClickhouse(subMinutes(new Date(), 5))}
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,23 @@
|
|||
import { parseFilters, rawQuery, getDateQuery, getTimestampInterval } from 'lib/db';
|
||||
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
||||
import {
|
||||
getDateQuery,
|
||||
getBetweenDatesClickhouse,
|
||||
getDateQueryClickhouse,
|
||||
getTimestampInterval,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
rawQueryClickhouse,
|
||||
runAnalyticsQuery,
|
||||
} from 'lib/db';
|
||||
|
||||
export function getWebsiteStats(website_id, start_at, end_at, filters = {}) {
|
||||
export async function getWebsiteStats(...args) {
|
||||
return runAnalyticsQuery({
|
||||
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
||||
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(website_id, start_at, end_at, filters = {}) {
|
||||
const params = [website_id, start_at, end_at];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
||||
|
||||
|
|
@ -9,21 +26,57 @@ export function getWebsiteStats(website_id, start_at, end_at, filters = {}) {
|
|||
select sum(t.c) as "pageviews",
|
||||
count(distinct t.session_id) as "uniques",
|
||||
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
||||
sum(case when m2 < m1 + interval '1 hour' then ${getTimestampInterval(
|
||||
'm2',
|
||||
'm1',
|
||||
)} else 0 end) as "totaltime"
|
||||
sum(t.time) as "totaltime"
|
||||
from (
|
||||
select pageview.session_id,
|
||||
${getDateQuery('pageview.created_at', 'hour')},
|
||||
count(*) c,
|
||||
${getTimestampInterval('pageview.created_at')} as "time"
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and pageview.created_at between $2 and $3
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by 1, 2
|
||||
select
|
||||
pageview.session_id,
|
||||
${getDateQuery('pageview.created_at', 'hour')},
|
||||
count(*) c,
|
||||
min(created_at) m1,
|
||||
max(created_at) m2
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id=$1
|
||||
and pageview.created_at between $2 and $3
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by 1, 2
|
||||
) t
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(website_id, start_at, end_at, filters = {}) {
|
||||
const params = [website_id];
|
||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
||||
|
||||
return rawQueryClickhouse(
|
||||
`
|
||||
select
|
||||
sum(t.c) as "pageviews",
|
||||
count(distinct t.session_id) as "uniques",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(if(max_time < min_time + interval 1 hour, max_time-min_time, 0)) as "totaltime"
|
||||
from (
|
||||
select pageview.session_id,
|
||||
${getDateQueryClickhouse('pageview.created_at', 'day')} time_series,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from pageview
|
||||
${joinSession}
|
||||
where pageview.website_id = $1
|
||||
and ${getBetweenDatesClickhouse('pageview.created_at', start_at, end_at)}
|
||||
${pageviewQuery}
|
||||
${sessionQuery}
|
||||
group by pageview.session_id, time_series
|
||||
) t;
|
||||
`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue