mirror of
https://github.com/umami-software/umami.git
synced 2026-02-13 00:55:37 +01:00
route to view or raw table based on filters
This commit is contained in:
parent
2dcb9e21bd
commit
954404f8df
7 changed files with 102 additions and 26 deletions
|
|
@ -109,7 +109,7 @@ async function clickhouseQuery(
|
||||||
sum(views) c,
|
sum(views) c,
|
||||||
min(min_time) min_time,
|
min(min_time) min_time,
|
||||||
max(max_time) max_time
|
max(max_time) max_time
|
||||||
from umami.website_event_stats_hourly "website_event"
|
from website_event_stats_hourly "website_event"
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
|
|
|
||||||
|
|
@ -142,18 +142,18 @@ async function clickhouseQuery(
|
||||||
`;
|
`;
|
||||||
} else {
|
} else {
|
||||||
let groupByQuery = '';
|
let groupByQuery = '';
|
||||||
let columnQuery = `session_id s, arrayJoin(${column})`;
|
let columnQuery = `arrayJoin(${column})`;
|
||||||
|
|
||||||
if (column === 'referrer_domain') {
|
if (column === 'referrer_domain') {
|
||||||
excludeDomain = `and t != ''`;
|
excludeDomain = `and t != ''`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'entry') {
|
if (type === 'entry') {
|
||||||
columnQuery = `session_id s, argMinMerge(entry_url)`;
|
columnQuery = `argMinMerge(entry_url)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'exit') {
|
if (type === 'exit') {
|
||||||
columnQuery = `session_id s, argMaxMerge(exit_url)`;
|
columnQuery = `argMaxMerge(exit_url)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'entry' || type === 'exit') {
|
if (type === 'entry' || type === 'exit') {
|
||||||
|
|
@ -164,7 +164,8 @@ async function clickhouseQuery(
|
||||||
select g.t as x,
|
select g.t as x,
|
||||||
uniq(s) as y
|
uniq(s) as y
|
||||||
from (
|
from (
|
||||||
select ${columnQuery} as t
|
select session_id s,
|
||||||
|
${columnQuery} as t
|
||||||
from website_event_stats_hourly website_event
|
from website_event_stats_hourly website_event
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ async function clickhouseQuery(
|
||||||
`
|
`
|
||||||
WITH level0 AS (
|
WITH level0 AS (
|
||||||
select distinct session_id, url_path, referrer_path, event_name, created_at
|
select distinct session_id, url_path, referrer_path, event_name, created_at
|
||||||
from umami.website_event
|
from website_event
|
||||||
where (${stepFilterQuery})
|
where (${stepFilterQuery})
|
||||||
and website_id = {websiteId:UUID}
|
and website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
|
|
|
||||||
|
|
@ -229,7 +229,7 @@ async function clickhouseQuery(
|
||||||
visit_id,
|
visit_id,
|
||||||
coalesce(nullIf(event_name, ''), url_path) event,
|
coalesce(nullIf(event_name, ''), url_path) event,
|
||||||
row_number() OVER (PARTITION BY visit_id ORDER BY created_at) AS event_number
|
row_number() OVER (PARTITION BY visit_id ORDER BY created_at) AS event_number
|
||||||
from umami.website_event
|
from website_event
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}),
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}),
|
||||||
${sequenceQuery}
|
${sequenceQuery}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
import clickhouse from '@/lib/clickhouse';
|
||||||
|
import { EVENT_COLUMNS } from '@/lib/constants';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
import { QueryFilters } from '@/lib/types';
|
import { QueryFilters } from '@/lib/types';
|
||||||
|
|
@ -55,20 +56,37 @@ async function clickhouseQuery(
|
||||||
...filters,
|
...filters,
|
||||||
});
|
});
|
||||||
|
|
||||||
return rawQuery(
|
let sql = '';
|
||||||
`
|
|
||||||
|
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
|
||||||
|
sql = `
|
||||||
|
select
|
||||||
|
sumIf(1, event_type = 1) as "pageviews",
|
||||||
|
uniq(session_id) as "visitors",
|
||||||
|
uniq(visit_id) as "visits",
|
||||||
|
uniq(country) as "countries",
|
||||||
|
sum(length(event_name)) as "events"
|
||||||
|
from website_event
|
||||||
|
${cohortQuery}
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
|
${filterQuery}
|
||||||
|
`;
|
||||||
|
} else {
|
||||||
|
sql = `
|
||||||
select
|
select
|
||||||
sum(views) as "pageviews",
|
sum(views) as "pageviews",
|
||||||
uniq(session_id) as "visitors",
|
uniq(session_id) as "visitors",
|
||||||
uniq(visit_id) as "visits",
|
uniq(visit_id) as "visits",
|
||||||
uniq(country) as "countries",
|
uniq(country) as "countries",
|
||||||
sum(length(event_name)) as "events"
|
sum(length(event_name)) as "events"
|
||||||
from umami.website_event_stats_hourly "website_event"
|
from website_event_stats_hourly website_event
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
`,
|
`;
|
||||||
params,
|
}
|
||||||
);
|
|
||||||
|
return rawQuery(sql, params);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
import clickhouse from '@/lib/clickhouse';
|
||||||
|
import { EVENT_COLUMNS } from '@/lib/constants';
|
||||||
import { CLICKHOUSE, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
|
import { CLICKHOUSE, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
import { PageParams, QueryFilters } from '@/lib/types';
|
import { PageParams, QueryFilters } from '@/lib/types';
|
||||||
|
|
@ -77,8 +78,45 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
|
||||||
const { params, dateQuery, filterQuery, cohortQuery } = await parseFilters(websiteId, filters);
|
const { params, dateQuery, filterQuery, cohortQuery } = await parseFilters(websiteId, filters);
|
||||||
const { search } = pageParams;
|
const { search } = pageParams;
|
||||||
|
|
||||||
return pagedQuery(
|
let sql = '';
|
||||||
`
|
|
||||||
|
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
|
||||||
|
sql = `
|
||||||
|
select
|
||||||
|
session_id as id,
|
||||||
|
website_id as websiteId,
|
||||||
|
browser,
|
||||||
|
os,
|
||||||
|
device,
|
||||||
|
screen,
|
||||||
|
language,
|
||||||
|
country,
|
||||||
|
region,
|
||||||
|
city,
|
||||||
|
${getDateStringSQL('min(created_at)')} as firstAt,
|
||||||
|
${getDateStringSQL('max(created_at)')} as lastAt,
|
||||||
|
uniq(visit_id) as visits,
|
||||||
|
sumIf(views, event_type = 1) as views,
|
||||||
|
lastAt as createdAt
|
||||||
|
from website_event
|
||||||
|
${cohortQuery}
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
${dateQuery}
|
||||||
|
${filterQuery}
|
||||||
|
${
|
||||||
|
search
|
||||||
|
? `and ((positionCaseInsensitive(distinct_id, {search:String}) > 0)
|
||||||
|
or (positionCaseInsensitive(city, {search:String}) > 0)
|
||||||
|
or (positionCaseInsensitive(browser, {search:String}) > 0)
|
||||||
|
or (positionCaseInsensitive(os, {search:String}) > 0)
|
||||||
|
or (positionCaseInsensitive(device, {search:String}) > 0))`
|
||||||
|
: ''
|
||||||
|
}
|
||||||
|
group by session_id, website_id, browser, os, device, screen, language, country, region, city
|
||||||
|
order by lastAt desc
|
||||||
|
`;
|
||||||
|
} else {
|
||||||
|
sql = `
|
||||||
select
|
select
|
||||||
session_id as id,
|
session_id as id,
|
||||||
website_id as websiteId,
|
website_id as websiteId,
|
||||||
|
|
@ -111,8 +149,8 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
|
||||||
}
|
}
|
||||||
group by session_id, website_id, browser, os, device, screen, language, country, region, city
|
group by session_id, website_id, browser, os, device, screen, language, country, region, city
|
||||||
order by lastAt desc
|
order by lastAt desc
|
||||||
`,
|
`;
|
||||||
{ ...params, search },
|
}
|
||||||
pageParams,
|
|
||||||
);
|
return pagedQuery(sql, { ...params, search }, pageParams);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import prisma from '@/lib/prisma';
|
||||||
import clickhouse from '@/lib/clickhouse';
|
import clickhouse from '@/lib/clickhouse';
|
||||||
import { runQuery, PRISMA, CLICKHOUSE } from '@/lib/db';
|
import { runQuery, PRISMA, CLICKHOUSE } from '@/lib/db';
|
||||||
import { QueryFilters } from '@/lib/types';
|
import { QueryFilters } from '@/lib/types';
|
||||||
|
import { EVENT_COLUMNS } from '@/lib/constants';
|
||||||
|
|
||||||
export async function getWebsiteSessionsWeekly(
|
export async function getWebsiteSessionsWeekly(
|
||||||
...args: [websiteId: string, filters?: QueryFilters]
|
...args: [websiteId: string, filters?: QueryFilters]
|
||||||
|
|
@ -35,21 +36,39 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||||
async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||||
const { timezone = 'utc' } = filters;
|
const { timezone = 'utc' } = filters;
|
||||||
const { rawQuery, parseFilters } = clickhouse;
|
const { rawQuery, parseFilters } = clickhouse;
|
||||||
const { params } = await parseFilters(websiteId, filters);
|
const { filterQuery, cohortQuery, params } = await parseFilters(websiteId, filters);
|
||||||
|
|
||||||
return rawQuery(
|
let sql = '';
|
||||||
`
|
|
||||||
|
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
|
||||||
|
sql = `
|
||||||
select
|
select
|
||||||
formatDateTime(toDateTime(created_at, '${timezone}'), '%w:%H') as time,
|
formatDateTime(toDateTime(created_at, '${timezone}'), '%w:%H') as time,
|
||||||
count(distinct session_id) as value
|
count(distinct session_id) as value
|
||||||
from website_event_stats_hourly
|
from website_event
|
||||||
|
${cohortQuery}
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
|
${filterQuery}
|
||||||
group by time
|
group by time
|
||||||
order by time
|
order by time
|
||||||
`,
|
`;
|
||||||
params,
|
} else {
|
||||||
).then(formatResults);
|
sql = `
|
||||||
|
select
|
||||||
|
formatDateTime(toDateTime(created_at, '${timezone}'), '%w:%H') as time,
|
||||||
|
count(distinct session_id) as value
|
||||||
|
from website_event_stats_hourly website_event
|
||||||
|
${cohortQuery}
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
|
${filterQuery}
|
||||||
|
group by time
|
||||||
|
order by time
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rawQuery(sql, params).then(formatResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatResults(data: any) {
|
function formatResults(data: any) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue