Feat/analytics api hardening#1
Conversation
- Convert ingestBatch to pure append-only mutation - Move visitor, session, and pageview updates to async worker - Implement background shard compaction for read optimization - Remove ensureSite overhead from ingestion path - Add SHA-256 hash cache for writeKey verification
Greptile SummaryThis PR hardens the analytics component across three main dimensions: query correctness, write efficiency, and API surface clarity. Query correctness: The previous Write efficiency: API surface: Key issues found:
Confidence Score: 3/5Safe to merge after fixing runUntilComplete: true default in registerDefaultAnalyticsCrons — that default contradicts the README and could cause unexpected billing in production. The core improvements (unbounded shard reads, in-batch delta accumulation, pagination, split API surfaces) are well-tested and address real production problems. However, the registerDefaultAnalyticsCrons P1 issue ships a cron helper that contradicts the project's own documented guidance, and the Dashboard's stale/unrounded timestamp is a real UX + performance regression. These issues are targeted and fixable without rearchitecting. src/client/maintenance.ts (runUntilComplete default), src/react/Dashboard.tsx (stale to timestamp), src/component/compaction.ts (unbounded outer loop) Important Files Changed
Sequence DiagramsequenceDiagram
participant Browser
participant HTTP as HTTP Ingest Route
participant Ingest as ingestBatch (mutation)
participant Worker as aggregateEventBatch (internalMutation)
participant DB as Convex DB
participant Compact as compactShards (internalAction)
participant Dashboard as Dashboard Queries
Browser->>HTTP: POST /ingest with writeKey + events
HTTP->>DB: ensureSite (mutation)
HTTP->>Ingest: ingestBatch
Ingest->>DB: insert events (append-only)
Ingest->>Worker: scheduler.runAfter(0)
Worker->>DB: ctx.db.get(eventId) x N
Worker->>Worker: accumulateRollupShards (in-memory)
Worker->>DB: upsertVisitor / upsertSession
Worker->>DB: flushRollupShards (unique-key writes)
Worker->>DB: patch event to done
Note over Worker: on error: mark failed, schedule retry (5s delay)
Worker->>Compact: scheduler.runAfter(0) after last batch
loop while targets exist
Compact->>DB: listCompactionTargets (internalQuery)
Compact->>DB: compactShardPairPage (internalMutation)
end
Dashboard->>DB: buildExactRangePlan - raw events + hourly + daily rollups
Dashboard->>DB: querySessionStats (for bounce rate / duration)
Dashboard-->>Browser: overview / timeseries / top-dimension results
|
No description provided.