mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-11 08:09:38 +01:00
WebsiteId query param. Fix Prisma filters.
This commit is contained in:
parent
f654e6728c
commit
b4bd988e4e
@ -211,7 +211,6 @@ export default function EventDataForm({ websiteId, onClose, className }) {
|
||||
<TextField />
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
+
|
||||
<FormButtons className={styles.formButtons}>
|
||||
<Button variant="action" type="submit">
|
||||
<FormattedMessage id="label.add-filter" defaultMessage="Add Filter" />
|
||||
|
@ -19,6 +19,18 @@ const POSTGRESQL_DATE_FORMATS = {
|
||||
year: 'YYYY-01-01',
|
||||
};
|
||||
|
||||
function toUuid(): string {
|
||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
||||
|
||||
if (db === POSTGRESQL) {
|
||||
return '::uuid';
|
||||
}
|
||||
|
||||
if (db === MYSQL) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function getDateQuery(field: string, unit: string, timezone?: string): string {
|
||||
const db = getDatabaseType(process.env.DATABASE_URL);
|
||||
|
||||
@ -173,7 +185,7 @@ function parseFilters(
|
||||
event: { eventName },
|
||||
joinSession:
|
||||
os || browser || device || country
|
||||
? `inner join session on ${sessionKey} = session.${sessionKey}`
|
||||
? `inner join session on website_event.${sessionKey} = session.${sessionKey}`
|
||||
: '',
|
||||
filterQuery: getFilterQuery(filters, params),
|
||||
};
|
||||
@ -198,6 +210,7 @@ export default {
|
||||
getFilterQuery,
|
||||
getEventDataColumnsQuery,
|
||||
getEventDataFilterQuery,
|
||||
toUuid,
|
||||
parseFilters,
|
||||
rawQuery,
|
||||
};
|
||||
|
@ -77,7 +77,7 @@ export default async (
|
||||
endDate,
|
||||
timezone,
|
||||
unit,
|
||||
count: 'distinct pageview.',
|
||||
count: 'distinct website_event.',
|
||||
filters: {
|
||||
url,
|
||||
os,
|
||||
|
@ -38,23 +38,23 @@ async function relationalQuery(
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, eventName, columns, filters } = data;
|
||||
const { rawQuery, getEventDataColumnsQuery, getEventDataFilterQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { toUuid, rawQuery, getEventDataColumnsQuery, getEventDataFilterQuery } = prisma;
|
||||
const params: any = [websiteId, startDate, endDate, eventName];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
${getEventDataColumnsQuery('event_data', columns)}
|
||||
from website_event
|
||||
where website_id ='${websiteId}'
|
||||
and created_at between $1 and $2
|
||||
where website_id = $1${toUuid()}
|
||||
and created_at between $2 and $3
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
${eventName ? `and eventName = ${eventName}` : ''}
|
||||
${eventName ? `and eventName = $4` : ''}
|
||||
${
|
||||
Object.keys(filters).length > 0
|
||||
? `and ${getEventDataFilterQuery('event_data', filters)}`
|
||||
: ''
|
||||
}`,
|
||||
params as any,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -45,8 +45,8 @@ async function relationalQuery(
|
||||
};
|
||||
},
|
||||
) {
|
||||
const { rawQuery, getDateQuery, getFilterQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { toUuid, rawQuery, getDateQuery, getFilterQuery } = prisma;
|
||||
const params: any = [websiteId, startDate, endDate];
|
||||
|
||||
return rawQuery(
|
||||
`select
|
||||
@ -54,8 +54,8 @@ async function relationalQuery(
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from website_event
|
||||
where website_id='${websiteId}'
|
||||
and created_at between $1 and $2
|
||||
where website_id = $1${toUuid()}
|
||||
and created_at between $2 and $3
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
${getFilterQuery(filters, params)}
|
||||
group by 1, 2
|
||||
|
@ -34,8 +34,9 @@ async function relationalQuery(
|
||||
},
|
||||
) {
|
||||
const { startDate, endDate, column, filters = {}, type } = data;
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const { rawQuery, parseFilters, toUuid } = prisma;
|
||||
const params: any = [
|
||||
websiteId,
|
||||
startDate,
|
||||
endDate,
|
||||
type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
@ -46,9 +47,9 @@ async function relationalQuery(
|
||||
`select ${column} x, count(*) y
|
||||
from website_event
|
||||
${joinSession}
|
||||
where website_id='${websiteId}'
|
||||
and website_event.created_at between $1 and $2
|
||||
and event_type = $3
|
||||
where website_event.website_id = $1${toUuid()}
|
||||
and website_event.created_at between $2 and $3
|
||||
and event_type = $4
|
||||
${filterQuery}
|
||||
group by 1
|
||||
order by 2 desc`,
|
||||
|
@ -45,8 +45,8 @@ async function relationalQuery(
|
||||
filters = {},
|
||||
sessionKey = 'session_id',
|
||||
} = data;
|
||||
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { toUuid, getDateQuery, parseFilters, rawQuery } = prisma;
|
||||
const params: any = [websiteId, startDate, endDate];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
@ -54,8 +54,8 @@ async function relationalQuery(
|
||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
||||
from website_event
|
||||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
where website_event.website_id = $1${toUuid()}
|
||||
and website_event.created_at between $2 and $3
|
||||
and event_type = ${EVENT_TYPE.pageView}
|
||||
${filterQuery}
|
||||
group by 1`,
|
||||
@ -90,7 +90,7 @@ async function clickhouseQuery(
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(${count !== '*' ? 'distinct session_id' : count}) y
|
||||
from event
|
||||
where website_id = $1
|
||||
where website_id = $1
|
||||
and rev_id = $2
|
||||
and event_type = ${EVENT_TYPE.pageView}
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
|
@ -20,21 +20,21 @@ async function relationalQuery(
|
||||
data: { startDate: Date; endDate: Date; field: string; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, field, filters = {} } = data;
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { toUuid, parseFilters, rawQuery } = prisma;
|
||||
const params: any = [websiteId, startDate, endDate];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
`select ${field} x, count(*) y
|
||||
from session as x
|
||||
where x.session_id in (
|
||||
select pageview.session_id
|
||||
from pageview
|
||||
select website_event.session_id
|
||||
from website_event
|
||||
join website
|
||||
on pageview.website_id = website.website_id
|
||||
on website_event.website_id = website.website_id
|
||||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
where website.website_id = $1${toUuid()}
|
||||
and website_event.created_at between $2 and $3
|
||||
${filterQuery}
|
||||
)
|
||||
group by 1
|
||||
|
@ -11,16 +11,18 @@ export async function getActiveVisitors(...args: [websiteId: string]) {
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string) {
|
||||
const date = subMinutes(new Date(), 5);
|
||||
const params = [date];
|
||||
const { toUuid, rawQuery } = prisma;
|
||||
|
||||
return prisma.rawQuery(
|
||||
const date = subMinutes(new Date(), 5);
|
||||
const params: any = [websiteId, date];
|
||||
|
||||
return rawQuery(
|
||||
`select count(distinct session_id) x
|
||||
from pageview
|
||||
from website_event
|
||||
join website
|
||||
on pageview.website_id = website.website_id
|
||||
where website.website_id = '${websiteId}'
|
||||
and pageview.created_at >= $1`,
|
||||
on website_event.website_id = website.website_id
|
||||
where website.website_id = $1${toUuid()}
|
||||
and website_event.created_at >= $2`,
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
||||
import cache from 'lib/cache';
|
||||
import { EVENT_TYPE } from 'lib/constants';
|
||||
|
||||
export async function getWebsiteStats(
|
||||
...args: [websiteId: string, data: { startDate: Date; endDate: Date; filters: object }]
|
||||
@ -17,8 +18,8 @@ async function relationalQuery(
|
||||
data: { startDate: Date; endDate: Date; filters: object },
|
||||
) {
|
||||
const { startDate, endDate, filters = {} } = data;
|
||||
const { getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
|
||||
const params = [startDate, endDate];
|
||||
const { toUuid, getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
|
||||
const params: any = [websiteId, startDate, endDate];
|
||||
const { filterQuery, joinSession } = parseFilters(filters, params);
|
||||
|
||||
return rawQuery(
|
||||
@ -27,16 +28,16 @@ async function relationalQuery(
|
||||
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
|
||||
sum(t.time) as "totaltime"
|
||||
from (
|
||||
select pageview.session_id,
|
||||
${getDateQuery('pageview.created_at', 'hour')},
|
||||
select website_event.session_id,
|
||||
${getDateQuery('website_event.created_at', 'hour')},
|
||||
count(*) c,
|
||||
${getTimestampInterval('pageview.created_at')} as "time"
|
||||
from pageview
|
||||
${getTimestampInterval('website_event.created_at')} as "time"
|
||||
from website_event
|
||||
join website
|
||||
on pageview.website_id = website.website_id
|
||||
on website_event.website_id = website.website_id
|
||||
${joinSession}
|
||||
where website.website_id='${websiteId}'
|
||||
and pageview.created_at between $1 and $2
|
||||
where website.website_id = $1${toUuid()}
|
||||
and website_event.created_at between $2 and $3
|
||||
${filterQuery}
|
||||
group by 1, 2
|
||||
) t`,
|
||||
@ -67,7 +68,7 @@ async function clickhouseQuery(
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from event
|
||||
where event_type = 1
|
||||
where event_type = ${EVENT_TYPE.pageView}
|
||||
and website_id = $1
|
||||
and rev_id = $2
|
||||
and ${getBetweenDates('created_at', startDate, endDate)}
|
||||
|
Loading…
Reference in New Issue
Block a user