mirror of
https://github.com/umami-software/umami.git
synced 2026-02-14 09:35:36 +01:00
Merge branch 'dev' into jajaja
# Conflicts: # pnpm-lock.yaml # src/queries/sql/getWebsiteStats.ts # src/queries/sql/pageviews/getPageviewMetrics.ts # src/queries/sql/sessions/getWebsiteSessionStats.ts # src/queries/sql/sessions/getWebsiteSessions.ts # src/queries/sql/sessions/getWebsiteSessionsWeekly.ts
This commit is contained in:
commit
2f7f8911cd
7 changed files with 96 additions and 25 deletions
|
|
@ -113,7 +113,8 @@ 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}
|
||||||
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}
|
||||||
and event_type = {eventType:UInt32}
|
and event_type = {eventType:UInt32}
|
||||||
|
|
|
||||||
|
|
@ -146,18 +146,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') {
|
||||||
|
|
@ -168,6 +168,9 @@ async function clickhouseQuery(
|
||||||
select g.t as x,
|
select g.t as x,
|
||||||
uniq(s) as y
|
uniq(s) as y
|
||||||
from (
|
from (
|
||||||
|
select session_id s,
|
||||||
|
${columnQuery} as t
|
||||||
|
from website_event_stats_hourly website_event
|
||||||
select ${columnQuery} as t
|
select ${columnQuery} as t
|
||||||
from website_event_stats_hourly as website_event
|
from website_event_stats_hourly as website_event
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
|
|
|
||||||
|
|
@ -203,7 +203,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}
|
||||||
|
|
|
||||||
|
|
@ -220,7 +220,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}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}),
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}),
|
||||||
|
|
|
||||||
|
|
@ -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';
|
||||||
|
|
@ -53,20 +54,37 @@ async function clickhouseQuery(
|
||||||
const { rawQuery, parseFilters } = clickhouse;
|
const { rawQuery, parseFilters } = clickhouse;
|
||||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });
|
const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });
|
||||||
|
|
||||||
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}
|
||||||
`,
|
`;
|
||||||
queryParams,
|
}
|
||||||
);
|
|
||||||
|
return rawQuery(sql, queryParams);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
import clickhouse from '@/lib/clickhouse';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||||
|
import { EVENT_COLUMNS } from '@/lib/constants';
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
import { QueryFilters } from '@/lib/types';
|
import { QueryFilters } from '@/lib/types';
|
||||||
|
|
||||||
|
|
@ -75,8 +76,37 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||||
|
|
||||||
const searchQuery = search ? `and positionCaseInsensitive(distinct_id, {search:String}) > 0` : '';
|
const searchQuery = search ? `and positionCaseInsensitive(distinct_id, {search:String}) > 0` : '';
|
||||||
|
|
||||||
return pagedRawQuery(
|
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}
|
||||||
|
${searchQuery}
|
||||||
|
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,
|
||||||
|
|
@ -102,8 +132,8 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||||
${searchQuery}
|
${searchQuery}
|
||||||
group by session_id, website_id, hostname, browser, os, device, screen, language, country, region, city
|
group by session_id, website_id, hostname, browser, os, device, screen, language, country, region, city
|
||||||
order by lastAt desc
|
order by lastAt desc
|
||||||
`,
|
`;
|
||||||
queryParams,
|
}
|
||||||
filters,
|
|
||||||
);
|
return pagedRawQuery(sql, queryParams, filters);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 { queryParams } = parseFilters(filters);
|
const { filterQuery, cohortQuery, queryParams } = await parseFilters({ ...filters, websiteId });
|
||||||
|
|
||||||
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
|
||||||
`,
|
`;
|
||||||
queryParams,
|
} 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, queryParams).then(formatResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatResults(data: any) {
|
function formatResults(data: any) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue