|
| 1 | +import { Redis } from 'ioredis' |
| 2 | +import type { Config } from './config.js' |
| 3 | + |
| 4 | +export interface CheckpointData { |
| 5 | + balance: number |
| 6 | + customer_id: string |
| 7 | + credit_type_id: string |
| 8 | + _synced_at: number |
| 9 | +} |
| 10 | + |
| 11 | +export interface PendingEvent { |
| 12 | + event_id: string |
| 13 | + event_type: string |
| 14 | + estimated_cost: number |
| 15 | + timestamp: number |
| 16 | + properties?: Record<string, unknown> |
| 17 | +} |
| 18 | + |
| 19 | +export interface OptimisticBalanceHash { |
| 20 | + value: string |
| 21 | + computed_at: string |
| 22 | + event_count: string |
| 23 | +} |
| 24 | + |
| 25 | +export type { Redis } |
| 26 | + |
| 27 | +export function createRedisClient(config: Config): Redis { |
| 28 | + const client = new Redis(config.REDIS_URL, { maxRetriesPerRequest: 3 }) |
| 29 | + client.on('error', (err) => { |
| 30 | + console.error(JSON.stringify({ msg: 'redis error', error: err.message })) |
| 31 | + }) |
| 32 | + return client |
| 33 | +} |
| 34 | + |
| 35 | +export function checkpointKey(config: Config, customerId: string): string { |
| 36 | + return `${config.CHECKPOINT_PREFIX}net_balance:${customerId}:${config.CREDIT_TYPE_ID}` |
| 37 | +} |
| 38 | + |
| 39 | +export function pendingSetKey(config: Config, customerId: string): string { |
| 40 | + return `${config.OPTIMISTIC_PREFIX}pending:${customerId}` |
| 41 | +} |
| 42 | + |
| 43 | +export function optimisticBalanceKey(config: Config, customerId: string): string { |
| 44 | + return `${config.OPTIMISTIC_PREFIX}balance:${customerId}` |
| 45 | +} |
| 46 | + |
| 47 | +export async function getCheckpoint( |
| 48 | + redis: Redis, |
| 49 | + config: Config, |
| 50 | + customerId: string |
| 51 | +): Promise<CheckpointData | null> { |
| 52 | + const raw = await redis.get(checkpointKey(config, customerId)) |
| 53 | + if (!raw) return null |
| 54 | + return JSON.parse(raw) as CheckpointData |
| 55 | +} |
| 56 | + |
| 57 | +export async function getPendingEvents( |
| 58 | + redis: Redis, |
| 59 | + config: Config, |
| 60 | + customerId: string |
| 61 | +): Promise<PendingEvent[]> { |
| 62 | + const members = await redis.zrange(pendingSetKey(config, customerId), 0, -1) |
| 63 | + return members.map((m) => JSON.parse(m) as PendingEvent) |
| 64 | +} |
| 65 | + |
| 66 | +export async function sumPendingCosts( |
| 67 | + redis: Redis, |
| 68 | + config: Config, |
| 69 | + customerId: string |
| 70 | +): Promise<{ total: number; count: number }> { |
| 71 | + const events = await getPendingEvents(redis, config, customerId) |
| 72 | + const total = events.reduce((sum, e) => sum + e.estimated_cost, 0) |
| 73 | + return { total, count: events.length } |
| 74 | +} |
| 75 | + |
| 76 | +export async function writeOptimisticBalance( |
| 77 | + redis: Redis, |
| 78 | + config: Config, |
| 79 | + customerId: string, |
| 80 | + value: number, |
| 81 | + eventCount: number |
| 82 | +): Promise<void> { |
| 83 | + const key = optimisticBalanceKey(config, customerId) |
| 84 | + await redis.hset(key, { |
| 85 | + value: String(value), |
| 86 | + computed_at: String(Date.now()), |
| 87 | + event_count: String(eventCount), |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +export async function getOptimisticBalance( |
| 92 | + redis: Redis, |
| 93 | + config: Config, |
| 94 | + customerId: string |
| 95 | +): Promise<OptimisticBalanceHash | null> { |
| 96 | + const data = await redis.hgetall(optimisticBalanceKey(config, customerId)) |
| 97 | + if (!data || !data.value) return null |
| 98 | + return data as unknown as OptimisticBalanceHash |
| 99 | +} |
0 commit comments