Updated date output for Clickhouse.

This commit is contained in:
Mike Cao 2024-07-08 23:41:40 -07:00
parent 62f35668ae
commit 4494665d2e
6 changed files with 20 additions and 17 deletions

View File

@ -54,7 +54,7 @@ export function RealtimeLog({ data }: { data: RealtimeData }) {
}, },
]; ];
const getTime = ({ timestamp }) => format(timestamp * 1000, 'h:mm:ss'); const getTime = ({ createdAt }) => format(new Date(createdAt), 'h:mm:ss');
const getColor = ({ id, sessionId }) => stringToColor(sessionId || id); const getColor = ({ id, sessionId }) => stringToColor(sessionId || id);
@ -149,7 +149,7 @@ export function RealtimeLog({ data }: { data: RealtimeData }) {
...e, ...e,
})), })),
...visitors.map(v => ({ __type: TYPE_SESSION, ...v })), ...visitors.map(v => ({ __type: TYPE_SESSION, ...v })),
].sort(thenby.firstBy('timestamp', -1)); ].sort(thenby.firstBy('createdAt', -1));
if (search) { if (search) {
logs = logs.filter(({ eventName, urlPath, browser, os, country, device }) => { logs = logs.filter(({ eventName, urlPath, browser, os, country, device }) => {

View File

@ -9,11 +9,12 @@ import { maxDate } from './date';
import { filtersToArray } from './params'; import { filtersToArray } from './params';
export const CLICKHOUSE_DATE_FORMATS = { export const CLICKHOUSE_DATE_FORMATS = {
minute: '%Y-%m-%d %H:%i:00', second: '%Y-%m-%dT%H:%i:%S',
hour: '%Y-%m-%d %H:00:00', minute: '%Y-%m-%dT%H:%i:00',
day: '%Y-%m-%d', hour: '%Y-%m-%dT%H:00:00',
month: '%Y-%m-01', day: '%Y-%m-%dT00:00:00',
year: '%Y-01-01', month: '%Y-%m-01T00:00:00',
year: '%Y-01-01T00:00:00',
}; };
const log = debug('umami:clickhouse'); const log = debug('umami:clickhouse');
@ -47,7 +48,11 @@ function getClient() {
return client; return client;
} }
function getDateStringSQL(data: any, unit: string | number) { function getDateStringSQL(data: any, unit: string | number, timezone?: string) {
if (timezone) {
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}', '${timezone}')`;
}
return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`; return `formatDateTime(${data}, '${CLICKHOUSE_DATE_FORMATS[unit]}')`;
} }

View File

@ -292,7 +292,7 @@ export function getDateArray(data: any[], startDate: Date, endDate: Date, unit:
for (let i = 0; i <= n; i++) { for (let i = 0; i <= n; i++) {
const t = start(add(startDate, i)); const t = start(add(startDate, i));
const y = data.find(({ x }) => start(getDateFromString(x)).getTime() === t.getTime())?.y || 0; const y = data.find(({ x }) => start(new Date(x)).getTime() === t.getTime())?.y || 0;
arr.push({ x: t, y }); arr.push({ x: t, y });
} }

View File

@ -24,7 +24,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
} }
async function clickhouseQuery(websiteId: string, filters: QueryFilters, pageParams?: PageParams) { 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); const { params, dateQuery, filterQuery } = await parseFilters(websiteId, filters);
return pagedQuery( return pagedQuery(
@ -33,8 +33,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
event_id as id, event_id as id,
website_id as websiteId, website_id as websiteId,
session_id as sessionId, session_id as sessionId,
created_at as createdAt, ${getDateStringSQL('created_at', 'second', filters.timezone)} as createdAt,
toUnixTimestamp(created_at) as timestamp,
url_path as urlPath, url_path as urlPath,
url_query as urlQuery, url_query as urlQuery,
referrer_path as referrerPath, referrer_path as referrerPath,

View File

@ -19,8 +19,8 @@ export async function getRealtimeData(
const { startDate, timezone } = criteria; const { startDate, timezone } = criteria;
const filters = { startDate, endDate: new Date(), unit: 'minute', timezone }; const filters = { startDate, endDate: new Date(), unit: 'minute', timezone };
const [events, sessions, pageviews, sessionviews] = await Promise.all([ const [events, sessions, pageviews, sessionviews] = await Promise.all([
getEvents(websiteId, { startDate }, { pageSize: 10000 }), getEvents(websiteId, { startDate, timezone }, { pageSize: 10000 }),
getSessions(websiteId, { startDate }, { pageSize: 10000 }), getSessions(websiteId, { startDate, timezone }, { pageSize: 10000 }),
getPageviewStats(websiteId, filters), getPageviewStats(websiteId, filters),
getSessionStats(websiteId, filters), getSessionStats(websiteId, filters),
]); ]);

View File

@ -24,7 +24,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
} }
async function clickhouseQuery(websiteId: string, filters: QueryFilters, pageParams?: PageParams) { 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); const { params, dateQuery, filterQuery } = await parseFilters(websiteId, filters);
return pagedQuery( return pagedQuery(
@ -32,8 +32,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
select select
session_id as id, session_id as id,
website_id as websiteId, website_id as websiteId,
created_at as createdAt, ${getDateStringSQL('created_at', 'second', filters.timezone)} as createdAt,
toUnixTimestamp(created_at) as timestamp,
hostname, hostname,
browser, browser,
os, os,