Render UTC dates in sessions.

This commit is contained in:
Mike Cao 2024-08-16 00:35:08 -07:00
parent c79720ae1d
commit deb9dd60df
10 changed files with 35 additions and 62 deletions

View file

@ -2,44 +2,32 @@ import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
export async function getSessionActivity(
...args: [websiteId: string, sessionId: string, startDate: Date, endDate: Date]
) {
export async function getSessionActivity(...args: [websiteId: string, sessionId: string]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(
websiteId: string,
sessionId: string,
startDate: Date,
endDate: Date,
) {
async function relationalQuery(websiteId: string, sessionId: string) {
return prisma.client.websiteEvent.findMany({
where: {
id: sessionId,
websiteId,
createdAt: { gte: startDate, lte: endDate },
},
take: 500,
});
}
async function clickhouseQuery(
websiteId: string,
sessionId: string,
startDate: Date,
endDate: Date,
) {
const { rawQuery } = clickhouse;
async function clickhouseQuery(websiteId: string, sessionId: string) {
const { rawQuery, getDateStringSQL } = clickhouse;
return rawQuery(
`
select
session_id as id,
website_id as websiteId,
created_at as createdAt,
${getDateStringSQL('created_at')} as createdAt,
url_path as urlPath,
url_query as urlQuery,
referrer_domain as referrerDomain,
@ -50,9 +38,9 @@ async function clickhouseQuery(
from website_event
where website_id = {websiteId:UUID}
and session_id = {sessionId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
order by created_at desc
limit 500
`,
{ websiteId, sessionId, startDate, endDate },
{ websiteId, sessionId },
);
}

View file

@ -19,7 +19,7 @@ async function relationalQuery(websiteId: string, sessionId: string) {
}
async function clickhouseQuery(websiteId: string, sessionId: string) {
const { rawQuery } = clickhouse;
const { rawQuery, getDateStringSQL } = clickhouse;
return rawQuery(
`
@ -34,8 +34,8 @@ async function clickhouseQuery(websiteId: string, sessionId: string) {
country,
subdivision1,
city,
min(min_time) as firstAt,
max(max_time) as lastAt,
${getDateStringSQL('min(min_time)')} as firstAt,
${getDateStringSQL('max(max_time)')} as lastAt,
uniq(visit_id) visits,
sum(views) as views,
sum(events) as events,

View file

@ -24,7 +24,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
}
async function clickhouseQuery(websiteId: string, filters: QueryFilters, pageParams?: PageParams) {
const { pagedQuery, parseFilters } = clickhouse;
const { pagedQuery, parseFilters, getDateStringSQL } = clickhouse;
const { params, dateQuery, filterQuery } = await parseFilters(websiteId, filters);
return pagedQuery(
@ -42,8 +42,8 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
country,
subdivision1,
city,
min(min_time) as firstAt,
max(max_time) as lastAt,
${getDateStringSQL('min(min_time)')} as firstAt,
${getDateStringSQL('max(max_time)')} as lastAt,
uniq(visit_id) as visits,
sumIf(views, event_type = 1) as views
from website_event_stats_hourly