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.

8.4 KB
Raw Blame
import { withCron } from "@/lib/cron/with-cron";
import { conn } from "@/lib/planetscale";
import { redis } from "@/lib/upstash/redis";
import { RedisStreamEntry } from "@/lib/upstash/redis-streams/client";
import {
  LinkClickEvent,
  linkClickEventStream,
} from "@/lib/upstash/redis-streams/link-click-events";
import { log } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { logAndRespond } from "../../utils";

export const dynamic = "force-dynamic";

const BATCH_SIZE = 10_000; // Max stream entries to consume per cron run
const SUB_BATCH_SIZE = 50; // DB updates to run in parallel within each batch
const BACKLOG_ALERT_THRESHOLD = 50_000; // Alert when stream length exceeds this
const BACKLOG_AGE_ALERT_MS = 5 * 60 * 1000; // Alert when oldest pending entry is older than 5 minutes
const LOCK_KEY = "lock:update-click-stats"; // Prevents concurrent GET/POST from double-counting
const LOCK_TTL_SECONDS = 600; // ≥ cron maxDuration (600s) so lock outlives a running invocation

type LinkAggregate = {
  linkId: string;
  clicks: number;
  lastClicked: number;
  entryIds: string[];
};

type WorkspaceAggregate = {
  workspaceId: string;
  clicks: number;
};

const aggregateClickStats = (
  entries: RedisStreamEntry<LinkClickEvent>[],
): {
  linkUpdates: LinkAggregate[];
  workspaceUpdates: WorkspaceAggregate[];
} => {
  const links = new Map<string, LinkAggregate>();
  const workspaces = new Map<string, WorkspaceAggregate>();

  for (const entry of entries) {
    const { linkId, workspaceId, timestamp } = entry.data;

    if (!linkId) {
      continue;
    }

    const parsedTimestamp = Date.parse(timestamp);
    const lastClicked = Number.isFinite(parsedTimestamp)
      ? parsedTimestamp
      : Date.now();

    const existingLink = links.get(linkId);
    if (existingLink) {
      existingLink.clicks += 1;
      existingLink.lastClicked = Math.max(
        existingLink.lastClicked,
        lastClicked,
      );
      existingLink.entryIds.push(entry.id);
    } else {
      links.set(linkId, {
        linkId,
        clicks: 1,
        lastClicked,
        entryIds: [entry.id],
      });
    }

    if (workspaceId) {
      const existingWorkspace = workspaces.get(workspaceId);
      if (existingWorkspace) {
        existingWorkspace.clicks += 1;
      } else {
        workspaces.set(workspaceId, {
          workspaceId,
          clicks: 1,
        });
      }
    }
  }

  return {
    linkUpdates: Array.from(links.values()),
    workspaceUpdates: Array.from(workspaces.values()),
  };
};

const processInSubBatches = async <T>(
  items: T[],
  handler: (item: T) => Promise<{ success: boolean; error?: unknown }>,
) => {
  const errors: unknown[] = [];
  let totalProcessed = 0;

  for (let i = 0; i < items.length; i += SUB_BATCH_SIZE) {
    const batch = items.slice(i, i + SUB_BATCH_SIZE);
    const results = await Promise.allSettled(batch.map(handler));

    for (const result of results) {
      if (result.status === "fulfilled" && result.value.success) {
        totalProcessed++;
      } else if (result.status === "fulfilled" && result.value.error) {
        errors.push(result.value.error);
      } else if (result.status === "rejected") {
        errors.push(result.reason);
      }
    }
  }

  return { totalProcessed, errors };
};

const getStreamEntryAgeMs = (entryId: string | null) => {
  if (!entryId) {
    return null;
  }

  const timestampMs = Number(entryId.split("-")[0]);
  if (!Number.isFinite(timestampMs)) {
    return null;
  }

  return Date.now() - timestampMs;
};

const processClickStatsStreamBatch = () =>
  linkClickEventStream.processBatch<LinkClickEvent>(
    async (entries) => {
      if (!entries || entries.length === 0) {
        return {
          success: true,
          linkUpdates: [],
          workspaceUpdates: [],
          processedEntryIds: [],
          totalProcessed: 0,
          errors: [],
        };
      }

      console.log(`Aggregating ${entries.length} click stats events`);

      const { linkUpdates, workspaceUpdates } = aggregateClickStats(entries);

      if (linkUpdates.length === 0) {
        console.log("No click stats updates to process");
        return {
          success: true,
          linkUpdates: [],
          workspaceUpdates: [],
          processedEntryIds: entries.map((entry) => entry.id),
          totalProcessed: 0,
          errors: [],
        };
      }

      console.log(
        `Processing ${linkUpdates.length} link, ${workspaceUpdates.length} workspace click stats updates...`,
      );

      const processedEntryIds: string[] = [];
      const errors: unknown[] = [];

      const linkResult = await processInSubBatches(
        linkUpdates,
        async (update) => {
          try {
            const lastClickedAt = new Date(update.lastClicked)
              .toISOString()
              .slice(0, 19)
              .replace("T", " ");

            await conn.execute(
              "UPDATE Link SET clicks = clicks + ?, lastClicked = GREATEST(COALESCE(lastClicked, ?), ?) WHERE id = ?",
              [update.clicks, lastClickedAt, lastClickedAt, update.linkId],
            );
            processedEntryIds.push(...update.entryIds);
            return { success: true };
          } catch (error) {
            console.error(`Failed to update link ${update.linkId}:`, error);
            return {
              success: false,
              error: { linkId: update.linkId, error },
            };
          }
        },
      );
      errors.push(...linkResult.errors);

      const workspaceResult = await processInSubBatches(
        workspaceUpdates,
        async (update) => {
          try {
            await conn.execute(
              "UPDATE Project p SET p.usage = p.usage + ?, p.totalClicks = p.totalClicks + ? WHERE id = ?",
              [update.clicks, update.clicks, update.workspaceId],
            );
            return { success: true };
          } catch (error) {
            console.error(
              `Failed to update workspace ${update.workspaceId}:`,
              error,
            );
            return {
              success: false,
              error: { workspaceId: update.workspaceId, error },
            };
          }
        },
      );
      errors.push(...workspaceResult.errors);

      const totalProcessed =
        linkResult.totalProcessed + workspaceResult.totalProcessed;

      console.log(
        `Processed ${linkResult.totalProcessed}/${linkUpdates.length} links, ${workspaceResult.totalProcessed}/${workspaceUpdates.length} workspaces`,
      );

      if (errors.length > 0) {
        console.error(
          `Encountered ${errors.length} errors while processing click stats:`,
          errors.slice(0, 5),
        );
      }

      return {
        linkUpdates,
        workspaceUpdates,
        errors,
        totalProcessed,
        processedEntryIds,
        entriesProcessed: entries.length,
      };
    },
    {
      count: BATCH_SIZE,
      deleteAfterRead: true,
    },
  );

const maybeAlertOnBacklog = async (streamInfo: {
  length: number;
  firstEntryId: string | null;
}) => {
  const ageMs = getStreamEntryAgeMs(streamInfo.firstEntryId);
  const isBackloggedByLength = streamInfo.length > BACKLOG_ALERT_THRESHOLD;
  const isBackloggedByAge =
    ageMs !== null && ageMs > BACKLOG_AGE_ALERT_MS && streamInfo.length > 0;

  if (!isBackloggedByLength && !isBackloggedByAge) {
    return;
  }

  await log({
    message: `Click stats stream backlog alert: length=${streamInfo.length}, oldestAgeMs=${ageMs ?? "unknown"}, firstEntryId=${streamInfo.firstEntryId ?? "none"}`,
    type: "errors",
  });
};

// GET /api/cron/streams/update-click-stats
export const GET = withCron(async () => {
  const acquired = await redis.set(LOCK_KEY, "1", {
    nx: true,
    ex: LOCK_TTL_SECONDS,
  });

  if (!acquired) {
    return logAndRespond(
      "[update-click-stats] Another run is in progress. Skipping...",
    );
  }

  const { linkUpdates, errors, totalProcessed } =
    await processClickStatsStreamBatch();

  const streamInfo = await linkClickEventStream.getStreamInfo();

  await redis.del(LOCK_KEY);

  waitUntil(maybeAlertOnBacklog(streamInfo));

  if (!linkUpdates.length) {
    return logAndRespond({
      success: true,
      processed: 0,
      streamInfo,
      message: "No updates to process",
    });
  }

  return logAndRespond({
    success: true,
    processed: totalProcessed,
    errors: errors?.length || 0,
    streamInfo,
    message: `Successfully processed ${totalProcessed} click stats updates`,
  });
});