mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 00:27:11 +01:00
Refactored filter logic.
This commit is contained in:
parent
57c3d03cc8
commit
74192cd695
18 changed files with 205 additions and 296 deletions
|
|
@ -1,20 +1,17 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { getWebsite } from 'queries';
|
||||
import { EVENT_TYPE, FILTER_COLUMNS } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
|
||||
export async function getPageviewMetrics(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: {
|
||||
criteria: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
||||
column: string;
|
||||
filters: object;
|
||||
type: string;
|
||||
},
|
||||
]
|
||||
) {
|
||||
|
|
@ -26,25 +23,32 @@ export async function getPageviewMetrics(
|
|||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
criteria: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
||||
column: string;
|
||||
filters: object;
|
||||
type: string;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, column, filters = {}, type } = data;
|
||||
const { startDate, endDate, filters = {}, column } = criteria;
|
||||
const { rawQuery, parseFilters, toUuid } = prisma;
|
||||
const website = await getWebsite({ id: websiteId });
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = website?.resetAt || website?.createdAt;
|
||||
const params: any = [
|
||||
websiteId,
|
||||
resetDate,
|
||||
startDate,
|
||||
endDate,
|
||||
type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
];
|
||||
|
||||
let domainFilter = '';
|
||||
|
||||
if (column === 'referrer_domain') {
|
||||
domainFilter = 'and website_event.referrer_domain != $6';
|
||||
params.push(website.domain);
|
||||
}
|
||||
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
|
|
@ -55,6 +59,7 @@ async function relationalQuery(
|
|||
and website_event.created_at >= $2
|
||||
and website_event.created_at between $3 and $4
|
||||
and event_type = $5
|
||||
${domainFilter}
|
||||
${filterQuery}
|
||||
group by 1
|
||||
order by 2 desc
|
||||
|
|
@ -65,22 +70,30 @@ async function relationalQuery(
|
|||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
criteria: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
||||
column: string;
|
||||
filters: object;
|
||||
type: string;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, column, filters = {}, type } = data;
|
||||
const { startDate, endDate, filters = {}, column } = criteria;
|
||||
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse;
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = website?.resetAt || website?.createdAt;
|
||||
const params = {
|
||||
websiteId,
|
||||
eventType: type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
domain: undefined,
|
||||
};
|
||||
|
||||
let excludeDomain = '';
|
||||
|
||||
if (column === 'referrer_domain') {
|
||||
excludeDomain = 'and referrer_domain != {domain:String}';
|
||||
params.domain = website.domain;
|
||||
}
|
||||
|
||||
const { filterQuery } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
|
|
@ -89,7 +102,8 @@ async function clickhouseQuery(
|
|||
where website_id = {websiteId:UUID}
|
||||
and event_type = {eventType:UInt32}
|
||||
and created_at >= ${getDateFormat(resetDate)}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
${excludeDomain}
|
||||
${filterQuery}
|
||||
group by x
|
||||
order by y desc
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import { getWebsite } from 'queries';
|
|||
export async function getPageviewStats(
|
||||
...args: [
|
||||
websiteId: string,
|
||||
data: {
|
||||
criteria: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone?: string;
|
||||
|
|
@ -27,7 +27,7 @@ export async function getPageviewStats(
|
|||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
criteria: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone?: string;
|
||||
|
|
@ -45,7 +45,7 @@ async function relationalQuery(
|
|||
count = '*',
|
||||
filters = {},
|
||||
sessionKey = 'session_id',
|
||||
} = data;
|
||||
} = criteria;
|
||||
const { toUuid, getDateQuery, parseFilters, rawQuery } = prisma;
|
||||
const website = await getWebsite({ id: websiteId });
|
||||
const resetDate = website?.resetAt || website?.createdAt;
|
||||
|
|
@ -69,7 +69,7 @@ async function relationalQuery(
|
|||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
data: {
|
||||
criteria: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone?: string;
|
||||
|
|
@ -79,7 +79,14 @@ async function clickhouseQuery(
|
|||
sessionKey?: string;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, timezone = 'UTC', unit = 'day', count = '*', filters = {} } = data;
|
||||
const {
|
||||
startDate,
|
||||
endDate,
|
||||
timezone = 'UTC',
|
||||
unit = 'day',
|
||||
count = '*',
|
||||
filters = {},
|
||||
} = criteria;
|
||||
const {
|
||||
parseFilters,
|
||||
getDateFormat,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue