mirror of
https://github.com/umami-software/umami.git
synced 2026-02-07 22:27:16 +01:00
MySQL query optimization. Added loading component.
This commit is contained in:
parent
a7e7469d22
commit
ccb98f836f
13 changed files with 158 additions and 109 deletions
|
|
@ -5,10 +5,28 @@ import { subMinutes } from 'date-fns';
|
|||
const POSTGRESQL = 'postgresql';
|
||||
const MYSQL = 'mysql';
|
||||
|
||||
const DATE_FORMATS = {
|
||||
minute: '%Y-%m-%d %H:%i:00',
|
||||
hour: '%Y-%m-%d %H:00:00',
|
||||
day: '%Y-%m-%d',
|
||||
month: '%Y-%m-01',
|
||||
year: '%Y-01-01',
|
||||
};
|
||||
|
||||
export function getDatabase() {
|
||||
return process.env.DATABASE_TYPE || process.env.DATABASE_URL.split(':')[0];
|
||||
}
|
||||
|
||||
export function getDateQuery(field, unit, timezone) {
|
||||
if (timezone) {
|
||||
const tz = moment.tz(timezone).format('Z');
|
||||
|
||||
return `DATE_FORMAT(convert_tz(${field},'+00:00','${tz}'), '${DATE_FORMATS[unit]}')`;
|
||||
}
|
||||
|
||||
return `DATE_FORMAT(${field}, '${DATE_FORMATS[unit]}')`;
|
||||
}
|
||||
|
||||
export async function getWebsiteById(website_id) {
|
||||
return runQuery(
|
||||
prisma.website.findOne({
|
||||
|
|
@ -264,7 +282,7 @@ export function getMetrics(website_id, start_at, end_at) {
|
|||
sum(t.time) as "totaltime"
|
||||
from (
|
||||
select session_id,
|
||||
date_trunc('hour', created_at),
|
||||
${getDateQuery('created_at', 'hour')},
|
||||
count(*) c,
|
||||
floor(unix_timestamp(max(created_at)) - unix_timestamp(min(created_at))) as "time"
|
||||
from pageview
|
||||
|
|
@ -296,7 +314,7 @@ export function getPageviews(
|
|||
return prisma.$queryRaw(
|
||||
`
|
||||
select date_trunc('${unit}', created_at at time zone '${timezone}') t,
|
||||
count(${count}) y
|
||||
count(${count}) y
|
||||
from pageview
|
||||
where website_id=$1
|
||||
and created_at between $2 and $3
|
||||
|
|
@ -310,11 +328,10 @@ export function getPageviews(
|
|||
}
|
||||
|
||||
if (db === MYSQL) {
|
||||
const tz = moment.tz(timezone).format('Z');
|
||||
return prisma.$queryRaw(
|
||||
`
|
||||
select date_trunc('${unit}', convert_tz(created_at,'+00:00','${tz}')) t,
|
||||
count(${count}) y
|
||||
select ${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(${count}) y
|
||||
from pageview
|
||||
where website_id=?
|
||||
and created_at between ? and ?
|
||||
|
|
@ -424,12 +441,11 @@ export function getEvents(website_id, start_at, end_at, timezone = 'utc', unit =
|
|||
}
|
||||
|
||||
if (db === MYSQL) {
|
||||
const tz = moment.tz(timezone).format('Z');
|
||||
return prisma.$queryRaw(
|
||||
`
|
||||
select
|
||||
event_value x,
|
||||
date_trunc('${unit}', convert_tz(created_at,'+00:00','${tz}')) t,
|
||||
${getDateQuery('created_at', unit, timezone)} t,
|
||||
count(*) y
|
||||
from event
|
||||
where website_id=?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue