|
| 1 | +//! Helpers for caching historical analytics results inside a site Durable Object. |
| 2 | + |
| 3 | +const ONE_MINUTE_MS = 60 * 1000; |
| 4 | +const ONE_HOUR_MS = 60 * ONE_MINUTE_MS; |
| 5 | +const DEFAULT_MAX_ENTRIES = 128; |
| 6 | +const ANALYTICS_RESULT_CACHE_VERSION = 1; |
| 7 | + |
| 8 | +export type AnalyticsResultCacheKind = "dashboard-aggregates" | "event-summary"; |
| 9 | + |
| 10 | +export type AnalyticsResultCacheEntry<T> = { |
| 11 | + expiresAt: number; |
| 12 | + value: T; |
| 13 | +}; |
| 14 | + |
| 15 | +export interface AnalyticsResultCachePersistence { |
| 16 | + get<T>( |
| 17 | + kind: AnalyticsResultCacheKind, |
| 18 | + key: string, |
| 19 | + ): Promise<AnalyticsResultCacheEntry<T> | null>; |
| 20 | + set<T>( |
| 21 | + kind: AnalyticsResultCacheKind, |
| 22 | + key: string, |
| 23 | + entry: AnalyticsResultCacheEntry<T>, |
| 24 | + ): Promise<void>; |
| 25 | +} |
| 26 | + |
| 27 | +type HistoricalCacheWindow = { |
| 28 | + endDate?: Date; |
| 29 | + timezone?: string | null; |
| 30 | + now?: Date; |
| 31 | +}; |
| 32 | + |
| 33 | +type AnalyticsCacheKeyPartValue = |
| 34 | + | string |
| 35 | + | number |
| 36 | + | boolean |
| 37 | + | null |
| 38 | + | undefined |
| 39 | + | Record<string, unknown>; |
| 40 | + |
| 41 | +const noopAnalyticsResultCachePersistence: AnalyticsResultCachePersistence = { |
| 42 | + async get() { |
| 43 | + return null; |
| 44 | + }, |
| 45 | + async set() {}, |
| 46 | +}; |
| 47 | + |
| 48 | +function normalizeTimeZone(timezone?: string | null): string { |
| 49 | + if (typeof timezone !== "string") return "UTC"; |
| 50 | + const trimmed = timezone.trim(); |
| 51 | + if (!trimmed) return "UTC"; |
| 52 | + |
| 53 | + try { |
| 54 | + Intl.DateTimeFormat(undefined, { timeZone: trimmed }); |
| 55 | + return trimmed; |
| 56 | + } catch { |
| 57 | + return "UTC"; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function getDateBucketInTimeZone(date: Date, timezone?: string | null): string { |
| 62 | + const formatter = new Intl.DateTimeFormat("en-CA", { |
| 63 | + timeZone: normalizeTimeZone(timezone), |
| 64 | + year: "numeric", |
| 65 | + month: "2-digit", |
| 66 | + day: "2-digit", |
| 67 | + }); |
| 68 | + |
| 69 | + const parts = formatter.formatToParts(date); |
| 70 | + const year = parts.find((part) => part.type === "year")?.value; |
| 71 | + const month = parts.find((part) => part.type === "month")?.value; |
| 72 | + const day = parts.find((part) => part.type === "day")?.value; |
| 73 | + |
| 74 | + if (!year || !month || !day) { |
| 75 | + return date.toISOString().slice(0, 10); |
| 76 | + } |
| 77 | + |
| 78 | + return `${year}-${month}-${day}`; |
| 79 | +} |
| 80 | + |
| 81 | +function parseDateBucket(bucket: string): number { |
| 82 | + const [year, month, day] = bucket.split("-").map((value) => Number(value)); |
| 83 | + return Date.UTC(year, month - 1, day); |
| 84 | +} |
| 85 | + |
| 86 | +function normalizeKeyPart(value: AnalyticsCacheKeyPartValue): unknown { |
| 87 | + if (value instanceof Date) { |
| 88 | + return value.toISOString(); |
| 89 | + } |
| 90 | + if (Array.isArray(value)) { |
| 91 | + return value.map((item) => normalizeKeyPart(item as AnalyticsCacheKeyPartValue)); |
| 92 | + } |
| 93 | + if (value && typeof value === "object") { |
| 94 | + const entries = Object.entries(value).toSorted(([left], [right]) => left.localeCompare(right)); |
| 95 | + return Object.fromEntries( |
| 96 | + entries.map(([key, innerValue]) => [key, normalizeKeyPart(innerValue as AnalyticsCacheKeyPartValue)]), |
| 97 | + ); |
| 98 | + } |
| 99 | + return value ?? null; |
| 100 | +} |
| 101 | + |
| 102 | +export function isHistoricalAnalyticsRange({ |
| 103 | + endDate, |
| 104 | + timezone, |
| 105 | + now = new Date(), |
| 106 | +}: HistoricalCacheWindow): boolean { |
| 107 | + if (!endDate) return false; |
| 108 | + const todayBucket = getDateBucketInTimeZone(now, timezone); |
| 109 | + const endBucket = getDateBucketInTimeZone(endDate, timezone); |
| 110 | + return endBucket < todayBucket; |
| 111 | +} |
| 112 | + |
| 113 | +export function getHistoricalAnalyticsCacheTtlMs(input: HistoricalCacheWindow): number { |
| 114 | + if (!isHistoricalAnalyticsRange(input)) return 0; |
| 115 | + |
| 116 | + const todayBucket = getDateBucketInTimeZone(input.now ?? new Date(), input.timezone); |
| 117 | + const endBucket = getDateBucketInTimeZone(input.endDate!, input.timezone); |
| 118 | + const diffDays = Math.max( |
| 119 | + 0, |
| 120 | + Math.round((parseDateBucket(todayBucket) - parseDateBucket(endBucket)) / (24 * ONE_HOUR_MS)), |
| 121 | + ); |
| 122 | + |
| 123 | + if (diffDays <= 1) return 5 * ONE_MINUTE_MS; |
| 124 | + if (diffDays <= 7) return 15 * ONE_MINUTE_MS; |
| 125 | + return ONE_HOUR_MS; |
| 126 | +} |
| 127 | + |
| 128 | +export function buildAnalyticsResultCacheKey( |
| 129 | + kind: AnalyticsResultCacheKind, |
| 130 | + parts: Record<string, AnalyticsCacheKeyPartValue>, |
| 131 | +): string { |
| 132 | + return JSON.stringify({ |
| 133 | + version: ANALYTICS_RESULT_CACHE_VERSION, |
| 134 | + kind, |
| 135 | + parts: normalizeKeyPart(parts), |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +export function createAnalyticsResultCachePersistence(_env: unknown): AnalyticsResultCachePersistence { |
| 140 | + return noopAnalyticsResultCachePersistence; |
| 141 | +} |
| 142 | + |
| 143 | +export class HistoricalAnalyticsResultMemoryCache { |
| 144 | + private readonly entries = new Map<string, AnalyticsResultCacheEntry<unknown>>(); |
| 145 | + |
| 146 | + constructor(private readonly maxEntries: number = DEFAULT_MAX_ENTRIES) {} |
| 147 | + |
| 148 | + get<T>(key: string, now = Date.now()): AnalyticsResultCacheEntry<T> | null { |
| 149 | + const entry = this.entries.get(key); |
| 150 | + if (!entry) return null; |
| 151 | + |
| 152 | + if (entry.expiresAt <= now) { |
| 153 | + this.entries.delete(key); |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + // Reinsert on hit to keep frequently used entries hot. |
| 158 | + this.entries.delete(key); |
| 159 | + this.entries.set(key, entry); |
| 160 | + |
| 161 | + return entry as AnalyticsResultCacheEntry<T>; |
| 162 | + } |
| 163 | + |
| 164 | + set<T>(key: string, entry: AnalyticsResultCacheEntry<T>) { |
| 165 | + if (entry.expiresAt <= Date.now()) return; |
| 166 | + |
| 167 | + this.entries.delete(key); |
| 168 | + this.entries.set(key, entry as AnalyticsResultCacheEntry<unknown>); |
| 169 | + this.trimToMaxEntries(); |
| 170 | + } |
| 171 | + |
| 172 | + clear() { |
| 173 | + this.entries.clear(); |
| 174 | + } |
| 175 | + |
| 176 | + private trimToMaxEntries() { |
| 177 | + while (this.entries.size > this.maxEntries) { |
| 178 | + const oldestKey = this.entries.keys().next().value; |
| 179 | + if (!oldestKey) return; |
| 180 | + this.entries.delete(oldestKey); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments