mirror of
https://github.com/umami-software/umami.git
synced 2026-02-21 13:05:36 +01:00
feat: sql to mongo pipeline
This commit is contained in:
parent
d5fac29dac
commit
ed29fc08e8
7 changed files with 558 additions and 33 deletions
|
|
@ -47,13 +47,123 @@ async function relationalQuery(
|
|||
},
|
||||
) {
|
||||
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
||||
const { toUuid, rawQuery, getEventDataFilterQuery, getDateQuery } = prisma;
|
||||
const { getDatabaseType, toUuid, rawQuery, getEventDataFilterQuery, getDateQuery, client } =
|
||||
prisma;
|
||||
const db = getDatabaseType();
|
||||
const website = await loadWebsite(websiteId);
|
||||
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
||||
const params: any = [websiteId, resetDate, startDate, endDate, eventName || ''];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
if (db === 'mongodb') {
|
||||
let joinAggregation: any = { match: {} };
|
||||
let matchAggregation: any = { match: {} };
|
||||
let eventTypeProjectProperty = '';
|
||||
let urlProjectProperty = '';
|
||||
if (eventName || urlPath) {
|
||||
joinAggregation = {
|
||||
$lookup: {
|
||||
from: 'website_event',
|
||||
localField: 'website_event_id',
|
||||
foreignField: '_id',
|
||||
as: 'result',
|
||||
},
|
||||
};
|
||||
eventTypeProjectProperty = 'event_name: {$arrayElemAt: ["$result.event_name", 0]}';
|
||||
}
|
||||
if (eventName) {
|
||||
matchAggregation = {
|
||||
$match: {
|
||||
'result.event_name': eventName,
|
||||
},
|
||||
};
|
||||
}
|
||||
if (urlPath) {
|
||||
urlProjectProperty = 'url_path: {$arrayElemAt: ["$result.url_path", 0],}';
|
||||
}
|
||||
let timeProjectProperty = '';
|
||||
if (timeSeries) {
|
||||
timeProjectProperty = `t: $dateTrunc: {date: "$created_at",unit: ${timeSeries.unit}, timezone : ${timeSeries.timezone}`;
|
||||
}
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{
|
||||
$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(),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
joinAggregation,
|
||||
matchAggregation,
|
||||
{
|
||||
$project: {
|
||||
eventTypeProjectProperty,
|
||||
timeProjectProperty,
|
||||
urlProjectProperty,
|
||||
},
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: {
|
||||
url_path: '$url_path',
|
||||
event_name: '$event_name',
|
||||
t: '$t',
|
||||
},
|
||||
x: {
|
||||
$sum: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
url_path: '$_id.url_path',
|
||||
urlPath: '$_id.url_path',
|
||||
event_name: '$_id.event_name',
|
||||
eventName: '$_id.event_name',
|
||||
x: 1,
|
||||
t: '$_id.t',
|
||||
_id: 0,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
} else {
|
||||
return rawQuery(
|
||||
`select
|
||||
count(*) x
|
||||
${eventName ? `,event_name eventName` : ''}
|
||||
${urlPath ? `,url_path urlPath` : ''}
|
||||
|
|
@ -72,8 +182,9 @@ async function relationalQuery(
|
|||
${eventName ? `and eventName = $5` : ''}
|
||||
${getEventDataFilterQuery(filters, params)}
|
||||
${timeSeries ? 'group by t' : ''}`,
|
||||
params,
|
||||
);
|
||||
params,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue