wip event list
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
ALTER TABLE
|
||||
events
|
||||
ADD
|
||||
COLUMN id UUID;
|
||||
|
||||
CREATE TABLE openpanel.events (
|
||||
`id` UUID,
|
||||
`name` String,
|
||||
`profile_id` String,
|
||||
`project_id` String,
|
||||
|
||||
@@ -12,11 +12,12 @@
|
||||
"with-env": "dotenv -e ../../.env -c --"
|
||||
},
|
||||
"dependencies": {
|
||||
"@clickhouse/client": "^0.2.9",
|
||||
"@mixan/common": "workspace:*",
|
||||
"@mixan/redis": "workspace:*",
|
||||
"@clickhouse/client": "^0.2.9",
|
||||
"@prisma/client": "^5.1.1",
|
||||
"ramda": "^0.29.1"
|
||||
"ramda": "^0.29.1",
|
||||
"uuid": "^9.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@mixan/eslint-config": "workspace:*",
|
||||
@@ -25,6 +26,7 @@
|
||||
"@mixan/types": "workspace:*",
|
||||
"@types/node": "^18.16.0",
|
||||
"@types/ramda": "^0.29.6",
|
||||
"@types/uuid": "^9.0.8",
|
||||
"eslint": "^8.48.0",
|
||||
"prettier": "^3.0.3",
|
||||
"prisma": "^5.1.1",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { IDBProfile } from '@/prisma-types';
|
||||
import { omit } from 'ramda';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
import { randomSplitName, toDots } from '@mixan/common';
|
||||
import { redis, redisPub } from '@mixan/redis';
|
||||
@@ -12,8 +12,11 @@ import {
|
||||
} from '../clickhouse-client';
|
||||
import type { Prisma } from '../prisma-client';
|
||||
import { db } from '../prisma-client';
|
||||
import type { IDBProfile } from '../prisma-types';
|
||||
import { createSqlBuilder } from '../sql-builder';
|
||||
|
||||
export interface IClickhouseEvent {
|
||||
id: string;
|
||||
name: string;
|
||||
profile_id: string;
|
||||
project_id: string;
|
||||
@@ -41,6 +44,7 @@ export function transformEvent(
|
||||
event: IClickhouseEvent
|
||||
): IServiceCreateEventPayload {
|
||||
return {
|
||||
id: event.id,
|
||||
name: event.name,
|
||||
profileId: event.profile_id,
|
||||
projectId: event.project_id,
|
||||
@@ -66,6 +70,7 @@ export function transformEvent(
|
||||
}
|
||||
|
||||
export interface IServiceCreateEventPayload {
|
||||
id: string;
|
||||
name: string;
|
||||
profileId: string;
|
||||
projectId: string;
|
||||
@@ -102,7 +107,10 @@ export async function getLiveVisitors(projectId: string) {
|
||||
return keys.length;
|
||||
}
|
||||
|
||||
export async function getEvents(sql: string, options: GetEventsOptions = {}) {
|
||||
export async function getEvents(
|
||||
sql: string,
|
||||
options: GetEventsOptions = {}
|
||||
): Promise<IServiceCreateEventPayload[]> {
|
||||
const events = await chQuery<IClickhouseEvent>(sql);
|
||||
if (options.profile) {
|
||||
const profileIds = events.map((e) => e.profile_id);
|
||||
@@ -124,7 +132,9 @@ export async function getEvents(sql: string, options: GetEventsOptions = {}) {
|
||||
return events.map(transformEvent);
|
||||
}
|
||||
|
||||
export async function createEvent(payload: IServiceCreateEventPayload) {
|
||||
export async function createEvent(
|
||||
payload: Omit<IServiceCreateEventPayload, 'id'>
|
||||
) {
|
||||
console.log(`create event ${payload.name} for ${payload.profileId}`);
|
||||
|
||||
if (payload.name === 'session_start') {
|
||||
@@ -167,6 +177,7 @@ export async function createEvent(payload: IServiceCreateEventPayload) {
|
||||
}
|
||||
|
||||
const event: IClickhouseEvent = {
|
||||
id: uuid(),
|
||||
name: payload.name,
|
||||
profile_id: payload.profileId,
|
||||
project_id: payload.projectId,
|
||||
@@ -193,6 +204,9 @@ export async function createEvent(payload: IServiceCreateEventPayload) {
|
||||
table: 'events',
|
||||
values: [event],
|
||||
format: 'JSONEachRow',
|
||||
clickhouse_settings: {
|
||||
date_time_input_format: 'best_effort',
|
||||
},
|
||||
});
|
||||
|
||||
redisPub.publish('event', JSON.stringify(transformEvent(event)));
|
||||
@@ -208,3 +222,35 @@ export async function createEvent(payload: IServiceCreateEventPayload) {
|
||||
document: event,
|
||||
};
|
||||
}
|
||||
|
||||
interface GetEventListOptions {
|
||||
projectId: string;
|
||||
profileId?: string;
|
||||
take: number;
|
||||
cursor?: string;
|
||||
}
|
||||
|
||||
export async function getEventList({
|
||||
cursor,
|
||||
take,
|
||||
projectId,
|
||||
profileId,
|
||||
}: GetEventListOptions) {
|
||||
const { sb, getSql } = createSqlBuilder();
|
||||
|
||||
sb.limit = take;
|
||||
sb.where.projectId = `project_id = '${projectId}'`;
|
||||
if (profileId) {
|
||||
sb.where.profileId = `profile_id = '${profileId}'`;
|
||||
}
|
||||
|
||||
if (cursor) {
|
||||
sb.where.cursor = `created_at <= '${formatClickhouseDate(cursor)}'`;
|
||||
}
|
||||
|
||||
sb.orderBy.created_at = 'created_at DESC';
|
||||
|
||||
const res = await getEvents(getSql(), { profile: true });
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user