mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-01 20:39:44 +01:00
Split out session query.
This commit is contained in:
parent
7b9c29e039
commit
4497951000
@ -2,9 +2,7 @@ import { Container } from 'react-basics';
|
|||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
import NavBar from 'components/layout/NavBar';
|
import NavBar from 'components/layout/NavBar';
|
||||||
import UpdateNotice from 'components/common/UpdateNotice';
|
import UpdateNotice from 'components/common/UpdateNotice';
|
||||||
import useRequireLogin from 'hooks/useRequireLogin';
|
import { useRequireLogin, useConfig } from 'hooks';
|
||||||
import useConfig from 'hooks/useConfig';
|
|
||||||
import { CURRENT_VERSION } from 'lib/constants';
|
|
||||||
import styles from './AppLayout.module.css';
|
import styles from './AppLayout.module.css';
|
||||||
|
|
||||||
export function AppLayout({ title, children }) {
|
export function AppLayout({ title, children }) {
|
||||||
@ -16,7 +14,7 @@ export function AppLayout({ title, children }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.layout} data-app-version={CURRENT_VERSION}>
|
<div className={styles.layout}>
|
||||||
<UpdateNotice user={user} config={config} />
|
<UpdateNotice user={user} config={config} />
|
||||||
<Head>
|
<Head>
|
||||||
<title>{title ? `${title} | umami` : 'umami'}</title>
|
<title>{title ? `${title} | umami` : 'umami'}</title>
|
||||||
|
@ -61,14 +61,13 @@ function getDateFormat(date) {
|
|||||||
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
|
return `'${dateFormat(date, 'UTC:yyyy-mm-dd HH:MM:ss')}'`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilterQuery(filters = {}, params = {}) {
|
function getFilterQuery(filters = {}) {
|
||||||
const query = Object.keys(filters).reduce((arr, key) => {
|
const query = Object.keys(filters).reduce((arr, key) => {
|
||||||
const filter = filters[key];
|
const filter = filters[key];
|
||||||
|
|
||||||
if (filter !== undefined) {
|
if (filter !== undefined) {
|
||||||
const column = FILTER_COLUMNS[key] || key;
|
const column = FILTER_COLUMNS[key] || key;
|
||||||
arr.push(`and ${column} = {${key}:String}`);
|
arr.push(`and ${column} = {${key}:String}`);
|
||||||
params[key] = decodeURIComponent(filter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
@ -77,9 +76,9 @@ function getFilterQuery(filters = {}, params = {}) {
|
|||||||
return query.join('\n');
|
return query.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseFilters(filters: WebsiteMetricFilter = {}, params: any = {}) {
|
function parseFilters(filters: WebsiteMetricFilter = {}) {
|
||||||
return {
|
return {
|
||||||
filterQuery: getFilterQuery(filters, params),
|
filterQuery: getFilterQuery(filters),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import prisma from '@umami/prisma-client';
|
import prisma from '@umami/prisma-client';
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
|
import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db';
|
||||||
import { FILTER_COLUMNS } from './constants';
|
import { FILTER_COLUMNS, SESSION_COLUMNS } from './constants';
|
||||||
|
|
||||||
const MYSQL_DATE_FORMATS = {
|
const MYSQL_DATE_FORMATS = {
|
||||||
minute: '%Y-%m-%d %H:%i:00',
|
minute: '%Y-%m-%d %H:%i:00',
|
||||||
@ -64,14 +64,13 @@ function getTimestampIntervalQuery(field: string): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilterQuery(filters = {}, params = []): string {
|
function getFilterQuery(filters = {}): string {
|
||||||
const query = Object.keys(filters).reduce((arr, key) => {
|
const query = Object.keys(filters).reduce((arr, key) => {
|
||||||
const filter = filters[key];
|
const filter = filters[key];
|
||||||
|
|
||||||
if (filter !== undefined) {
|
if (filter !== undefined) {
|
||||||
const column = FILTER_COLUMNS[key] || key;
|
const column = FILTER_COLUMNS[key] || key;
|
||||||
arr.push(`and ${column}={{${key}}}`);
|
arr.push(`and ${column}={{${key}}}`);
|
||||||
params.push(decodeURIComponent(filter));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
@ -80,19 +79,12 @@ function getFilterQuery(filters = {}, params = []): string {
|
|||||||
return query.join('\n');
|
return query.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseFilters(
|
function parseFilters(filters: { [key: string]: any } = {}) {
|
||||||
filters: { [key: string]: any } = {},
|
|
||||||
params = [],
|
|
||||||
sessionKey = 'session_id',
|
|
||||||
) {
|
|
||||||
const { os, browser, device, country, region, city } = filters;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
joinSession:
|
joinSession: Object.keys(filters).find(key => SESSION_COLUMNS[key])
|
||||||
os || browser || device || country || region || city
|
? `inner join session on website_event.session_id = session.session_id`
|
||||||
? `inner join session on website_event.${sessionKey} = session.${sessionKey}`
|
|
||||||
: '',
|
: '',
|
||||||
filterQuery: getFilterQuery(filters, params),
|
filterQuery: getFilterQuery(filters),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ export default async (
|
|||||||
|
|
||||||
filters[type] = undefined;
|
filters[type] = undefined;
|
||||||
|
|
||||||
let data = await getSessionMetrics(websiteId, {
|
const data = await getSessionMetrics(websiteId, {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
column,
|
column,
|
||||||
@ -88,7 +88,7 @@ export default async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data = Object.values(combined);
|
return ok(res, Object.values(combined));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ok(res, data);
|
return ok(res, data);
|
||||||
|
@ -6,6 +6,7 @@ import { canViewWebsite } from 'lib/auth';
|
|||||||
import { useAuth, useCors } from 'lib/middleware';
|
import { useAuth, useCors } from 'lib/middleware';
|
||||||
import { getPageviewStats } from 'queries';
|
import { getPageviewStats } from 'queries';
|
||||||
import { parseDateRangeQuery } from 'lib/query';
|
import { parseDateRangeQuery } from 'lib/query';
|
||||||
|
import { getSessionStats } from '../../../../queries/analytics/sessions/getSessionStats';
|
||||||
|
|
||||||
export interface WebsitePageviewRequestQuery {
|
export interface WebsitePageviewRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
@ -62,7 +63,6 @@ export default async (
|
|||||||
endDate,
|
endDate,
|
||||||
timezone,
|
timezone,
|
||||||
unit,
|
unit,
|
||||||
count: '*',
|
|
||||||
filters: {
|
filters: {
|
||||||
url,
|
url,
|
||||||
referrer,
|
referrer,
|
||||||
@ -75,14 +75,14 @@ export default async (
|
|||||||
city,
|
city,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
getPageviewStats(websiteId, {
|
getSessionStats(websiteId, {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
timezone,
|
timezone,
|
||||||
unit,
|
unit,
|
||||||
count: 'distinct website_event.',
|
|
||||||
filters: {
|
filters: {
|
||||||
url,
|
url,
|
||||||
|
referrer,
|
||||||
title,
|
title,
|
||||||
os,
|
os,
|
||||||
browser,
|
browser,
|
||||||
|
@ -84,6 +84,7 @@ async function clickhouseQuery(
|
|||||||
const { rawQuery, parseFilters } = clickhouse;
|
const { rawQuery, parseFilters } = clickhouse;
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const params = {
|
const params = {
|
||||||
|
...filters,
|
||||||
websiteId,
|
websiteId,
|
||||||
startDate: maxDate(startDate, website.resetAt),
|
startDate: maxDate(startDate, website.resetAt),
|
||||||
endDate,
|
endDate,
|
||||||
@ -98,7 +99,7 @@ async function clickhouseQuery(
|
|||||||
params.domain = website.domain;
|
params.domain = website.domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { filterQuery } = parseFilters(filters, params);
|
const { filterQuery } = parseFilters(filters);
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
|
@ -10,9 +10,19 @@ export interface PageviewStatsCriteria {
|
|||||||
endDate: Date;
|
endDate: Date;
|
||||||
timezone?: string;
|
timezone?: string;
|
||||||
unit?: string;
|
unit?: string;
|
||||||
count?: string;
|
filters: {
|
||||||
filters: object;
|
url?: string;
|
||||||
sessionKey?: string;
|
referrer?: string;
|
||||||
|
title?: string;
|
||||||
|
browser?: string;
|
||||||
|
os?: string;
|
||||||
|
device?: string;
|
||||||
|
screen?: string;
|
||||||
|
language?: string;
|
||||||
|
country?: string;
|
||||||
|
region?: string;
|
||||||
|
city?: string;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPageviewStats(
|
export async function getPageviewStats(
|
||||||
@ -25,15 +35,7 @@ export async function getPageviewStats(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function relationalQuery(websiteId: string, criteria: PageviewStatsCriteria) {
|
async function relationalQuery(websiteId: string, criteria: PageviewStatsCriteria) {
|
||||||
const {
|
const { startDate, endDate, timezone = 'utc', unit = 'day', filters = {} } = criteria;
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
timezone = 'utc',
|
|
||||||
unit = 'day',
|
|
||||||
count = '*',
|
|
||||||
filters = {},
|
|
||||||
sessionKey = 'session_id',
|
|
||||||
} = criteria;
|
|
||||||
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const { filterQuery, joinSession } = parseFilters(filters);
|
const { filterQuery, joinSession } = parseFilters(filters);
|
||||||
@ -42,7 +44,7 @@ async function relationalQuery(websiteId: string, criteria: PageviewStatsCriteri
|
|||||||
`
|
`
|
||||||
select
|
select
|
||||||
${getDateQuery('website_event.created_at', unit, timezone)} x,
|
${getDateQuery('website_event.created_at', unit, timezone)} x,
|
||||||
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
count(*) y
|
||||||
from website_event
|
from website_event
|
||||||
${joinSession}
|
${joinSession}
|
||||||
where website_event.website_id = {{websiteId::uuid}}
|
where website_event.website_id = {{websiteId::uuid}}
|
||||||
@ -52,24 +54,17 @@ async function relationalQuery(websiteId: string, criteria: PageviewStatsCriteri
|
|||||||
group by 1
|
group by 1
|
||||||
`,
|
`,
|
||||||
{
|
{
|
||||||
|
...filters,
|
||||||
websiteId,
|
websiteId,
|
||||||
startDate: maxDate(startDate, website.resetAt),
|
startDate: maxDate(startDate, website.resetAt),
|
||||||
endDate,
|
endDate,
|
||||||
eventType: EVENT_TYPE.pageView,
|
eventType: EVENT_TYPE.pageView,
|
||||||
...filters,
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function clickhouseQuery(websiteId: string, criteria: PageviewStatsCriteria) {
|
async function clickhouseQuery(websiteId: string, criteria: PageviewStatsCriteria) {
|
||||||
const {
|
const { startDate, endDate, timezone = 'UTC', unit = 'day', filters = {} } = criteria;
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
timezone = 'UTC',
|
|
||||||
unit = 'day',
|
|
||||||
count = '*',
|
|
||||||
filters = {},
|
|
||||||
} = criteria;
|
|
||||||
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery } = clickhouse;
|
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery } = clickhouse;
|
||||||
const website = await loadWebsite(websiteId);
|
const website = await loadWebsite(websiteId);
|
||||||
const { filterQuery } = parseFilters(filters);
|
const { filterQuery } = parseFilters(filters);
|
||||||
@ -82,7 +77,7 @@ async function clickhouseQuery(websiteId: string, criteria: PageviewStatsCriteri
|
|||||||
from (
|
from (
|
||||||
select
|
select
|
||||||
${getDateQuery('created_at', unit, timezone)} as t,
|
${getDateQuery('created_at', unit, timezone)} as t,
|
||||||
count(${count !== '*' ? 'distinct session_id' : count}) as y
|
count(*) as y
|
||||||
from website_event
|
from website_event
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
||||||
|
@ -28,8 +28,8 @@ async function relationalQuery(
|
|||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`select ${column} x, count(*) y
|
`select ${column} x, count(*) y
|
||||||
from session as x
|
from session as s
|
||||||
where x.session_id in (
|
where s.session_id in (
|
||||||
select website_event.session_id
|
select website_event.session_id
|
||||||
from website_event
|
from website_event
|
||||||
join website
|
join website
|
||||||
@ -38,7 +38,7 @@ async function relationalQuery(
|
|||||||
where website.website_id = {{websiteId::uuid}}
|
where website.website_id = {{websiteId::uuid}}
|
||||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
)
|
) as t
|
||||||
group by 1
|
group by 1
|
||||||
order by 2 desc
|
order by 2 desc
|
||||||
limit 100`,
|
limit 100`,
|
||||||
@ -64,7 +64,7 @@ async function clickhouseQuery(
|
|||||||
`
|
`
|
||||||
select
|
select
|
||||||
${column} x, count(distinct session_id) y
|
${column} x, count(distinct session_id) y
|
||||||
from website_event as x
|
from website_event
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
||||||
and event_type = {eventType:UInt32}
|
and event_type = {eventType:UInt32}
|
||||||
|
98
queries/analytics/sessions/getSessionStats.ts
Normal file
98
queries/analytics/sessions/getSessionStats.ts
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
import clickhouse from 'lib/clickhouse';
|
||||||
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||||
|
import prisma from 'lib/prisma';
|
||||||
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
|
import { loadWebsite } from 'lib/load';
|
||||||
|
import { maxDate } from 'lib/date';
|
||||||
|
|
||||||
|
export interface SessionStatsCriteria {
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
timezone?: string;
|
||||||
|
unit?: string;
|
||||||
|
filters: {
|
||||||
|
url?: string;
|
||||||
|
referrer?: string;
|
||||||
|
title?: string;
|
||||||
|
browser?: string;
|
||||||
|
os?: string;
|
||||||
|
device?: string;
|
||||||
|
screen?: string;
|
||||||
|
language?: string;
|
||||||
|
country?: string;
|
||||||
|
region?: string;
|
||||||
|
city?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getSessionStats(
|
||||||
|
...args: [websiteId: string, criteria: SessionStatsCriteria]
|
||||||
|
) {
|
||||||
|
return runQuery({
|
||||||
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function relationalQuery(websiteId: string, criteria: SessionStatsCriteria) {
|
||||||
|
const { startDate, endDate, timezone = 'utc', unit = 'day', filters = {} } = criteria;
|
||||||
|
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const { filterQuery, joinSession } = parseFilters(filters);
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`
|
||||||
|
select
|
||||||
|
${getDateQuery('website_event.created_at', unit, timezone)} x,
|
||||||
|
count(distinct website_event.session_id) y
|
||||||
|
from website_event
|
||||||
|
${joinSession}
|
||||||
|
where website_event.website_id = {{websiteId::uuid}}
|
||||||
|
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||||
|
and event_type = {{eventType}}
|
||||||
|
${filterQuery}
|
||||||
|
group by 1
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
...filters,
|
||||||
|
websiteId,
|
||||||
|
startDate: maxDate(startDate, website.resetAt),
|
||||||
|
endDate,
|
||||||
|
eventType: EVENT_TYPE.pageView,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(websiteId: string, criteria: SessionStatsCriteria) {
|
||||||
|
const { startDate, endDate, timezone = 'UTC', unit = 'day', filters = {} } = criteria;
|
||||||
|
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery } = clickhouse;
|
||||||
|
const website = await loadWebsite(websiteId);
|
||||||
|
const { filterQuery } = parseFilters(filters);
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`
|
||||||
|
select
|
||||||
|
${getDateStringQuery('g.t', unit)} as x,
|
||||||
|
g.y as y
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
${getDateQuery('created_at', unit, timezone)} as t,
|
||||||
|
count(distinct session_id) as y
|
||||||
|
from website_event
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
||||||
|
and event_type = {eventType:UInt32}
|
||||||
|
${filterQuery}
|
||||||
|
group by t
|
||||||
|
) as g
|
||||||
|
order by t
|
||||||
|
`,
|
||||||
|
{
|
||||||
|
...filters,
|
||||||
|
websiteId,
|
||||||
|
startDate: maxDate(startDate, website.resetAt),
|
||||||
|
endDate,
|
||||||
|
eventType: EVENT_TYPE.pageView,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user