The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
final changes, increase batch size to 10K
Steven Teyab39482ed90f
diff --git a/apps/web/app/(ee)/api/cron/usage/update-workspace-clicks/route.ts b/apps/web/app/(ee)/api/cron/usage/update-workspace-clicks/route.tsindex 336891315c..cdaa344319 100644--- a/apps/web/app/(ee)/api/cron/usage/update-workspace-clicks/route.ts+++ b/apps/web/app/(ee)/api/cron/usage/update-workspace-clicks/route.ts@@ -3,14 +3,14 @@ import { verifyVercelSignature } from "@/lib/cron/verify-vercel";import { conn } from "@/lib/planetscale";import {ClickEvent,- Entry,+ RedisStreamEntry,workspaceUsageStream,} from "@/lib/upstash/redis-streams";import { NextResponse } from "next/server";export const dynamic = "force-dynamic";-const BATCH_SIZE = 1000;+const BATCH_SIZE = 10000;type WorkspaceAggregateUsage = {workspaceId: string;@@ -22,7 +22,7 @@ type WorkspaceAggregateUsage = {};const aggregateWorkspaceUsage = (- entries: Entry<ClickEvent>[],+ entries: RedisStreamEntry<ClickEvent>[],): { updates: WorkspaceAggregateUsage[]; lastProcessedId: string | null } => {// Aggregate usage by workspaceIdconst aggregatedUsage = new Map<string, WorkspaceAggregateUsage>();@@ -180,7 +180,7 @@ const processWorkspaceUpdateStreamBatch = () =>);// This route is used to process aggregated workspace usage events from Redis streams-// It runs every 5 seconds to consume high-frequency usage updates+// It runs every minute with a batch size of 10,000 to consume high-frequency usage updatesexport async function GET(req: Request) {try {await verifyVercelSignature(req);diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.tsindex bf0315d2f2..bb2f0e7292 100644--- a/apps/web/lib/tinybird/record-click.ts+++ b/apps/web/lib/tinybird/record-click.ts@@ -188,14 +188,14 @@ export async function recordClick({"UPDATE Link SET clicks = clicks + 1, lastClicked = NOW() WHERE id = ?",[linkId],),- // if the link has a destination URL, increment the usage count for the workspace- // and then we have a cron that will reset it at the start of new billing cycle- url &&+ // if the link is associated with a workspace + has a destination URL+ // increment the usage count for the workspace+ workspaceId &&+ url &&publishClickEvent({linkId,workspaceId,timestamp: clickData.timestamp,- url,}).catch(() => {// Fallback on writing directly to the databasereturn conn.execute(diff --git a/apps/web/lib/upstash/redis-streams.ts b/apps/web/lib/upstash/redis-streams.tsindex 142077a325..3f5796d990 100644--- a/apps/web/lib/upstash/redis-streams.ts+++ b/apps/web/lib/upstash/redis-streams.ts@@ -5,12 +5,11 @@ export const WORKSPACE_USAGE_UPDATES_STREAM_KEY = "workspace:usage:updates";export interface ClickEvent {linkId: string;- workspaceId?: string;+ workspaceId: string;timestamp: string;- url?: string;}-export type Entry<T> = {+export type RedisStreamEntry<T> = {id: string;data: T;};@@ -28,7 +27,7 @@ export class RedisStream {*/async processBatch<T>(handler: (- records: Entry<T>[],+ records: RedisStreamEntry<T>[],) => Promise<any & { processedEntryIds: string[] }>,options: {startId?: string;@@ -53,7 +52,7 @@ export class RedisStream {count,);- const entries: Entry<T>[] = Object.entries(entriesMap).map(+ const entries: RedisStreamEntry<T>[] = Object.entries(entriesMap).map(([id, data]) => ({id,data: data as any,@@ -154,7 +153,7 @@ export const workspaceUsageStream = new RedisStream(// Publishes a click event to any relevant streams in a single transactionexport const publishClickEvent = async (event: ClickEvent) => {- const { linkId, timestamp, workspaceId, url } = event;+ const { linkId, timestamp, workspaceId } = event;const entry = { linkId, timestamp, workspaceId };try {@@ -163,9 +162,7 @@ export const publishClickEvent = async (event: ClickEvent) => {// TODO: - Uncomment when we handle click updates// pipeline.xadd(LINK_CLICK_UPDATES_STREAM_KEY, "*", entry);- if (url) {- pipeline.xadd(WORKSPACE_USAGE_UPDATES_STREAM_KEY, "*", entry);- }+ pipeline.xadd(WORKSPACE_USAGE_UPDATES_STREAM_KEY, "*", entry);return await pipeline.exec();} catch (error) {