umami/queries/analytics/session/getSessionMetrics.js
2022-08-25 22:23:19 -07:00

71 lines
1.7 KiB
JavaScript

import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
import clickhouse from 'lib/clickhouse';
import { runAnalyticsQuery } from 'lib/db';
import { parseFilters, rawQuery } from 'lib/relational';
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',
null,
filters,
params,
);
return rawQuery(
`
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 pageview.created_at between $2 and $3
${pageviewQuery}
${sessionQuery}
)
group by 1
order by 2 desc
`,
params,
);
}
async function clickhouseQuery(website_id, start_at, end_at, field, filters = {}) {
const params = [website_id];
const { pageviewQuery, sessionQuery, joinSession } = clickhouse.parseFilters(
'pageview',
null,
filters,
params,
'session_uuid',
);
return clickhouse.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)}
${pageviewQuery}
${sessionQuery}
)
group by x
order by y desc
`,
params,
);
}