update dashboard queries to use mv

This commit is contained in:
Francis Cao 2024-07-08 11:18:30 -07:00
parent 5289c277fb
commit bfd5c5f150
5 changed files with 44 additions and 12 deletions

View File

@ -64,7 +64,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { websiteId, type, limit, offset, search } = req.query;
const { websiteId, type, limit, offset, search, unit } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
@ -89,7 +89,7 @@ export default async (
}
if (SESSION_COLUMNS.includes(type)) {
const data = await getSessionMetrics(websiteId, type, filters, limit, offset);
const data = await getSessionMetrics(websiteId, type, filters, limit, offset, unit as string);
if (type === 'language') {
const combined = {};
@ -111,7 +111,14 @@ export default async (
}
if (EVENT_COLUMNS.includes(type)) {
const data = await getPageviewMetrics(websiteId, type, filters, limit, offset);
const data = await getPageviewMetrics(
websiteId,
type,
filters,
limit,
offset,
unit as string,
);
return ok(res, data);
}

View File

@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars, @typescript-eslint/no-unused-vars */
import clickhouse from 'lib/clickhouse';
import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
@ -5,7 +6,14 @@ import prisma from 'lib/prisma';
import { QueryFilters } from 'lib/types';
export async function getPageviewMetrics(
...args: [websiteId: string, type: string, filters: QueryFilters, limit?: number, offset?: number]
...args: [
websiteId: string,
type: string,
filters: QueryFilters,
limit?: number,
offset?: number,
unit?: string,
]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -19,6 +27,7 @@ async function relationalQuery(
filters: QueryFilters,
limit: number = 500,
offset: number = 0,
unit: string,
) {
const column = FILTER_COLUMNS[type] || type;
const { rawQuery, parseFilters } = prisma;
@ -79,6 +88,7 @@ async function clickhouseQuery(
filters: QueryFilters,
limit: number = 500,
offset: number = 0,
unit: string,
): Promise<{ x: string; y: number }[]> {
const column = FILTER_COLUMNS[type] || type;
const { rawQuery, parseFilters } = clickhouse;
@ -108,10 +118,12 @@ async function clickhouseQuery(
and x.target_created_at = website_event.created_at`;
}
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
return rawQuery(
`
select ${column} x, count(*) y
from website_event
select ${column} x, countMerge(views) y
from ${table} website_event
${entryExitQuery}
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}

View File

@ -46,6 +46,7 @@ async function clickhouseQuery(
...filters,
eventType: EVENT_TYPE.pageView,
});
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
return rawQuery(
`
@ -55,8 +56,8 @@ async function clickhouseQuery(
from (
select
${getDateQuery('created_at', unit, timezone)} as t,
count(*) as y
from website_event
countMerge(views) as y
from ${table} website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = {eventType:UInt32}

View File

@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars, @typescript-eslint/no-unused-vars */
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
@ -5,7 +6,14 @@ import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants';
import { QueryFilters } from 'lib/types';
export async function getSessionMetrics(
...args: [websiteId: string, type: string, filters: QueryFilters, limit?: number, offset?: number]
...args: [
websiteId: string,
type: string,
filters: QueryFilters,
limit?: number,
offset?: number,
unit?: string,
]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -19,6 +27,7 @@ async function relationalQuery(
filters: QueryFilters,
limit: number = 500,
offset: number = 0,
unit: string,
) {
const column = FILTER_COLUMNS[type] || type;
const { parseFilters, rawQuery } = prisma;
@ -62,6 +71,7 @@ async function clickhouseQuery(
filters: QueryFilters,
limit: number = 500,
offset: number = 0,
unit: string,
): Promise<{ x: string; y: number }[]> {
const column = FILTER_COLUMNS[type] || type;
const { parseFilters, rawQuery } = clickhouse;
@ -70,6 +80,7 @@ async function clickhouseQuery(
eventType: EVENT_TYPE.pageView,
});
const includeCountry = column === 'city' || column === 'subdivision1';
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
return rawQuery(
`
@ -77,7 +88,7 @@ async function clickhouseQuery(
${column} x,
uniq(session_id) y
${includeCountry ? ', country' : ''}
from website_event
from ${table} website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = {eventType:UInt32}

View File

@ -46,6 +46,7 @@ async function clickhouseQuery(
...filters,
eventType: EVENT_TYPE.pageView,
});
const table = unit === 'hour' ? 'website_event_metric_hourly' : 'website_event_metric_daily';
return rawQuery(
`
@ -55,8 +56,8 @@ async function clickhouseQuery(
from (
select
${getDateQuery('created_at', unit, timezone)} as t,
count(distinct session_id) as y
from website_event
uniq(session_id) as y
from ${table} website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = {eventType:UInt32}