Skip to content

Commit f52b742

Browse files
committed
schema
1 parent 069e307 commit f52b742

File tree

6 files changed

+5
-3559
lines changed

6 files changed

+5
-3559
lines changed

.cursor/rules/01-MUST-DO.mdc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ use Icon at the end of phosphor react icons, like CaretIcon not Caret AND THATS
4545
use json.stringify() when adding debugging
4646

4747
Almost NEVER use useEffect unless it's critical
48+
49+
do NOT use types any, unknown or never, use proper explicit types, check the @shared/types folder for types, and use them in the codebase, or create ones if needed, don't define types in the same file as the component, create a new file in the @shared/types folder for the type

apps/better-admin

packages/db/src/clickhouse/schema.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { clickHouse } from './client';
22

3-
// Define the analytics database schema with tables for events, sessions, and aggregated data
43
const ANALYTICS_DATABASE = 'analytics';
54
const OBSERVABILITY_DATABASE = 'observability';
65

7-
// SQL statements for creating the analytics database and tables
86
const CREATE_DATABASE = `
97
CREATE DATABASE IF NOT EXISTS ${ANALYTICS_DATABASE}
108
`;
119

12-
// Optimizations:
13-
// 1. Use LowCardinality(String) for fields with limited distinct values
14-
// 2. Reorder ORDER BY to prioritize time-based queries
15-
// 3. Add materialized views for common aggregations
16-
// 4. Remove Nullable where 0 is a sensible default
17-
18-
// Events table stores all raw events
1910
const CREATE_EVENTS_TABLE = `
2011
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.events (
2112
id UUID,
@@ -93,7 +84,6 @@ ORDER BY (client_id, time, id)
9384
SETTINGS index_granularity = 8192
9485
`;
9586

96-
// Dedicated errors table for error events
9787
const CREATE_ERRORS_TABLE = `
9888
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.errors (
9989
id UUID,
@@ -130,7 +120,6 @@ ORDER BY (client_id, timestamp, id)
130120
SETTINGS index_granularity = 8192
131121
`;
132122

133-
// Dedicated web vitals table for performance metrics
134123
const CREATE_WEB_VITALS_TABLE = `
135124
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.web_vitals (
136125
id UUID,
@@ -166,7 +155,6 @@ ORDER BY (client_id, timestamp, id)
166155
SETTINGS index_granularity = 8192
167156
`;
168157

169-
// Stripe Payment Intents table
170158
const CREATE_STRIPE_PAYMENT_INTENTS_TABLE = `
171159
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.stripe_payment_intents (
172160
id String,
@@ -195,7 +183,6 @@ ORDER BY (client_id, webhook_token, created, id)
195183
SETTINGS index_granularity = 8192
196184
`;
197185

198-
// Stripe Charges table
199186
const CREATE_STRIPE_CHARGES_TABLE = `
200187
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.stripe_charges (
201188
id String,
@@ -224,7 +211,6 @@ ORDER BY (client_id, webhook_token, created, id)
224211
SETTINGS index_granularity = 8192
225212
`;
226213

227-
// Stripe Refunds table
228214
const CREATE_STRIPE_REFUNDS_TABLE = `
229215
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.stripe_refunds (
230216
id String,
@@ -285,7 +271,6 @@ TTL toDateTime(timestamp) + INTERVAL 6 MONTH
285271
SETTINGS index_granularity = 8192
286272
`;
287273

288-
// Email events table for tracking email processing and labeling
289274
const CREATE_EMAIL_EVENTS_TABLE = `
290275
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.email_events (
291276
event_id UUID DEFAULT generateUUIDv4(),
@@ -341,11 +326,11 @@ ORDER BY (service, environment, category, level, start_time)
341326
SETTINGS index_granularity = 8192
342327
`;
343328

