only use hourly table, remove daily table logic, fix updatechart undefined

This commit is contained in:
Francis Cao 2024-07-23 22:35:11 -07:00
parent 038ecdb592
commit 174b9e4376
10 changed files with 38 additions and 166 deletions

View file

@ -51,8 +51,6 @@ async function clickhouseQuery(
eventType: EVENT_TYPE.customEvent,
});
const table = unit === 'hour' ? 'website_event_stats_hourly' : 'website_event_stats_daily';
return rawQuery(
`
select
@ -62,7 +60,7 @@ async function clickhouseQuery(
from (
select arrayJoin(event_name) as event_name,
created_at
from ${table} website_event
from website_event_stats_hourly website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = {eventType:UInt32}

View file

@ -6,7 +6,7 @@ import prisma from 'lib/prisma';
import { QueryFilters } from 'lib/types';
export async function getWebsiteStats(
...args: [websiteId: string, unit: string, filters: QueryFilters]
...args: [websiteId: string, filters: QueryFilters]
): Promise<
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
> {
@ -18,7 +18,6 @@ export async function getWebsiteStats(
async function relationalQuery(
websiteId: string,
unit: string,
filters: QueryFilters,
): Promise<
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
@ -59,7 +58,6 @@ async function relationalQuery(
async function clickhouseQuery(
websiteId: string,
unit: string,
filters: QueryFilters,
): Promise<
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
@ -69,7 +67,6 @@ async function clickhouseQuery(
...filters,
eventType: EVENT_TYPE.pageView,
});
const table = unit === 'hour' ? 'website_event_stats_hourly' : 'website_event_stats_daily';
return rawQuery(
`
@ -79,7 +76,7 @@ async function clickhouseQuery(
uniq(visit_id) as "visits",
sumIf(1, views = 1) as "bounces",
sum(max_time-min_time) as "totaltime"
from ${table} "website_event"
from website_event_stats_hourly "website_event"
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = {eventType:UInt32}

View file

@ -1,4 +1,3 @@
/* 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';
@ -6,14 +5,7 @@ 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,
unit?: string,
]
...args: [websiteId: string, type: string, filters: QueryFilters, limit?: number, offset?: number]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
@ -27,7 +19,6 @@ async function relationalQuery(
filters: QueryFilters,
limit: number = 500,
offset: number = 0,
unit: string,
) {
const column = FILTER_COLUMNS[type] || type;
const { rawQuery, parseFilters } = prisma;
@ -91,7 +82,6 @@ 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;
@ -121,15 +111,13 @@ async function clickhouseQuery(
groupByQuery = 'group by x';
}
const table = unit === 'hour' ? 'website_event_stats_hourly' : 'website_event_stats_daily';
return rawQuery(
`
select g.t as x,
count(*) as y
from (
select ${columnQuery} as t
from ${table} website_event
from website_event_stats_hourly website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = {eventType:UInt32}

View file

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

View file

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

View file

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