umami/queries/analytics/pageview/getPageviewParams.js

50 lines
1.4 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-26 18:49:38 +02:00
const params = [param, param, website_id, start_at, end_at];
2022-07-21 23:57:29 +02:00
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
2022-07-26 18:49:38 +02:00
url,
IF( LENGTH(url) - LENGTH(${splitFn}(url, concat($1, '='), -1)) > 1, ${splitFn}(${splitFn}(url, concat($2, '='), -1), '&', 1), null ) param
from
pageview
${joinSession}
where
2022-07-26 18:49:38 +02:00
${table}.website_id=$3 and ${table}.created_at between $4 and $5
${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.'));
}