344-
// OpenTelemetry traces table for distributed tracing
345329
const CREATE_OTEL_TRACES_TABLE = `
346330
CREATE TABLE IF NOT EXISTS ${OBSERVABILITY_DATABASE}.otel_traces (
347331
Timestamp DateTime64(9) CODEC(Delta(8), ZSTD(1)),
348332
TraceId String CODEC(ZSTD(1)),
333+
TenantId String CODEC(ZSTD(1)),
349334
SpanId String CODEC(ZSTD(1)),
350335
ParentSpanId String CODEC(ZSTD(1)),
351336
TraceState String CODEC(ZSTD(1)),
@@ -379,11 +364,11 @@ TTL toDateTime(Timestamp) + toIntervalDay(3)
379364
SETTINGS ttl_only_drop_parts = 1
380365
`;
381366

382-
// OpenTelemetry logs table for structured logging
383367
const CREATE_OTEL_LOGS_TABLE = `
384368
CREATE TABLE IF NOT EXISTS ${OBSERVABILITY_DATABASE}.otel_logs (
385369
Timestamp DateTime64(9) CODEC(Delta(8), ZSTD(1)),
386370
TraceId String CODEC(ZSTD(1)),
371+
TenantId String CODEC(ZSTD(1)),
387372
SpanId String CODEC(ZSTD(1)),
388373
TraceFlags UInt32 CODEC(ZSTD(1)),
389374
SeverityText LowCardinality(String) CODEC(ZSTD(1)),
@@ -412,7 +397,6 @@ TTL toDateTime(Timestamp) + toIntervalDay(3)
412397
SETTINGS ttl_only_drop_parts = 1
413398
`;
414399

415-
// Custom events table with minimal essential fields
416400
const CREATE_CUSTOM_EVENTS_TABLE = `
417401
CREATE TABLE IF NOT EXISTS ${ANALYTICS_DATABASE}.custom_events (
418402
id UUID,
@@ -690,29 +674,24 @@ export interface CustomOutgoingLink {
690674
timestamp: number;
691675
}
692676

693-
// TypeScript interface that matches the ClickHouse schema
694677
export interface AnalyticsEvent {
695-
// Core identification
696678
id: string;
697679
client_id: string;
698680
event_name: string;
699681
anonymous_id: string;
700682
time: number;
701683
session_id: string;
702684

703-
// New fields
704685
event_type?: 'track' | 'error' | 'web_vitals';
705686
event_id?: string;
706687
session_start_time?: number;
707688
timestamp?: number;
708689

709-
// Page context
710690
referrer?: string;
711691
url: string;
712692
path: string;
713693
title?: string;
714694

715-
// Server enrichment
716695
ip: string;
717696
user_agent: string;
718697
browser_name?: string;
@@ -726,31 +705,26 @@ export interface AnalyticsEvent {
726705
region?: string;
727706
city?: string;
728707

729-
// User context
730708
screen_resolution?: string;
731709
viewport_size?: string;
732710
language?: string;
733711
timezone?: string;
734712

735-
// Connection info
736713
connection_type?: string;
737714
rtt?: number;
738715
downlink?: number;
739716

740-
// Engagement metrics
741717
time_on_page?: number;
742718
scroll_depth?: number;
743719
interaction_count?: number;
744720
page_count: number;
745721

746-
// UTM parameters
747722
utm_source?: string;
748723
utm_medium?: string;
749724
utm_campaign?: string;
750725
utm_term?: string;
751726
utm_content?: string;
752727

753-
// Performance metrics
754728
load_time?: number;
755729
dom_ready_time?: number;
756730
dom_interactive?: number;
@@ -760,17 +734,13 @@ export interface AnalyticsEvent {
760734
redirect_time?: number;
761735
domain_lookup_time?: number;
762736

763-
// Link tracking
764737
href?: string;
765738
text?: string;
766739

767-
// Custom event value
768740
value?: string;
769741

770-
// Legacy properties
771742
properties: string;
772743

773-
// Metadata
774744
created_at: number;
775745
}
776746

0 commit comments

Comments
 (0)