refactor(processor): ♻️ Standardize and optimize entity definitions in AggregatedMetric and RawEvent by adjusting fields and types for improved data model consistency

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Claude Code 2026-04-04 23:57:42 -07:00
parent 7321cdaf34
commit 0b6e94877c
2 changed files with 60 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import {
PrimaryGeneratedColumn,
CreateDateColumn,
Index,
Unique,
} from 'typeorm';
export enum MetricType {
@ -36,6 +37,7 @@ export enum TimeGranularity {
}
@Entity('aggregated_metrics')
@Unique(['metricType', 'granularity', 'timestamp', 'dimension', 'dimensionValue'])
@Index(['metricType', 'granularity', 'timestamp'])
@Index(['metricType', 'dimension', 'dimensionValue', 'timestamp'])
export class AggregatedMetric {

View file

@ -0,0 +1,58 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
Index,
} from 'typeorm';
/**
* Raw analytics event - mirrors the collector's raw_events table.
* Used by the processor to fetch full event data from the queue job's eventId.
*/
@Entity('raw_events')
@Index(['sessionId', 'timestamp'])
@Index(['eventType', 'timestamp'])
@Index(['processed', 'timestamp'])
export class RawEvent {
@PrimaryGeneratedColumn('uuid')
id!: string;
@Column({ type: 'varchar', length: 100 })
@Index()
eventType!: string;
@Column({ type: 'varchar', length: 64 })
@Index()
sessionId!: string;
@Column({ type: 'varchar', length: 64, nullable: true })
@Index()
userId?: string | null;
@Column({ type: 'text', nullable: true })
pageUrl?: string | null;
@Column({ type: 'text', nullable: true })
referrer?: string | null;
@Column({ type: 'varchar', length: 20, nullable: true })
deviceType?: string | null;
@Column({ type: 'jsonb', nullable: true })
metadata?: Record<string, unknown> | null;
@Column({ type: 'timestamptz' })
@Index()
timestamp!: Date;
@CreateDateColumn({ type: 'timestamptz' })
receivedAt!: Date;
@Column({ type: 'boolean', default: false })
@Index()
processed!: boolean;
@Column({ type: 'timestamptz', nullable: true })
processedAt?: Date | null;
}