The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
remove cancelScheduledABTestCompletion
Steven Teyd12036229534
diff --git a/apps/web/app/api/cron/links/[linkId]/complete-tests/route.ts b/apps/web/app/api/cron/links/[linkId]/complete-tests/route.tsindex 1a7cda04f8..742b40310b 100644--- a/apps/web/app/api/cron/links/[linkId]/complete-tests/route.ts+++ b/apps/web/app/api/cron/links/[linkId]/complete-tests/route.ts@@ -30,11 +30,15 @@ export async function POST(return new Response(`Link ${linkId} not found. Skipping...`);}+ // only complete tests if:+ // - there are test variants+ // - the tests completion time is in the past+ // - the tests completion time is within the last 1 minuteif (link.testVariants &&link.testCompletedAt &&link.testCompletedAt < new Date() &&- Date.now() - link.testCompletedAt.getTime() < 2 * 60 * 60 * 1000 // Limit to two hour window+ Date.now() - link.testCompletedAt.getTime() < 60 * 1000 // Limit to 1 minute window) {await completeABTests(link as any);diff --git a/apps/web/lib/api/links/ab-test-scheduler.ts b/apps/web/lib/api/links/ab-test-scheduler.tsindex 1eef3cf32c..77831c6481 100644--- a/apps/web/lib/api/links/ab-test-scheduler.ts+++ b/apps/web/lib/api/links/ab-test-scheduler.ts@@ -6,8 +6,6 @@ import { ExpandedLink } from "./utils";export async function scheduleABTestCompletion(link: Pick<ExpandedLink, "id" | "testVariants" | "testCompletedAt">,) {- await cancelScheduledABTestCompletion(link.id);-if (!link.testVariants || !link.testCompletedAt) {return;}@@ -24,29 +22,3 @@ export async function scheduleABTestCompletion(});}}--// Cancels a scheduled AB test completion for a link (Eg: if the link is updated or deleted)-export async function cancelScheduledABTestCompletion(linkId: string) {- const url = `${APP_DOMAIN_WITH_NGROK}/api/cron/links/${linkId}/complete-tests`;-- const response = await fetch("https://qstash.upstash.io/v2/messages", {- method: "DELETE",- headers: {- Authorization: `Bearer ${process.env.QSTASH_TOKEN}`,- "Content-Type": "application/json",- },- body: JSON.stringify({- url,- }),- });-- if (!response.ok) {- console.error(- `Failed to cancel scheduled AB test completion for link ${linkId}`,- {- status: response.status,- statusText: response.statusText,- },- );- }-}diff --git a/apps/web/lib/api/links/complete-ab-tests.ts b/apps/web/lib/api/links/complete-ab-tests.tsindex 5a56fdb025..8d20fa5b4d 100644--- a/apps/web/lib/api/links/complete-ab-tests.ts+++ b/apps/web/lib/api/links/complete-ab-tests.ts@@ -1,11 +1,9 @@import { getAnalytics } from "@/lib/analytics/get-analytics";-import { NewLinkProps, WorkspaceProps } from "@/lib/types";+import { NewLinkProps } from "@/lib/types";import { sendWorkspaceWebhook } from "@/lib/webhook/publish";import { ABTestVariantsSchema, linkEventSchema } from "@/lib/zod/schemas/links";import { Link, Project } from "@prisma/client";import { waitUntil } from "@vercel/functions";-import { DubApiError, ErrorCodes } from "../errors";-import { processLink } from "./process-link";import { updateLink } from "./update-link";export async function completeABTests(link: Link & { project: Project }) {@@ -36,7 +34,6 @@ export async function completeABTests(link: Link & { project: Project }) {console.log(`AB Test completed but all results are zero for ${link.id}, doing nothing.`,);-return;}@@ -45,10 +42,12 @@ export async function completeABTests(link: Link & { project: Project }) {(analytics.find(({ url }) => url === test.url)?.leads || 0) === max,);+ // this should NEVER happen, but just in caseif (winners.length === 0) {- throw new Error(- `AB Test completed but failed to find winners based on max leads for link ${link.id}.`,+ console.log(+ `AB Test completed but failed to find winners based on max leads for link ${link.id}, doing nothing.`,);+ return;}const winner = winners[Math.floor(Math.random() * winners.length)];@@ -62,6 +61,8 @@ export async function completeABTests(link: Link & { project: Project }) {const updatedLink = {...originalLink,+ url: winner.url,+ // all these are just to make TypeScript happy – we should create a helper fn for these laterexpiresAt:link.expiresAt instanceof Date? link.expiresAt.toISOString()@@ -76,39 +77,15 @@ export async function completeABTests(link: Link & { project: Project }) {link.testStartedAt instanceof Date? link.testStartedAt.toISOString(): link.testStartedAt,- url: winner.url,- ...(link.key === "_root" && {- domain: link.domain,- key: link.key,- }),};- const {- link: processedLink,- error,- code,- } = await processLink({- payload: updatedLink,- workspace: link.project as WorkspaceProps,- skipKeyChecks: true,- skipExternalIdChecks: true,- skipFolderChecks: true,- });-- if (error) {- throw new DubApiError({- code: code as ErrorCodes,- message: error,- });- }-const response = await updateLink({oldLink: {domain: link.domain,key: link.key,image: link.image,},- updatedLink: processedLink,+ updatedLink,});waitUntil(diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.tsindex 3e80ed8f8d..c498a8b580 100644--- a/apps/web/lib/api/links/delete-link.ts+++ b/apps/web/lib/api/links/delete-link.ts@@ -3,7 +3,6 @@ import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";import { prisma } from "@dub/prisma";import { R2_URL } from "@dub/utils";import { waitUntil } from "@vercel/functions";-import { cancelScheduledABTestCompletion } from "./ab-test-scheduler";import { linkCache } from "./cache";import { includeTags } from "./include-tags";import { transformLink } from "./utils";@@ -43,10 +42,6 @@ export async function deleteLink(linkId: string) {totalLinks: { decrement: 1 },},}),-- link.testVariants &&- link.testCompletedAt &&- cancelScheduledABTestCompletion(link.id),]),);diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex a3e3b4298a..19c75810e4 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -28,6 +28,7 @@ export async function updateLink({domain: string;key: string;image?: string | null;+ testCompletedAt?: Date | null;};updatedLink: ProcessedLinkProps &Pick<LinkProps, "id" | "clicks" | "lastClicked" | "updatedAt">;