-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredisHelper.ts
More file actions
149 lines (132 loc) · 4 KB
/
redisHelper.ts
File metadata and controls
149 lines (132 loc) · 4 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
import HawkCatcher from '@hawk.so/nodejs';
import { createClient, RedisClientType } from 'redis';
import { Effect, sgr } from './utils/ansi';
/**
* Helper class for working with Redis
*/
export default class RedisHelper {
/**
* TTL for lock records in Redis (in seconds)
*/
private static readonly LOCK_TTL = 10;
/**
* Redis client instance
*/
private readonly redisClient!: RedisClientType;
/**
* Constructor
* Initializes the Redis client and sets up error handling
*/
constructor() {
try {
this.redisClient = createClient({ url: process.env.REDIS_URL });
this.redisClient.on('error', (error) => {
console.error('[Redis] Client error:', error);
if (error) {
HawkCatcher.send(error);
}
});
} catch (error) {
console.error('[Redis] Error creating client:', error);
}
}
/**
* Connect to Redis
*/
public async initialize(): Promise<void> {
try {
await this.redisClient.connect();
console.log('[Redis] Connected successfully');
} catch (error) {
console.error('[Redis] Connection failed:', error);
HawkCatcher.send(error as Error);
}
}
/**
* Close Redis client
*/
public async close(): Promise<void> {
if (this.redisClient.isOpen) {
await this.redisClient.quit();
console.log('[Redis] Connection closed');
}
}
public async getChartDataFromRedis(
startDate: string,
endDate: string,
groupBy: number, // minutes: 1=minute, 60=hour, 1440=day
timezoneOffset = 0,
projectId = '',
groupHash = ''
): Promise<{ timestamp: number; count: number }[]> {
if (!this.redisClient.isOpen) {
throw new Error('Redis client not connected');
}
// Determine suffix based on groupBy
let suffix: string;
if (groupBy === 1) {
suffix = 'minutely';
} else if (groupBy === 60) {
suffix = 'hourly';
} else if (groupBy === 1440) {
suffix = 'daily';
} else {
// For custom intervals, fallback to minutely with aggregation
suffix = 'minutely';
}
const key = groupHash
? `ts:events:${groupHash}:${suffix}`
: projectId
? `ts:events:${projectId}:${suffix}`
: `ts:events:${suffix}`;
// Parse dates (support ISO string or Unix timestamp in seconds)
const start = typeof startDate === 'string' && startDate.includes('-')
? new Date(startDate).getTime()
: Number(startDate) * 1000;
const end = typeof endDate === 'string' && endDate.includes('-')
? new Date(endDate).getTime()
: Number(endDate) * 1000;
const bucketMs = groupBy * 60 * 1000;
let result: [string, string][] = [];
try {
// Use aggregation to sum values within each bucket
// TS.INCRBY creates one point per time period with accumulated count
result = (await this.redisClient.sendCommand([
'TS.RANGE',
key,
start.toString(),
end.toString(),
'AGGREGATION',
'sum',
bucketMs.toString(),
])) as [string, string][] | [];
} catch (err: any) {
if (err.message.includes('TSDB: the key does not exist')) {
console.warn(`[Redis] Key ${key} does not exist, returning zeroed data`);
result = [];
} else {
throw err;
}
}
// Transform data from Redis
const dataPoints: { [ts: number]: number } = {};
for (const [tsStr, valStr] of result) {
const tsMs = Number(tsStr);
dataPoints[tsMs] = Number(valStr) || 0;
}
// Fill missing intervals with zeros
const filled: { timestamp: number; count: number }[] = [];
let current = start;
// Round current to the nearest bucket boundary
current = Math.floor(current / bucketMs) * bucketMs;
while (current <= end) {
const count = dataPoints[current] || 0;
filled.push({
timestamp: Math.floor((current + timezoneOffset * 60 * 1000) / 1000),
count,
});
current += bucketMs;
}
return filled.sort((a, b) => a.timestamp - b.timestamp);
}
}