mirror of
https://github.com/umami-software/umami.git
synced 2026-02-21 13:05:36 +01:00
feat: separate mongoQuery & add mongo filter
This commit is contained in:
parent
4c57ab1388
commit
b5b689b156
15 changed files with 858 additions and 710 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
|
||||
|
|
@ -17,6 +17,7 @@ export async function getPageviewMetrics(
|
|||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[MONGODB]: () => mongodbQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
|
@ -31,8 +32,7 @@ async function relationalQuery(
|
|||
},
|
||||
) {
|
||||
const { startDate, endDate, filters = {}, column } = criteria;
|
||||
const { getDatabaseType, rawQuery, parseFilters, toUuid, client } = prisma;
|
||||
const db = getDatabaseType();
|
||||
const { rawQuery, parseFilters, toUuid } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||
const params: any = [
|
||||
|
|
@ -44,99 +44,15 @@ async function relationalQuery(
|
|||
];
|
||||
|
||||
let excludeDomain = '';
|
||||
let excludeDomainMongo = {};
|
||||
|
||||
if (column === 'referrer_domain') {
|
||||
excludeDomain = 'and website_event.referrer_domain != $6';
|
||||
excludeDomainMongo = {
|
||||
$ne: ['$referrer_domain', website.domain],
|
||||
};
|
||||
params.push(website.domain);
|
||||
}
|
||||
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
if (db === 'mongodb') {
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$eq: ['$event_type', params[4]],
|
||||
},
|
||||
{
|
||||
$eq: ['$website_id', websiteId],
|
||||
},
|
||||
{
|
||||
$gte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: resetDate.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
$gte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: startDate.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
$lte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: endDate.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
excludeDomainMongo,
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$' + column,
|
||||
y: {
|
||||
$sum: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
x: '$_id',
|
||||
y: 1,
|
||||
_id: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
x: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
y: -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$limit: 100,
|
||||
},
|
||||
],
|
||||
});
|
||||
} else {
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
return rawQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from website_event
|
||||
${joinSession}
|
||||
where website_event.website_id = $1${toUuid()}
|
||||
|
|
@ -148,9 +64,8 @@ async function relationalQuery(
|
|||
group by 1
|
||||
order by 2 desc
|
||||
limit 100`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
@ -196,3 +111,113 @@ async function clickhouseQuery(
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function mongodbQuery(
|
||||
websiteId: string,
|
||||
criteria: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
column: string;
|
||||
filters: object;
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, filters = {}, column } = criteria;
|
||||
const { parseMongoFilter, client } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||
const params: any = [
|
||||
websiteId,
|
||||
resetDate,
|
||||
startDate,
|
||||
endDate,
|
||||
column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
];
|
||||
|
||||
let excludeDomainMongo: any = '';
|
||||
|
||||
if (column === 'referrer_domain') {
|
||||
excludeDomainMongo = {
|
||||
$ne: ['$referrer_domain', website.domain],
|
||||
};
|
||||
params.push(website.domain);
|
||||
}
|
||||
const mongoFilter = parseMongoFilter(filters);
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
mongoFilter,
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$eq: ['$event_type', params[4]],
|
||||
},
|
||||
{
|
||||
$eq: ['$website_id', websiteId],
|
||||
},
|
||||
{
|
||||
$gte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: resetDate.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
$gte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: startDate.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
$lte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: endDate.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
excludeDomainMongo,
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$' + column,
|
||||
y: {
|
||||
$sum: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
x: '$_id',
|
||||
y: 1,
|
||||
_id: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
x: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
y: -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$limit: 100,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue