mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-01 12:29:35 +01:00
refactor views and queries for dashboard
This commit is contained in:
parent
bfd5c5f150
commit
f518066d93
@ -58,8 +58,8 @@ export const SESSION_COLUMNS = [
|
|||||||
|
|
||||||
export const FILTER_COLUMNS = {
|
export const FILTER_COLUMNS = {
|
||||||
url: 'url_path',
|
url: 'url_path',
|
||||||
entry: 'url_path',
|
entry: 'entry_url',
|
||||||
exit: 'url_path',
|
exit: 'exit_url',
|
||||||
referrer: 'referrer_domain',
|
referrer: 'referrer_domain',
|
||||||
host: 'hostname',
|
host: 'hostname',
|
||||||
title: 'page_title',
|
title: 'page_title',
|
||||||
|
@ -56,7 +56,7 @@ export default async (
|
|||||||
await useAuth(req, res);
|
await useAuth(req, res);
|
||||||
await useValidate(schema, req, res);
|
await useValidate(schema, req, res);
|
||||||
|
|
||||||
const { websiteId, compare } = req.query;
|
const { websiteId, compare, unit } = req.query;
|
||||||
|
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||||
@ -72,9 +72,13 @@ export default async (
|
|||||||
|
|
||||||
const filters = getRequestFilters(req);
|
const filters = getRequestFilters(req);
|
||||||
|
|
||||||
const metrics = await getWebsiteStats(websiteId, { ...filters, startDate, endDate });
|
const metrics = await getWebsiteStats(websiteId, unit as string, {
|
||||||
|
...filters,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
});
|
||||||
|
|
||||||
const prevPeriod = await getWebsiteStats(websiteId, {
|
const prevPeriod = await getWebsiteStats(websiteId, unit as string, {
|
||||||
...filters,
|
...filters,
|
||||||
startDate: compareStartDate,
|
startDate: compareStartDate,
|
||||||
endDate: compareEndDate,
|
endDate: compareEndDate,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-unused-vars, @typescript-eslint/no-unused-vars */
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
import { EVENT_TYPE } from 'lib/constants';
|
import { EVENT_TYPE } from 'lib/constants';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||||
@ -5,7 +6,7 @@ import prisma from 'lib/prisma';
|
|||||||
import { QueryFilters } from 'lib/types';
|
import { QueryFilters } from 'lib/types';
|
||||||
|
|
||||||
export async function getWebsiteStats(
|
export async function getWebsiteStats(
|
||||||
...args: [websiteId: string, filters: QueryFilters]
|
...args: [websiteId: string, unit: string, filters: QueryFilters]
|
||||||
): Promise<
|
): Promise<
|
||||||
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
|
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
|
||||||
> {
|
> {
|
||||||
@ -17,6 +18,7 @@ export async function getWebsiteStats(
|
|||||||
|
|
||||||
async function relationalQuery(
|
async function relationalQuery(
|
||||||
websiteId: string,
|
websiteId: string,
|
||||||
|
unit: string,
|
||||||
filters: QueryFilters,
|
filters: QueryFilters,
|
||||||
): Promise<
|
): Promise<
|
||||||
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
|
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
|
||||||
@ -57,6 +59,7 @@ async function relationalQuery(
|
|||||||
|
|
||||||
async function clickhouseQuery(
|
async function clickhouseQuery(
|
||||||
websiteId: string,
|
websiteId: string,
|
||||||
|
unit: string,
|
||||||
filters: QueryFilters,
|
filters: QueryFilters,
|
||||||
): Promise<
|
): Promise<
|
||||||
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
|
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
|
||||||
@ -66,29 +69,21 @@ async function clickhouseQuery(
|
|||||||
...filters,
|
...filters,
|
||||||
eventType: EVENT_TYPE.pageView,
|
eventType: EVENT_TYPE.pageView,
|
||||||
});
|
});
|
||||||
|
const table = unit === 'hour' ? 'website_event_stats_hourly' : 'website_event_stats_daily';
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
select
|
select
|
||||||
sum(t.c) as "pageviews",
|
sum(views) as "pageviews",
|
||||||
uniq(t.session_id) as "visitors",
|
uniq(session_id) as "visitors",
|
||||||
uniq(t.visit_id) as "visits",
|
uniq(visit_id) as "visits",
|
||||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
sumIf(1, views = 1) as "bounces",
|
||||||
sum(max_time-min_time) as "totaltime"
|
sum(max_time-min_time) as "totaltime"
|
||||||
from (
|
from ${table} "website_event"
|
||||||
select
|
|
||||||
session_id,
|
|
||||||
visit_id,
|
|
||||||
count(*) c,
|
|
||||||
min(created_at) min_time,
|
|
||||||
max(created_at) max_time
|
|
||||||
from website_event
|
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
and event_type = {eventType:UInt32}
|
and event_type = {eventType:UInt32}
|
||||||
${filterQuery}
|
${filterQuery};
|
||||||
group by session_id, visit_id
|
|
||||||
) as t;
|
|
||||||
`,
|
`,
|
||||||
params,
|
params,
|
||||||
).then(result => {
|
).then(result => {
|
||||||
|
@ -97,34 +97,26 @@ async function clickhouseQuery(
|
|||||||
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||||
});
|
});
|
||||||
|
|
||||||
let entryExitQuery = '';
|
let columnAgg = column;
|
||||||
let excludeDomain = '';
|
let excludeDomain = '';
|
||||||
if (column === 'referrer_domain') {
|
if (column === 'referrer_domain') {
|
||||||
excludeDomain = `and referrer_domain != {websiteDomain:String} and referrer_domain != ''`;
|
excludeDomain = `and referrer_domain != {websiteDomain:String} and referrer_domain != ''`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'entry' || type === 'exit') {
|
if (type === 'entry') {
|
||||||
const aggregrate = type === 'entry' ? 'min' : 'max';
|
columnAgg = `argMinMerge(${column})`;
|
||||||
|
}
|
||||||
|
|
||||||
entryExitQuery = `
|
if (type === 'exit') {
|
||||||
JOIN (select visit_id,
|
columnAgg = `argMaxMerge(${column})`;
|
||||||
${aggregrate}(created_at) target_created_at
|
|
||||||
from website_event
|
|
||||||
where website_id = {websiteId:UUID}
|
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
|
||||||
and event_type = {eventType:UInt32}
|
|
||||||
group by visit_id) x
|
|
||||||
ON x.visit_id = website_event.visit_id
|
|
||||||
and x.target_created_at = website_event.created_at`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
|
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
select ${column} x, countMerge(views) y
|
select ${column} x, sum(views) y
|
||||||
from ${table} website_event
|
from ${table} website_event
|
||||||
${entryExitQuery}
|
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
and event_type = {eventType:UInt32}
|
and event_type = {eventType:UInt32}
|
||||||
|
@ -46,7 +46,7 @@ async function clickhouseQuery(
|
|||||||
...filters,
|
...filters,
|
||||||
eventType: EVENT_TYPE.pageView,
|
eventType: EVENT_TYPE.pageView,
|
||||||
});
|
});
|
||||||
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
|
const table = unit === 'hour' ? 'website_event_stats_hourly' : 'website_event_stats_daily';
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
@ -56,7 +56,7 @@ async function clickhouseQuery(
|
|||||||
from (
|
from (
|
||||||
select
|
select
|
||||||
${getDateQuery('created_at', unit, timezone)} as t,
|
${getDateQuery('created_at', unit, timezone)} as t,
|
||||||
countMerge(views) as y
|
sum(views) as y
|
||||||
from ${table} website_event
|
from ${table} website_event
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
|
@ -80,7 +80,7 @@ async function clickhouseQuery(
|
|||||||
eventType: EVENT_TYPE.pageView,
|
eventType: EVENT_TYPE.pageView,
|
||||||
});
|
});
|
||||||
const includeCountry = column === 'city' || column === 'subdivision1';
|
const includeCountry = column === 'city' || column === 'subdivision1';
|
||||||
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
|
const table = unit === 'hour' ? 'website_event_stats_hourly' : 'website_event_stats_daily';
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
|
@ -46,7 +46,7 @@ async function clickhouseQuery(
|
|||||||
...filters,
|
...filters,
|
||||||
eventType: EVENT_TYPE.pageView,
|
eventType: EVENT_TYPE.pageView,
|
||||||
});
|
});
|
||||||
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
|
const table = unit === 'hour' ? 'website_event_stats_hourly' : 'website_event_stats_daily';
|
||||||
|
|
||||||
return rawQuery(
|
return rawQuery(
|
||||||
`
|
`
|
||||||
|
Loading…
Reference in New Issue
Block a user