2022-07-21 06:31:26 +02:00
|
|
|
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
2022-07-16 01:47:38 +02:00
|
|
|
import {
|
2022-07-21 06:31:26 +02:00
|
|
|
getBetweenDatesClickhouse,
|
2022-07-16 01:47:38 +02:00
|
|
|
getDateQuery,
|
2022-07-21 06:31:26 +02:00
|
|
|
getDateQueryClickhouse,
|
2022-07-16 01:47:38 +02:00
|
|
|
getFilterQuery,
|
|
|
|
rawQuery,
|
2022-07-23 02:03:42 +02:00
|
|
|
rawQueryClickhouse,
|
2022-07-16 01:47:38 +02:00
|
|
|
runAnalyticsQuery,
|
|
|
|
} from 'lib/db';
|
2022-07-12 23:14:36 +02:00
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
export async function getEventMetrics(...args) {
|
|
|
|
return runAnalyticsQuery({
|
|
|
|
[`${RELATIONAL}`]: () => relationalQuery(...args),
|
|
|
|
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
|
|
|
|
});
|
2022-07-16 01:47:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
async function relationalQuery(
|
2022-07-12 23:14:36 +02:00
|
|
|
website_id,
|
|
|
|
start_at,
|
|
|
|
end_at,
|
|
|
|
timezone = 'utc',
|
|
|
|
unit = 'day',
|
|
|
|
filters = {},
|
|
|
|
) {
|
|
|
|
const params = [website_id, start_at, end_at];
|
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
`
|
|
|
|
select
|
|
|
|
event_value x,
|
2022-07-21 22:21:33 +02:00
|
|
|
${getDateQuery('created_at', unit, timezone)} t,
|
2022-07-12 23:14:36 +02:00
|
|
|
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
|
|
|
|
`,
|
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|
2022-07-16 01:47:38 +02:00
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
async function clickhouseQuery(
|
2022-07-16 01:47:38 +02:00
|
|
|
website_id,
|
|
|
|
start_at,
|
|
|
|
end_at,
|
2022-07-21 06:31:26 +02:00
|
|
|
timezone = 'UTC',
|
2022-07-16 01:47:38 +02:00
|
|
|
unit = 'day',
|
|
|
|
filters = {},
|
|
|
|
) {
|
2022-07-21 06:31:26 +02:00
|
|
|
const params = [website_id];
|
2022-07-16 01:47:38 +02:00
|
|
|
|
2022-07-21 06:31:26 +02:00
|
|
|
return rawQueryClickhouse(
|
2022-07-16 01:47:38 +02:00
|
|
|
`
|
|
|
|
select
|
|
|
|
event_value x,
|
2022-07-21 06:31:26 +02:00
|
|
|
${getDateQueryClickhouse('created_at', unit, timezone)} t,
|
2022-07-16 01:47:38 +02:00
|
|
|
count(*) y
|
|
|
|
from event
|
2022-07-21 06:31:26 +02:00
|
|
|
where website_id= $1
|
|
|
|
and ${getBetweenDatesClickhouse('created_at', start_at, end_at)}
|
|
|
|
${getFilterQuery('event', filters, params)}
|
|
|
|
group by x, t
|
|
|
|
order by t
|
2022-07-16 01:47:38 +02:00
|
|
|
`,
|
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|