Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

rename to publishLinkClickEvent / linkClickEventStream

Steven Tey76ea2347fe74
diff --git a/apps/web/app/(ee)/api/cron/streams/update-click-stats/route.ts b/apps/web/app/(ee)/api/cron/streams/update-click-stats/route.ts
index 32de415dd0..25ddabe3a8 100644
--- a/apps/web/app/(ee)/api/cron/streams/update-click-stats/route.ts
+++ b/apps/web/app/(ee)/api/cron/streams/update-click-stats/route.ts
@@ -2,11 +2,11 @@ import { qstash } from "@/lib/cron";
import { withCron } from "@/lib/cron/with-cron";
import { conn } from "@/lib/planetscale";
import { redis } from "@/lib/upstash/redis";
-import {
- ClickStatsEvent,
- clickStatsStream,
-} from "@/lib/upstash/redis-streams/click-stats";
import { RedisStreamEntry } from "@/lib/upstash/redis-streams/client";
+import {
+ LinkClickEvent,
+ linkClickEventStream,
+} from "@/lib/upstash/redis-streams/link-click-events";
import { APP_DOMAIN_WITH_NGROK, log } from "@dub/utils";
import { format } from "date-fns";
import { NextResponse } from "next/server";
@@ -40,7 +40,7 @@ type EnrollmentAggregate = {
};
const aggregateClickStats = (
- entries: RedisStreamEntry<ClickStatsEvent>[],
+ entries: RedisStreamEntry<LinkClickEvent>[],
): {
linkUpdates: LinkAggregate[];
workspaceUpdates: WorkspaceAggregate[];
@@ -152,7 +152,7 @@ const getStreamEntryAgeMs = (entryId: string | null) => {
};
const processClickStatsStreamBatch = () =>
- clickStatsStream.processBatch<ClickStatsEvent>(
+ linkClickEventStream.processBatch<LinkClickEvent>(
async (entries) => {
if (!entries || entries.length === 0) {
return {
@@ -325,7 +325,7 @@ const executeClickStatsCron = async () => {
entriesProcessed = 0,
} = await processClickStatsStreamBatch();
- const streamInfo = await clickStatsStream.getStreamInfo();
+ const streamInfo = await linkClickEventStream.getStreamInfo();
await maybeAlertOnBacklog(streamInfo);
const hasMore =
diff --git a/apps/web/app/(ee)/api/cron/streams/update-workspace-clicks/route.ts b/apps/web/app/(ee)/api/cron/streams/update-workspace-clicks/route.ts
index 4055d92da7..e3f676c9d7 100644
--- a/apps/web/app/(ee)/api/cron/streams/update-workspace-clicks/route.ts
+++ b/apps/web/app/(ee)/api/cron/streams/update-workspace-clicks/route.ts
@@ -7,6 +7,7 @@ import {
} from "@/lib/upstash/redis-streams/workspace-clicks-usage";
import { NextResponse } from "next/server";
+// TODO: Remove once stream is drained
export const dynamic = "force-dynamic";
const BATCH_SIZE = 10000;
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index 4080a7d3a5..38873c9183 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -13,7 +13,7 @@ import { detectBot } from "../middleware/utils/detect-bot";
import { detectQr } from "../middleware/utils/detect-qr";
import { getIdentityHash } from "../middleware/utils/get-identity-hash";
import { redis } from "../upstash";
-import { publishClickStatsEvent } from "../upstash/redis-streams/click-stats";
+import { publishLinkClickEvent } from "../upstash/redis-streams/link-click-events";
import { publishWorkspaceClickEvent } from "../upstash/redis-streams/workspace-click-events";
/**
@@ -188,7 +188,7 @@ export async function recordClick({
clickId,
}),
- publishClickStatsEvent({
+ publishLinkClickEvent({
linkId,
timestamp: clickData.timestamp,
...(workspaceId && url && { workspaceId }),
diff --git a/apps/web/lib/upstash/redis-streams/click-stats.ts b/apps/web/lib/upstash/redis-streams/link-click-events.ts
similarity index 74%
rename from apps/web/lib/upstash/redis-streams/click-stats.ts
rename to apps/web/lib/upstash/redis-streams/link-click-events.ts
index c0a1b5096e..48dc9e6d3b 100644
--- a/apps/web/lib/upstash/redis-streams/click-stats.ts
+++ b/apps/web/lib/upstash/redis-streams/link-click-events.ts
@@ -3,11 +3,11 @@ import { conn } from "@/lib/planetscale";
import { redis } from "../redis";
import { RedisStream } from "./client";
-const CLICK_STATS_STREAM_KEY = "click:stats:updates";
+const STREAM_KEY = "link:click:events";
-export const clickStatsStream = new RedisStream(CLICK_STATS_STREAM_KEY);
+export const linkClickEventStream = new RedisStream(STREAM_KEY);
-export interface ClickStatsEvent {
+export interface LinkClickEvent {
linkId: string;
timestamp: string;
workspaceId?: string;
@@ -15,13 +15,17 @@ export interface ClickStatsEvent {
partnerId?: string;
}
-export const publishClickStatsEvent = async ({
+// Publishes a link click event to the stream to update:
+// - link clicks count + lastClicked timestamp
+// - workspace usage + totalClicks
+// - program enrollment totalClicks
+export const publishLinkClickEvent = async ({
linkId,
timestamp,
workspaceId,
programId,
partnerId,
-}: ClickStatsEvent) => {
+}: LinkClickEvent) => {
const payload = {
linkId,
timestamp,
@@ -30,11 +34,11 @@ export const publishClickStatsEvent = async ({
};
try {
- return await redis.xadd(CLICK_STATS_STREAM_KEY, "*", payload);
+ return await redis.xadd(STREAM_KEY, "*", payload);
} catch (error) {
logger.error("stream.publish_failed", {
service: "upstash",
- streamKey: CLICK_STATS_STREAM_KEY,
+ streamKey: STREAM_KEY,
error: toErrorFields(error),
correlation: {
linkId,
diff --git a/apps/web/lib/upstash/redis-streams/partner-activity.ts b/apps/web/lib/upstash/redis-streams/partner-activity.ts
index 7af5848be7..ee4cf03517 100644
--- a/apps/web/lib/upstash/redis-streams/partner-activity.ts
+++ b/apps/web/lib/upstash/redis-streams/partner-activity.ts
@@ -15,7 +15,7 @@ export interface PartnerActivityEvent {
eventType: "lead" | "sale" | "commission";
}
-// Publishes a partner activity event to the stream
+// Publishes a partner activity event to the stream to update programEnrollment stats (e.g. totalClicks, totalLeads, totalSales, totalCommissions)
export const publishPartnerActivityEvent = async (
event: PartnerActivityEvent,
) => {
diff --git a/apps/web/lib/upstash/redis-streams/workspace-click-events.ts b/apps/web/lib/upstash/redis-streams/workspace-click-events.ts
index 2868bd9847..c6659ca6a1 100644
--- a/apps/web/lib/upstash/redis-streams/workspace-click-events.ts
+++ b/apps/web/lib/upstash/redis-streams/workspace-click-events.ts
@@ -8,6 +8,7 @@ const STREAM_KEY = "workspace:click:events";
export const workspaceClickEventStream = new RedisStream(STREAM_KEY);
+// Publishes a workspace click event to the stream (only for workspaces with link.clicked webhooks)
export const publishWorkspaceClickEvent = async (event) => {
try {
const parsedEvent = clickEventSchemaTB.parse({
diff --git a/apps/web/lib/upstash/redis-streams/workspace-clicks-usage.ts b/apps/web/lib/upstash/redis-streams/workspace-clicks-usage.ts
new file mode 100644
index 0000000000..005bf41827
--- /dev/null
+++ b/apps/web/lib/upstash/redis-streams/workspace-clicks-usage.ts
@@ -0,0 +1,31 @@
+import { redis } from "../redis";
+import { RedisStream } from "./client";
+
+// TODO: Remove once stream is drained
+const WORKSPACE_CLICKS_USAGE_UPDATES_STREAM_KEY = "workspace:usage:updates";
+
+export const workspaceClicksUsageStream = new RedisStream(
+ WORKSPACE_CLICKS_USAGE_UPDATES_STREAM_KEY,
+);
+
+export interface WorkspaceClicksUsageEvent {
+ linkId: string;
+ workspaceId: string;
+ timestamp: string;
+}
+// Publishes a click event to any relevant streams in a single transaction
+export const publishWorkspaceClicksUsageEvent = async (
+ event: WorkspaceClicksUsageEvent,
+) => {
+ const { linkId, workspaceId, timestamp } = event;
+ try {
+ return await redis.xadd(WORKSPACE_CLICKS_USAGE_UPDATES_STREAM_KEY, "*", {
+ linkId,
+ workspaceId,
+ timestamp,
+ });
+ } catch (error) {
+ console.error("Failed to publish workspace clicks usage event:", error);
+ throw error;
+ }
+};
diff --git a/apps/web/lib/upstash/redis-streams/workspace-links-usage.ts b/apps/web/lib/upstash/redis-streams/workspace-links-usage.ts
index d7c029d5f8..a0dfa56837 100644
--- a/apps/web/lib/upstash/redis-streams/workspace-links-usage.ts
+++ b/apps/web/lib/upstash/redis-streams/workspace-links-usage.ts
@@ -15,6 +15,7 @@ export interface WorkspaceLinksUsageEvent {
timestamp: string;
}
+// Publishes a workspace links usage event to the stream to update workspace linksUsage and totalLinks
export const publishWorkspaceLinksUsageEvent = async (
event: WorkspaceLinksUsageEvent,
) => {