umami/queries/analytics/event/getEventMetrics.js

67 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-07-16 01:47:38 +02:00
import {
getDateQuery,
getDateStringQuery,
getFilterQuery,
rawQuery,
runAnalyticsQuery,
clickhouse,
} from 'lib/db';
2022-07-12 23:14:36 +02:00
2022-07-16 01:47:38 +02:00
export function getEventMetrics(...args) {
return runAnalyticsQuery(relationalQuery(...args), clickhouseQuery(...args));
}
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-13 07:39:16 +02:00
${getDateStringQuery(getDateQuery('created_at', unit, timezone), unit)} 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
function clickhouseQuery(
website_id,
start_at,
end_at,
timezone = 'utc',
unit = 'day',
filters = {},
) {
const params = [website_id, start_at, end_at];
return clickhouse.query(
`
select
event_value x,
${getDateStringQuery(getDateQuery('created_at', unit, timezone), unit)} 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
`,
params,
);
}