mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
optimize queries
This commit is contained in:
parent
3139fe0c6d
commit
3023890cba
@ -21,24 +21,36 @@ export function getDatabase() {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getDateStringQuery(data, unit) {
|
||||||
|
const db = getDatabase();
|
||||||
|
|
||||||
|
if (db === POSTGRESQL) {
|
||||||
|
return `to_char(${data}, '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (db === MYSQL) {
|
||||||
|
return `DATE_FORMAT(${data}, '${MYSQL_DATE_FORMATS[unit]}')`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function getDateQuery(field, unit, timezone) {
|
export function getDateQuery(field, unit, timezone) {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
|
|
||||||
if (db === POSTGRESQL) {
|
if (db === POSTGRESQL) {
|
||||||
if (timezone) {
|
if (timezone) {
|
||||||
return `to_char(date_trunc('${unit}', ${field} at time zone '${timezone}'), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
return `date_trunc('${unit}', ${field} at time zone '${timezone}')`;
|
||||||
}
|
}
|
||||||
return `to_char(date_trunc('${unit}', ${field}), '${POSTGRESQL_DATE_FORMATS[unit]}')`;
|
return `date_trunc('${unit}', ${field})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (db === MYSQL) {
|
if (db === MYSQL) {
|
||||||
if (timezone) {
|
if (timezone) {
|
||||||
const tz = moment.tz(timezone).format('Z');
|
const tz = moment.tz(timezone).format('Z');
|
||||||
|
|
||||||
return `DATE_FORMAT(convert_tz(${field},'+00:00','${tz}'), '${MYSQL_DATE_FORMATS[unit]}')`;
|
return `convert_tz(${field},'+00:00','${tz}')`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `DATE_FORMAT(${field}, '${MYSQL_DATE_FORMATS[unit]}')`;
|
return `${field}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -438,7 +450,7 @@ export function getWebsiteStats(website_id, start_at, end_at, filters = {}) {
|
|||||||
sum(t.time) as "totaltime"
|
sum(t.time) as "totaltime"
|
||||||
from (
|
from (
|
||||||
select pageview.session_id,
|
select pageview.session_id,
|
||||||
${getDateQuery('pageview.created_at', 'hour')},
|
${getDateQuery('pageview.created_at', 'hour', false, 'date')},
|
||||||
count(*) c,
|
count(*) c,
|
||||||
${getTimestampInterval('pageview.created_at')} as "time"
|
${getTimestampInterval('pageview.created_at')} as "time"
|
||||||
from pageview
|
from pageview
|
||||||
@ -467,16 +479,20 @@ export function getPageviewStats(
|
|||||||
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
const { pageviewQuery, sessionQuery, joinSession } = parseFilters('pageview', filters, params);
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`--getPageviewStats
|
||||||
select ${getDateQuery('pageview.created_at', unit, timezone)} t,
|
select
|
||||||
count(${count}) y
|
${getDateStringQuery('g.t', unit)} as t,
|
||||||
from pageview
|
g.y as y
|
||||||
${joinSession}
|
from
|
||||||
where pageview.website_id=$1
|
(select ${getDateQuery('pageview.created_at', unit, timezone)} t,
|
||||||
and pageview.created_at between $2 and $3
|
count(${count}) y
|
||||||
${pageviewQuery}
|
from pageview
|
||||||
${sessionQuery}
|
${joinSession}
|
||||||
group by 1
|
where pageview.website_id=$1
|
||||||
|
and pageview.created_at between $2 and $3
|
||||||
|
${pageviewQuery}
|
||||||
|
${sessionQuery}
|
||||||
|
group by 1) g
|
||||||
order by 1
|
order by 1
|
||||||
`,
|
`,
|
||||||
params,
|
params,
|
||||||
@ -559,9 +575,10 @@ export function getEventMetrics(
|
|||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
|
--getEventMetrics
|
||||||
select
|
select
|
||||||
event_value x,
|
event_value x,
|
||||||
${getDateQuery('created_at', unit, timezone)} t,
|
${getDateStringQuery(getDateQuery('created_at', unit, timezone), unit)} t,
|
||||||
count(*) y
|
count(*) y
|
||||||
from event
|
from event
|
||||||
where website_id=$1
|
where website_id=$1
|
||||||
|
Loading…
Reference in New Issue
Block a user