umami/queries/analytics/pageview/getPageviewParams.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-07-23 04:50:43 +02:00
import { getDatabase, parseFilters, rawQuery, runAnalyticsQuery } from 'lib/db';
import { MYSQL, POSTGRESQL, CLICKHOUSE, RELATIONAL } from 'lib/constants';
2022-07-21 23:57:29 +02:00
2022-07-23 04:50:43 +02:00
export async function getPageviewParams(...args) {
return runAnalyticsQuery({
2022-07-25 18:47:11 +02:00
[RELATIONAL]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
2022-07-23 04:50:43 +02:00
});
}
function relationalQuery(param, website_id, start_at, end_at, column, table, filters = {}) {
2022-07-21 23:57:29 +02:00
const params = [param, website_id, start_at, end_at];
const { pageviewQuery, sessionQuery, eventQuery, joinSession } = parseFilters(
table,
column,
filters,
params,
);
let splitFn;
let db = getDatabase();
if (db === MYSQL) splitFn = 'substring_index';
if (db === POSTGRESQL) splitFn = 'split_part';
if (!splitFn) return Promise.reject(new Error('Unknown database.'));
2022-07-21 23:57:29 +02:00
return rawQuery(
`select * from (
select
url, ${splitFn}(${splitFn}(url, concat($1, '='), 2), '&', 1) param
from
pageview
${joinSession}
where
${table}.website_id=$2 and ${table}.created_at between $3 and $4
${pageviewQuery}
${joinSession && sessionQuery}
${eventQuery}
group by 1, 2
order by 2 desc
2022-07-21 23:57:29 +02:00
) q
where q.param <> ''`,
params,
);
}
2022-07-23 04:50:43 +02:00
function clickhouseQuery() {
return Promise.reject(new Error('Not implemented.'));
}