mirror of
https://github.com/umami-software/umami.git
synced 2026-02-21 04:55: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 { WebsiteEventMetric } from 'lib/types';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { loadWebsite } from 'lib/query';
|
||||
|
|
@ -23,6 +23,7 @@ export async function getEventMetrics(
|
|||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
[MONGODB]: () => mongodbQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -45,107 +46,13 @@ async function relationalQuery(
|
|||
};
|
||||
},
|
||||
) {
|
||||
const { getDatabaseType, toUuid, rawQuery, getDateQuery, getFilterQuery, client } = prisma;
|
||||
const { toUuid, rawQuery, getDateQuery, getFilterQuery } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||
const params: any = [websiteId, resetDate, startDate, endDate];
|
||||
const filterQuery = getFilterQuery(filters, params);
|
||||
const db = getDatabaseType();
|
||||
|
||||
if (db === 'mongodb') {
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$eq: ['$event_type', EVENT_TYPE.customEvent],
|
||||
},
|
||||
{
|
||||
$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(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
t: {
|
||||
$dateTrunc: {
|
||||
date: '$created_at',
|
||||
unit: unit,
|
||||
timezone: timezone,
|
||||
},
|
||||
},
|
||||
event_name: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
t: '$t',
|
||||
name: '$event_name',
|
||||
},
|
||||
y: {
|
||||
$sum: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
x: '$_id.name',
|
||||
t: {
|
||||
$dateToString: {
|
||||
date: '$_id.t',
|
||||
format: getDateQuery('', unit, timezone),
|
||||
timezone: timezone,
|
||||
},
|
||||
},
|
||||
y: 1,
|
||||
_id: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
t: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
} else {
|
||||
return rawQuery(
|
||||
`select
|
||||
return rawQuery(
|
||||
`select
|
||||
event_name x,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
|
|
@ -157,9 +64,8 @@ async function relationalQuery(
|
|||
${filterQuery}
|
||||
group by 1, 2
|
||||
order by 2`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
@ -202,3 +108,120 @@ async function clickhouseQuery(
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function mongodbQuery(
|
||||
websiteId: string,
|
||||
{
|
||||
startDate,
|
||||
endDate,
|
||||
timezone = 'utc',
|
||||
unit = 'day',
|
||||
filters,
|
||||
}: {
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
timezone: string;
|
||||
unit: string;
|
||||
filters: {
|
||||
url: string;
|
||||
eventName: string;
|
||||
};
|
||||
},
|
||||
) {
|
||||
const { getDateQuery, parseMongoFilter, client } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||
const mongoFilter = parseMongoFilter(filters);
|
||||
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
mongoFilter,
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$eq: ['$event_type', EVENT_TYPE.customEvent],
|
||||
},
|
||||
{
|
||||
$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(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
t: {
|
||||
$dateTrunc: {
|
||||
date: '$created_at',
|
||||
unit: unit,
|
||||
timezone: timezone,
|
||||
},
|
||||
},
|
||||
event_name: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
t: '$t',
|
||||
name: '$event_name',
|
||||
},
|
||||
y: {
|
||||
$sum: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
x: '$_id.name',
|
||||
t: {
|
||||
$dateToString: {
|
||||
date: '$_id.t',
|
||||
format: getDateQuery('', unit, timezone),
|
||||
timezone: timezone,
|
||||
},
|
||||
},
|
||||
y: 1,
|
||||
_id: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
t: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
import { runQuery, CLICKHOUSE, PRISMA, MONGODB } from 'lib/db';
|
||||
|
||||
export function getEvents(...args: [websiteId: string, startAt: Date, eventType: number]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[MONGODB]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } from 'lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import { CLICKHOUSE, MONGODB, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import { uuid } from 'lib/crypto';
|
||||
|
|
@ -29,6 +29,7 @@ export async function saveEvent(args: {
|
|||
}) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(args),
|
||||
[MONGODB]: () => relationalQuery(args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(args),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue