forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics-ingest.service.ts
More file actions
327 lines (293 loc) · 8.86 KB
/
Copy pathanalytics-ingest.service.ts
File metadata and controls
327 lines (293 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Analytics event ingestion and processing
import { Injectable, Logger, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Kafka, KafkaConfig, Consumer, Producer, logLevel } from 'kafkajs';
import { v4 as uuidv4 } from 'uuid';
import {
AnalyticsEvent,
AnalyticsEventType,
ANALYTICS_EVENTS_TABLE_SCHEMA,
} from './analytics-events';
import { cacheMetrics, getCachedMetrics } from './analytics.cache';
@Injectable()
export class AnalyticsIngestService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(AnalyticsIngestService.name);
private kafka: Kafka;
private consumer: Consumer;
private producer: Producer;
private isConnected = false;
// Track available features for graceful degradation
private features = {
kafka: false,
clickhouse: false,
redis: true,
};
constructor(private readonly configService: ConfigService) {
this.kafka = new Kafka({
clientId: 'analytics-ingest',
brokers: this.parseBrokers(),
logLevel: logLevel.ERROR,
retry: {
initialRetryTime: 100,
retries: 3,
},
});
this.consumer = this.kafka.consumer({
groupId: 'analytics-ingest-group',
sessionTimeout: 30000,
heartbeatInterval: 3000,
});
this.producer = this.kafka.producer({
allowAutoTopicCreation: false,
transactionTimeout: 30000,
});
}
async onModuleInit() {
await this.initializeConnections();
}
async onModuleDestroy() {
await this.shutdown();
}
private parseBrokers(): string[] {
const brokers = this.configService.get<string>('KAFKA_BROKERS');
if (!brokers) {
this.logger.warn('KAFKA_BROKERS not configured, analytics will use fallback mode');
return [];
}
return brokers.split(',').map((b) => b.trim());
}
private async initializeConnections(): Promise<void> {
// Try to connect to Kafka
if (this.configService.get<string>('KAFKA_BROKERS')) {
try {
await this.producer.connect();
await this.consumer.connect();
this.features.kafka = true;
this.logger.log('Connected to Kafka for analytics');
} catch (error) {
this.logger.warn('Failed to connect to Kafka, using fallback mode', error);
}
}
// Verify ClickHouse connection
await this.verifyClickHouse();
// Verify Redis connection
await this.verifyRedis();
this.isConnected = true;
}
private async verifyClickHouse(): Promise<void> {
try {
const clickhouse = this.getClickHouseClient();
await clickhouse.query('SELECT 1');
this.features.clickhouse = true;
this.logger.log('ClickHouse connection verified');
} catch (error) {
this.logger.warn('ClickHouse not available, using fallback mode', error);
}
}
private async verifyRedis(): Promise<void> {
try {
const cached = await getCachedMetrics();
this.features.redis = true;
this.logger.log('Redis connection verified');
} catch (error) {
this.logger.warn('Redis not available', error);
this.features.redis = false;
}
}
private getClickHouseClient() {
const ClickHouse = require('@apla/clickhouse');
return new ClickHouse({
host: this.configService.get('CLICKHOUSE_HOST') || 'localhost',
port: this.configService.get('CLICKHOUSE_PORT') || 8123,
user: this.configService.get('CLICKHOUSE_USER') || 'default',
password: this.configService.get('CLICKHOUSE_PASSWORD') || '',
database: this.configService.get('CLICKHOUSE_DB') || 'analytics',
});
}
/**
* Track an analytics event
* This is the main entry point for analytics events from the application
*/
async trackEvent(
type: AnalyticsEventType,
payload: Record<string, unknown>,
options: {
userId?: string;
sessionId?: string;
ipAddress?: string;
userAgent?: string;
source?: string;
platform?: string;
version?: string;
locale?: string;
resourceType?: 'split' | 'item' | 'payment' | 'user';
resourceId?: string;
} = {},
): Promise<string> {
const event: AnalyticsEvent = {
id: uuidv4(),
type,
timestamp: new Date().toISOString(),
actor: {
userId: options.userId,
sessionId: options.sessionId,
ipAddress: options.ipAddress,
userAgent: options.userAgent,
},
context: {
source: options.source || 'api',
platform: options.platform,
version: options.version,
locale: options.locale,
},
payload,
resource: options.resourceId
? {
type: options.resourceType || 'split',
id: options.resourceId,
}
: undefined,
};
// Always emit to internal event bus for same-process consumers
this.emitInternalEvent(event);
// Send to Kafka if available
if (this.features.kafka) {
await this.sendToKafka(event);
}
// Store in ClickHouse if available
if (this.features.clickhouse) {
await this.storeInClickHouse(event);
}
return event.id;
}
/**
* Emit event to in-process subscribers
*/
private emitInternalEvent(event: AnalyticsEvent): void {
// This could use EventEmitter2 or NestJS EventEmitter
// For now, we log at debug level
this.logger.debug(`Analytics event: ${event.type}`, {
id: event.id,
resource: event.resource?.id,
});
}
/**
* Send event to Kafka for async processing
*/
private async sendToKafka(event: AnalyticsEvent): Promise<void> {
try {
await this.producer.send({
topic: 'analytics-events',
messages: [
{
key: event.id,
value: JSON.stringify(event),
timestamp: Date.now().toString(),
headers: {
eventType: event.type,
},
},
],
});
} catch (error) {
this.logger.error('Failed to send event to Kafka', error);
// Fallback to direct storage
await this.storeInClickHouse(event);
}
}
/**
* Store event directly in ClickHouse
*/
private async storeInClickHouse(event: AnalyticsEvent): Promise<void> {
try {
const clickhouse = this.getClickHouseClient();
// Ensure table exists
await clickhouse.query(ANALYTICS_EVENTS_TABLE_SCHEMA);
// Insert event
await clickhouse.insert('analytics_events', {
id: event.id,
type: event.type,
timestamp: new Date(event.timestamp),
actor_user_id: event.actor.userId,
actor_session_id: event.actor.sessionId,
actor_ip_address: event.actor.ipAddress,
actor_user_agent: event.actor.userAgent,
context_source: event.context.source,
context_platform: event.context.platform,
context_version: event.context.version,
context_locale: event.context.locale,
payload: JSON.stringify(event.payload),
resource_type: event.resource?.type,
resource_id: event.resource?.id,
processed_at: new Date(),
});
} catch (error) {
this.logger.error('Failed to store event in ClickHouse', error);
}
}
/**
* Query analytics data from ClickHouse
*/
async queryAnalytics<T = unknown>(sql: string): Promise<T> {
if (!this.features.clickhouse) {
throw new Error('ClickHouse not available');
}
const clickhouse = this.getClickHouseClient();
const result = await clickhouse.query(sql);
return result as T;
}
/**
* Get daily metrics summary
*/
async getDailyMetrics(date: Date): Promise<unknown> {
if (!this.features.clickhouse) {
return this.getFallbackMetrics(date);
}
const dateStr = date.toISOString().split('T')[0];
const sql = `
SELECT
toDate(timestamp) as date,
type,
count() as event_count,
uniq(actor_user_id) as unique_users
FROM analytics_events
WHERE toDate(timestamp) = '${dateStr}'
GROUP BY date, type
ORDER BY event_count DESC
`;
return this.queryAnalytics(sql);
}
/**
* Fallback metrics when ClickHouse is unavailable
*/
private getFallbackMetrics(date: Date): Record<string, unknown> {
this.logger.warn('Returning fallback metrics - ClickHouse unavailable');
return {
date: date.toISOString().split('T')[0],
available: false,
message: 'Analytics storage temporarily unavailable',
};
}
/**
* Get feature availability status
*/
getFeatures(): Record<string, boolean> {
return { ...this.features };
}
/**
* Check if analytics is fully operational
*/
isOperational(): boolean {
return this.isConnected;
}
private async shutdown(): Promise<void> {
try {
if (this.features.kafka) {
await this.consumer.disconnect();
await this.producer.disconnect();
}
} catch (error) {
this.logger.error('Error during shutdown', error);
}
}
}