improve(dashboard): better event details

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-05-13 23:09:37 +02:00
parent dd39ff70a9
commit 2cd358e1bb
15 changed files with 607 additions and 213 deletions

View File

@@ -21,6 +21,7 @@ import { createSqlBuilder } from '../sql-builder';
import { getEventFiltersWhereClause } from './chart.service';
import type { IClickhouseProfile, IServiceProfile } from './profile.service';
import {
getProfileById,
getProfiles,
transformProfile,
upsertProfile,
@@ -830,7 +831,7 @@ class EventService {
id: string;
createdAt?: Date;
}) {
return clix(this.client)
const event = await clix(this.client)
.select<IClickhouseEvent>(['*'])
.from('events')
.where('project_id', '=', projectId)
@@ -852,6 +853,15 @@ class EventService {
return transformEvent(res[0]);
});
if (event?.profileId) {
const profile = await getProfileById(event?.profileId, projectId);
if (profile) {
event.profile = profile;
}
}
return event;
}
async getList({

View File

@@ -1,3 +1,6 @@
import { TABLE_NAMES, ch } from '../clickhouse/client';
import { clix } from '../clickhouse/query-builder';
export type IClickhouseSession = {
id: string;
profile_id: string;
@@ -39,3 +42,19 @@ export type IClickhouseSession = {
version: number;
properties: Record<string, string>;
};
class SessionService {
constructor(private client: typeof ch) {}
byId(sessionId: string, projectId: string) {
return clix(this.client)
.select<IClickhouseSession>(['*'])
.from(TABLE_NAMES.sessions)
.where('id', '=', sessionId)
.where('project_id', '=', projectId)
.execute()
.then((res) => res[0]);
}
}
export const sessionService = new SessionService(ch);

View File

@@ -12,8 +12,8 @@ import {
formatClickhouseDate,
getEventList,
getEvents,
getTopPages,
overviewService,
sessionService,
} from '@openpanel/db';
import {
zChartEventFilter,
@@ -21,7 +21,6 @@ import {
zTimeInterval,
} from '@openpanel/validation';
import { addMinutes, subDays, subMinutes } from 'date-fns';
import { clone } from 'ramda';
import { getProjectAccessCached } from '../access';
import { TRPCAccessError } from '../errors';
@@ -79,6 +78,36 @@ export const eventRouter = createTRPCRouter({
return res;
}),
details: protectedProcedure
.input(
z.object({
id: z.string(),
projectId: z.string(),
createdAt: z.date().optional(),
}),
)
.query(async ({ input: { id, projectId, createdAt } }) => {
const res = await eventService.getById({
projectId,
id,
createdAt,
});
if (!res) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Event not found',
});
}
const session = await sessionService.byId(res?.sessionId, projectId);
return {
event: res,
session,
};
}),
events: protectedProcedure
.input(
z.object({