mirror of
https://github.com/umami-software/umami.git
synced 2026-02-13 17:15:37 +01:00
Refactored query parameter handling.
This commit is contained in:
parent
157862834d
commit
7148f66d1a
17 changed files with 260 additions and 469 deletions
|
|
@ -1,9 +1,7 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { DEFAULT_RESET_DATE } from 'lib/constants';
|
||||
import { maxDate } from 'lib/date';
|
||||
|
||||
export async function getWebsiteDateRange(...args: [websiteId: string]) {
|
||||
return runQuery({
|
||||
|
|
@ -13,8 +11,8 @@ export async function getWebsiteDateRange(...args: [websiteId: string]) {
|
|||
}
|
||||
|
||||
async function relationalQuery(websiteId: string) {
|
||||
const { rawQuery } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const { params } = await parseFilters(websiteId, { startDate: new Date(DEFAULT_RESET_DATE) });
|
||||
|
||||
const result = await rawQuery(
|
||||
`
|
||||
|
|
@ -25,15 +23,15 @@ async function relationalQuery(websiteId: string) {
|
|||
where website_id = {{websiteId::uuid}}
|
||||
and created_at >= {{startDate}}
|
||||
`,
|
||||
{ websiteId, startDate: maxDate(new Date(DEFAULT_RESET_DATE), new Date(website.resetAt)) },
|
||||
params,
|
||||
);
|
||||
|
||||
return result[0] ?? null;
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string) {
|
||||
const { rawQuery } = clickhouse;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { params } = await parseFilters(websiteId, { startDate: new Date(DEFAULT_RESET_DATE) });
|
||||
|
||||
const result = await rawQuery(
|
||||
`
|
||||
|
|
@ -44,7 +42,7 @@ async function clickhouseQuery(websiteId: string) {
|
|||
where website_id = {websiteId:UUID}
|
||||
and created_at >= {startDate:DateTime}
|
||||
`,
|
||||
{ websiteId, startDate: maxDate(new Date(DEFAULT_RESET_DATE), new Date(website.resetAt)) },
|
||||
params,
|
||||
);
|
||||
|
||||
return result[0] ?? null;
|
||||
|
|
|
|||
|
|
@ -2,29 +2,21 @@ import prisma from 'lib/prisma';
|
|||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/load';
|
||||
import { maxDate } from 'lib/date';
|
||||
import { QueryFilters } from 'lib/types';
|
||||
|
||||
export async function getWebsiteStats(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: { startDate: Date; endDate: Date; type?: string; filters: object },
|
||||
]
|
||||
) {
|
||||
export async function getWebsiteStats(...args: [websiteId: string, filters: QueryFilters]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = criteria;
|
||||
async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
const { getDateQuery, getTimestampIntervalQuery, parseFilters, rawQuery } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const { filterQuery, joinSession } = parseFilters(filters);
|
||||
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
|
||||
...filters,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
|
|
@ -43,32 +35,23 @@ async function relationalQuery(
|
|||
join website
|
||||
on website_event.website_id = website.website_id
|
||||
${joinSession}
|
||||
where event_type = {{eventType}}
|
||||
and website.website_id = {{websiteId::uuid}}
|
||||
where website.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and event_type = {{eventType}}
|
||||
${filterQuery}
|
||||
group by 1, 2
|
||||
) as t
|
||||
`,
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
domain: website.domain,
|
||||
},
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = criteria;
|
||||
async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||
const { rawQuery, getDateQuery, parseFilters } = clickhouse;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const { filterQuery } = parseFilters(filters);
|
||||
const { filterQuery, params } = await parseFilters(websiteId, {
|
||||
...filters,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
|
|
@ -92,13 +75,6 @@ async function clickhouseQuery(
|
|||
group by session_id, time_series
|
||||
) as t;
|
||||
`,
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
startDate: maxDate(startDate, website.resetAt),
|
||||
endDate,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
domain: website.domain,
|
||||
},
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue