mirror of
https://github.com/umami-software/umami.git
synced 2026-02-21 04:55:36 +01:00
feat: support mongodb connection
This commit is contained in:
parent
586529a5ca
commit
bb30b43a8e
9 changed files with 370 additions and 30 deletions
|
|
@ -3,6 +3,16 @@ import cache from 'lib/cache';
|
|||
import { ROLES } from 'lib/constants';
|
||||
import prisma from 'lib/prisma';
|
||||
import { Website, User, Roles } from 'lib/types';
|
||||
import { getDatabaseType } from '../../lib/db';
|
||||
|
||||
function whereDeletedAtNotNull() {
|
||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
||||
if (db === 'mongodb') {
|
||||
return { isSet: false };
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUser(
|
||||
where: Prisma.UserWhereInput | Prisma.UserWhereUniqueInput,
|
||||
|
|
@ -11,7 +21,14 @@ export async function getUser(
|
|||
const { includePassword = false, showDeleted = false } = options;
|
||||
|
||||
return prisma.client.user.findFirst({
|
||||
where: { ...where, ...(showDeleted ? {} : { deletedAt: null }) },
|
||||
where: {
|
||||
...where,
|
||||
...(showDeleted
|
||||
? {}
|
||||
: {
|
||||
deletedAt: whereDeletedAtNotNull(),
|
||||
}),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
|
|
@ -26,7 +43,7 @@ export async function getUsers(): Promise<User[]> {
|
|||
return prisma.client.user.findMany({
|
||||
take: 100,
|
||||
where: {
|
||||
deletedAt: null,
|
||||
deletedAt: whereDeletedAtNotNull(),
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
|
|
@ -76,7 +93,7 @@ export async function getUserWebsites(userId: string): Promise<Website[]> {
|
|||
return prisma.client.website.findMany({
|
||||
where: {
|
||||
userId,
|
||||
deletedAt: null,
|
||||
deletedAt: whereDeletedAtNotNull(),
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -45,14 +45,34 @@ async function relationalQuery(
|
|||
filters = {},
|
||||
sessionKey = 'session_id',
|
||||
} = criteria;
|
||||
const { toUuid, getDateQuery, parseFilters, rawQuery } = prisma;
|
||||
const { getDatabaseType, toUuid, getDateQuery, parseFilters, rawQuery, 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];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${getDateQuery('website_event.created_at', unit, timezone)} x,
|
||||
//TODO: 구현해야 함
|
||||
|
||||
if (db === 'mongodb') {
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$project: {
|
||||
x: {
|
||||
$dateToString: {
|
||||
date: '$created_at',
|
||||
timezone: timezone,
|
||||
format: getDateQuery('website_event.created_at', unit, timezone),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
} else {
|
||||
return rawQuery(
|
||||
`select ${getDateQuery('website_event.created_at', unit, timezone)} x,
|
||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||
from website_event
|
||||
${joinSession}
|
||||
|
|
@ -62,8 +82,9 @@ async function relationalQuery(
|
|||
and event_type = ${EVENT_TYPE.pageView}
|
||||
${filterQuery}
|
||||
group by 1`,
|
||||
params,
|
||||
);
|
||||
params,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
|
|||
|
|
@ -11,20 +11,62 @@ export async function getActiveVisitors(...args: [websiteId: string]) {
|
|||
}
|
||||
|
||||
async function relationalQuery(websiteId: string) {
|
||||
const { toUuid, rawQuery } = prisma;
|
||||
const { getDatabaseType, toUuid, rawQuery, client } = prisma;
|
||||
const db = getDatabaseType();
|
||||
|
||||
const date = subMinutes(new Date(), 5);
|
||||
const params: any = [websiteId, date];
|
||||
|
||||
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,
|
||||
);
|
||||
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
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string) {
|
||||
|
|
|
|||
|
|
@ -21,14 +21,89 @@ async function relationalQuery(
|
|||
criteria: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = criteria;
|
||||
const { toUuid, getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
|
||||
const {
|
||||
getDatabaseType,
|
||||
toUuid,
|
||||
getDateQuery,
|
||||
getTimestampInterval,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
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];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select sum(t.c) as "pageviews",
|
||||
if (db === 'mongodb') {
|
||||
return await client.websiteEvent.aggregateRaw({
|
||||
pipeline: [
|
||||
{
|
||||
$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",
|
||||
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"
|
||||
|
|
@ -48,8 +123,9 @@ async function relationalQuery(
|
|||
${filterQuery}
|
||||
group by 1, 2
|
||||
) t`,
|
||||
params,
|
||||
);
|
||||
params,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue