mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 22:57:12 +01:00
New goals page. Upgraded prisma.
This commit is contained in:
parent
99330a1a4d
commit
49bcbfd7f9
65 changed files with 769 additions and 1195 deletions
77
src/queries/sql/reports/getGoal.ts
Normal file
77
src/queries/sql/reports/getGoal.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import clickhouse from '@/lib/clickhouse';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
export interface GoalCriteria {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
type: string;
|
||||
value: string;
|
||||
operator?: string;
|
||||
property?: string;
|
||||
}
|
||||
|
||||
export async function getGoal(...args: [websiteId: string, criteria: GoalCriteria]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string, criteria: GoalCriteria) {
|
||||
const { type, value } = criteria;
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const { filterQuery, dateQuery, params } = await parseFilters(websiteId, criteria);
|
||||
const isPage = type === 'page';
|
||||
const column = isPage ? 'url_path' : 'event_name';
|
||||
const eventType = isPage ? 1 : 2;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select count(*) as num,
|
||||
(
|
||||
select count(${isPage ? '*' : 'distinct session_id'})
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type = ${eventType}
|
||||
${dateQuery}
|
||||
) as total
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type = ${eventType}
|
||||
and ${column} = {value:String}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
`,
|
||||
{ ...params, value },
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string, criteria: GoalCriteria) {
|
||||
const { type, value } = criteria;
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { filterQuery, dateQuery, params } = await parseFilters(websiteId, criteria);
|
||||
const isPage = type === 'page';
|
||||
const column = isPage ? 'url_path' : 'event_name';
|
||||
const eventType = isPage ? 1 : 2;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select count(*) as num,
|
||||
(
|
||||
select count(${isPage ? '*' : 'distinct session_id'})
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type = ${eventType}
|
||||
${dateQuery}
|
||||
) as total
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type = ${eventType}
|
||||
and ${column} = {value:String}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
`,
|
||||
{ ...params, value },
|
||||
).then(results => results?.[0]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue