|
| 1 | +/** |
| 2 | + * In-memory store for per-org analytics report records. |
| 3 | + * |
| 4 | + * Each report entry holds a reference to the S3 key (or a local JSON buffer |
| 5 | + * when S3 is not configured) for the rendered snapshot. Signed download URLs |
| 6 | + * are generated on demand from the stored S3 key. |
| 7 | + * |
| 8 | + * Retention: reports older than ANALYTICS_REPORT_RETENTION_DAYS are pruned |
| 9 | + * automatically whenever a new report is saved for that org. |
| 10 | + */ |
| 11 | + |
| 12 | +const DEFAULT_RETENTION_DAYS = |
| 13 | + Number.parseInt(process.env.ANALYTICS_REPORT_RETENTION_DAYS ?? '30', 10) || 30 |
| 14 | + |
| 15 | +const DEFAULT_REPORT_QUOTA = |
| 16 | + Number.parseInt(process.env.ANALYTICS_REPORT_DAILY_QUOTA ?? '10', 10) || 10 |
| 17 | + |
| 18 | +export interface AnalyticsReport { |
| 19 | + id: string |
| 20 | + orgId: string |
| 21 | + createdAt: string |
| 22 | + /** Present when S3 is configured. */ |
| 23 | + s3Key?: string |
| 24 | + /** Present when S3 is not configured (dev / test). */ |
| 25 | + localBuffer?: Buffer |
| 26 | + snapshotAt: string |
| 27 | + /** Content size in bytes (for informational purposes). */ |
| 28 | + sizeBytes: number |
| 29 | +} |
| 30 | + |
| 31 | +// Keyed by orgId -> AnalyticsReport[] (sorted oldest-first) |
| 32 | +const _store = new Map<string, AnalyticsReport[]>() |
| 33 | + |
| 34 | +export function _resetReportsStore(): void { |
| 35 | + _store.clear() |
| 36 | +} |
| 37 | + |
| 38 | +/** Return a shallow copy of all reports for an org, newest-first. */ |
| 39 | +export function getOrgReports(orgId: string): AnalyticsReport[] { |
| 40 | + return (_store.get(orgId) ?? []).slice().reverse() |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Persist a new report record for an org and purge stale entries. |
| 45 | + * Returns the saved report. |
| 46 | + */ |
| 47 | +export function saveOrgReport( |
| 48 | + report: Omit<AnalyticsReport, 'id' | 'createdAt'>, |
| 49 | + retentionDays = DEFAULT_RETENTION_DAYS, |
| 50 | +): AnalyticsReport { |
| 51 | + const { randomUUID } = require('node:crypto') as typeof import('node:crypto') |
| 52 | + const saved: AnalyticsReport = { |
| 53 | + id: randomUUID(), |
| 54 | + createdAt: new Date().toISOString(), |
| 55 | + ...report, |
| 56 | + } |
| 57 | + |
| 58 | + const list = _store.get(report.orgId) ?? [] |
| 59 | + list.push(saved) |
| 60 | + _store.set(report.orgId, list) |
| 61 | + |
| 62 | + purgeOldReports(report.orgId, retentionDays) |
| 63 | + return saved |
| 64 | +} |
| 65 | + |
| 66 | +/** Remove reports older than `retentionDays` for the given org. */ |
| 67 | +function purgeOldReports(orgId: string, retentionDays: number): void { |
| 68 | + const cutoff = new Date() |
| 69 | + cutoff.setUTCDate(cutoff.getUTCDate() - retentionDays) |
| 70 | + const cutoffIso = cutoff.toISOString() |
| 71 | + |
| 72 | + const list = _store.get(orgId) |
| 73 | + if (!list) return |
| 74 | + |
| 75 | + const kept = list.filter((r) => r.createdAt >= cutoffIso) |
| 76 | + if (kept.length !== list.length) { |
| 77 | + _store.set(orgId, kept) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** All org IDs that currently have at least one stored report. */ |
| 82 | +export function getAllOrgIds(): string[] { |
| 83 | + return Array.from(_store.keys()) |
| 84 | +} |
| 85 | + |
| 86 | +/** Daily in-memory quota counters (orgId -> date -> count). */ |
| 87 | +const _quotaCounters = new Map<string, Map<string, number>>() |
| 88 | + |
| 89 | +export function _resetQuotaCounters(): void { |
| 90 | + _quotaCounters.clear() |
| 91 | +} |
| 92 | + |
| 93 | +const utcDate = (d = new Date()): string => d.toISOString().slice(0, 10) |
| 94 | + |
| 95 | +/** |
| 96 | + * Check and increment the per-org report-generation quota for today. |
| 97 | + * Returns true when allowed, false when the daily cap is exhausted. |
| 98 | + */ |
| 99 | +export function checkAndIncrementReportQuota( |
| 100 | + orgId: string, |
| 101 | + dailyLimit = DEFAULT_REPORT_QUOTA, |
| 102 | +): boolean { |
| 103 | + const today = utcDate() |
| 104 | + if (!_quotaCounters.has(orgId)) { |
| 105 | + _quotaCounters.set(orgId, new Map()) |
| 106 | + } |
| 107 | + const byDate = _quotaCounters.get(orgId)! |
| 108 | + const current = byDate.get(today) ?? 0 |
| 109 | + if (current >= dailyLimit) return false |
| 110 | + byDate.set(today, current + 1) |
| 111 | + return true |
| 112 | +} |
0 commit comments