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,72 +1,31 @@
|
|||
import { subMinutes } from 'date-fns';
|
||||
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';
|
||||
|
||||
export async function getActiveVisitors(...args: [websiteId: string]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[MONGODB]: () => mongodbQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string) {
|
||||
const { getDatabaseType, toUuid, rawQuery, client } = prisma;
|
||||
const db = getDatabaseType();
|
||||
const { toUuid, rawQuery } = prisma;
|
||||
|
||||
const date = subMinutes(new Date(), 5);
|
||||
const params: any = [websiteId, date];
|
||||
|
||||
if (db === 'mongodb') {
|
||||
const result: any = await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$eq: ['$website_id', websiteId],
|
||||
},
|
||||
{
|
||||
$gte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: date.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$session_id',
|
||||
},
|
||||
},
|
||||
{
|
||||
$count: 'x',
|
||||
},
|
||||
],
|
||||
});
|
||||
if (result.length > 0) {
|
||||
return { x: result[0].x };
|
||||
} else {
|
||||
return { x: 0 };
|
||||
}
|
||||
} else {
|
||||
return rawQuery(
|
||||
`select count(distinct session_id) x
|
||||
return rawQuery(
|
||||
`select count(distinct session_id) x
|
||||
from website_event
|
||||
join website
|
||||
on website_event.website_id = website.website_id
|
||||
where website.website_id = $1${toUuid()}
|
||||
and website_event.created_at >= $2`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string) {
|
||||
|
|
@ -81,3 +40,48 @@ async function clickhouseQuery(websiteId: string) {
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function mongodbQuery(websiteId: string) {
|
||||
const { client } = prisma;
|
||||
|
||||
const date = subMinutes(new Date(), 5);
|
||||
|
||||
const result: any = await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$eq: ['$website_id', websiteId],
|
||||
},
|
||||
{
|
||||
$gte: [
|
||||
'$created_at',
|
||||
{
|
||||
$dateFromString: {
|
||||
dateString: date.toISOString(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$session_id',
|
||||
},
|
||||
},
|
||||
{
|
||||
$count: 'x',
|
||||
},
|
||||
],
|
||||
});
|
||||
if (result.length > 0) {
|
||||
return { x: result[0].x };
|
||||
} else {
|
||||
return { x: 0 };
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
||||
|
|
@ -12,6 +12,7 @@ export async function getWebsiteStats(
|
|||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[MONGODB]: () => mongodbQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
|
@ -21,133 +22,13 @@ async function relationalQuery(
|
|||
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = criteria;
|
||||
const {
|
||||
getDatabaseType,
|
||||
toUuid,
|
||||
getDateQuery,
|
||||
getTimestampInterval,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
client,
|
||||
} = prisma;
|
||||
const db = getDatabaseType();
|
||||
const { toUuid, getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||
const params: any = [websiteId, resetDate, startDate, endDate];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
if (db === 'mongodb') {
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$eq: ['$event_type', EVENT_TYPE.pageView],
|
||||
},
|
||||
{
|
||||
$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: {
|
||||
session_id: '$session_id',
|
||||
hour: {
|
||||
$toString: { $hour: '$created_at' },
|
||||
},
|
||||
created_at: '$created_at',
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
$concat: ['$session_id', ':', '$hour'],
|
||||
},
|
||||
session_id: { $first: '$session_id' },
|
||||
hour: { $first: '$hour' },
|
||||
count: { $sum: 1 },
|
||||
timeMax: { $max: '$created_at' },
|
||||
timeMin: { $min: '$created_at' },
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: '$_id',
|
||||
session_id: '$session_id',
|
||||
hour: '$hour',
|
||||
count: '$count',
|
||||
time: {
|
||||
$dateDiff: {
|
||||
endDate: '$timeMax',
|
||||
startDate: '$timeMin',
|
||||
unit: 'second',
|
||||
},
|
||||
},
|
||||
bounce: {
|
||||
$cond: {
|
||||
if: { $eq: ['$count', 1] },
|
||||
then: 1,
|
||||
else: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$session_id',
|
||||
pageviews: { $sum: '$count' },
|
||||
bounces: { $sum: '$bounce' },
|
||||
totaltime: { $sum: '$time' },
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '',
|
||||
pageviews: { $sum: '$pageviews' },
|
||||
uniques: { $sum: 1 },
|
||||
bounces: { $sum: '$bounces' },
|
||||
totaltime: { $sum: '$totaltime' },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
} else {
|
||||
return rawQuery(
|
||||
`select sum(t.c) as "pageviews",
|
||||
return rawQuery(
|
||||
`select sum(t.c) as "pageviews",
|
||||
count(distinct t.session_id) as "uniques",
|
||||
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
||||
sum(t.time) as "totaltime"
|
||||
|
|
@ -167,9 +48,8 @@ async function relationalQuery(
|
|||
${filterQuery}
|
||||
group by 1, 2
|
||||
) t`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
@ -206,3 +86,124 @@ async function clickhouseQuery(
|
|||
params,
|
||||
);
|
||||
}
|
||||
|
||||
async function mongodbQuery(
|
||||
websiteId: string,
|
||||
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = criteria;
|
||||
const { 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.pageView],
|
||||
},
|
||||
{
|
||||
$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: {
|
||||
session_id: '$session_id',
|
||||
hour: {
|
||||
$toString: { $hour: '$created_at' },
|
||||
},
|
||||
created_at: '$created_at',
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
$concat: ['$session_id', ':', '$hour'],
|
||||
},
|
||||
session_id: { $first: '$session_id' },
|
||||
hour: { $first: '$hour' },
|
||||
count: { $sum: 1 },
|
||||
timeMax: { $max: '$created_at' },
|
||||
timeMin: { $min: '$created_at' },
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: '$_id',
|
||||
session_id: '$session_id',
|
||||
hour: '$hour',
|
||||
count: '$count',
|
||||
time: {
|
||||
$dateDiff: {
|
||||
endDate: '$timeMax',
|
||||
startDate: '$timeMin',
|
||||
unit: 'second',
|
||||
},
|
||||
},
|
||||
bounce: {
|
||||
$cond: {
|
||||
if: { $eq: ['$count', 1] },
|
||||
then: 1,
|
||||
else: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$session_id',
|
||||
pageviews: { $sum: '$count' },
|
||||
bounces: { $sum: '$bounce' },
|
||||
totaltime: { $sum: '$time' },
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '',
|
||||
pageviews: { $sum: '$pageviews' },
|
||||
uniques: { $sum: 1 },
|
||||
bounces: { $sum: '$bounces' },
|
||||
totaltime: { $sum: '$totaltime' },
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue