mirror of
https://github.com/umami-software/umami.git
synced 2026-02-05 21:27:20 +01:00
Add MetricsBar to Events page. Closes #3830
This commit is contained in:
parent
1498da2d02
commit
a1a092dc19
5 changed files with 220 additions and 2 deletions
97
src/queries/sql/events/getWebsiteEventStats.ts
Normal file
97
src/queries/sql/events/getWebsiteEventStats.ts
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import clickhouse from '@/lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
const FUNCTION_NAME = 'getWebsiteEventStats';
|
||||
|
||||
export interface WebsiteEventStatsData {
|
||||
events: number;
|
||||
visitors: number;
|
||||
visits: number;
|
||||
uniqueEvents: number;
|
||||
}
|
||||
|
||||
export async function getWebsiteEventStats(
|
||||
...args: [websiteId: string, filters: QueryFilters]
|
||||
): Promise<WebsiteEventStatsData[]> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
filters: QueryFilters,
|
||||
): Promise<WebsiteEventStatsData[]> {
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
cast(coalesce(sum(t.c), 0) as bigint) as "events",
|
||||
count(distinct t.session_id) as "visitors",
|
||||
count(distinct t.visit_id) as "visits",
|
||||
count(distinct t.event_name) as "uniqueEvents"
|
||||
from (
|
||||
select
|
||||
website_event.session_id,
|
||||
website_event.visit_id,
|
||||
website_event.event_name,
|
||||
count(*) as "c"
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type = 2
|
||||
${filterQuery}
|
||||
group by 1, 2, 3
|
||||
) as t
|
||||
`,
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
).then(result => result?.[0]);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
filters: QueryFilters,
|
||||
): Promise<WebsiteEventStatsData[]> {
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
sum(t.c) as "events",
|
||||
uniq(t.session_id) as "visitors",
|
||||
uniq(t.visit_id) as "visits",
|
||||
count(distinct t.event_name) as "uniqueEvents"
|
||||
from (
|
||||
select
|
||||
session_id,
|
||||
visit_id,
|
||||
event_name,
|
||||
count(*) c
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = 2
|
||||
${filterQuery}
|
||||
group by session_id, visit_id, event_name
|
||||
) as t;
|
||||
`,
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
).then(result => result?.[0]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue