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.

removed duplicate getTopLinks implementation

Steven Tey87cd2888faae
diff --git a/apps/web/app/api/cron/usage/utils.ts b/apps/web/app/api/cron/usage/utils.ts
index c1fdb912ef..b997b278c4 100644
--- a/apps/web/app/api/cron/usage/utils.ts
+++ b/apps/web/app/api/cron/usage/utils.ts
@@ -1,8 +1,8 @@
import { limiter } from "@/lib/cron";
import prisma from "@/lib/prisma";
-import { getTopLinks } from "@/lib/tinybird";
+import { getStats } from "@/lib/stats";
import { ProjectProps } from "@/lib/types";
-import { getAdjustedBillingCycleStart, log } from "@dub/utils";
+import { getAdjustedBillingCycleStart, linkConstructor, log } from "@dub/utils";
import { sendEmail } from "emails";
import ClicksSummary from "emails/clicks-summary";
import UsageExceeded from "emails/usage-exceeded";
@@ -114,7 +114,28 @@ export const updateUsage = async () => {
},
},
}),
- getTopLinks(project.domains.map((domain) => domain.slug)),
+ getStats({
+ domain: project.domains.map((domain) => domain.slug).join(","),
+ endpoint: "top_links",
+ interval: "30d",
+ }).then((data) =>
+ data
+ .slice(0, 5)
+ .map(
+ ({
+ domain,
+ key,
+ clicks,
+ }: {
+ domain: string;
+ key: string;
+ clicks: number;
+ }) => ({
+ link: linkConstructor({ domain, key, pretty: true }),
+ clicks,
+ }),
+ ),
+ ),
]);
const emails = project.users.map((user) => user.user.email) as string[];
diff --git a/apps/web/lib/stats.ts b/apps/web/lib/stats.ts
index e5a9f6fc94..7da8cb0614 100644
--- a/apps/web/lib/stats.ts
+++ b/apps/web/lib/stats.ts
@@ -90,7 +90,7 @@ export const getStats = async ({
...rest
}: {
domain: string;
- key: string;
+ key?: string;
endpoint: string;
interval?: string;
} & {
@@ -110,7 +110,7 @@ export const getStats = async ({
// 1. endpoint is /clicks
// 2. interval is not defined
// 3. there's a connection to MySQL
- if (endpoint === "clicks" && !interval && conn) {
+ if (endpoint === "clicks" && key && !interval && conn) {
const response =
key === "_root"
? await conn.execute("SELECT clicks FROM Domain WHERE slug = ?", [
diff --git a/apps/web/lib/tinybird.ts b/apps/web/lib/tinybird.ts
index 8c8233e66c..84f865eb89 100644
--- a/apps/web/lib/tinybird.ts
+++ b/apps/web/lib/tinybird.ts
@@ -122,23 +122,3 @@ export async function deleteClickData({
console.log({ response });
return response;
}
-
-export async function getTopLinks(domains: string[]) {
- return await fetch(
- `https://api.us-east.tinybird.co/v0/pipes/top_links.json?domains=${domains.join(
- ",",
- )}`,
- {
- headers: {
- Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
- },
- },
- )
- .then((res) => res.json())
- .then(({ data }) =>
- data.map((link: { domain: string; key: string; clicks: number }) => ({
- link: `${link.domain}/${link.key}`,
- clicks: link.clicks,
- })),
- );
-}
diff --git a/apps/web/scripts/get-top-links-for-project.ts b/apps/web/scripts/get-top-links-for-project.ts
new file mode 100644
index 0000000000..f401e75ce5
--- /dev/null
+++ b/apps/web/scripts/get-top-links-for-project.ts
@@ -0,0 +1,83 @@
+import "dotenv-flow/config";
+import prisma from "@/lib/prisma";
+import { getStats } from "@/lib/stats";
+
+function linkConstructor({
+ key,
+ domain = "dub.sh",
+ localhost,
+ pretty,
+ noDomain,
+}: {
+ key: string;
+ domain?: string;
+ localhost?: boolean;
+ pretty?: boolean;
+ noDomain?: boolean;
+}) {
+ const link = `${
+ localhost ? "http://home.localhost:8888" : `https://${domain}`
+ }${key !== "_root" ? `/${key}` : ""}`;
+
+ if (noDomain) return `/${key}`;
+ return pretty ? link.replace(/^https?:\/\//, "") : link;
+}
+
+async function main() {
+ const project = await prisma.project.findUnique({
+ where: {
+ slug: "dub",
+ },
+ select: {
+ id: true,
+ name: true,
+ slug: true,
+ usage: true,
+ usageLimit: true,
+ plan: true,
+ billingCycleStart: true,
+ users: {
+ select: {
+ user: true,
+ },
+ },
+ domains: {
+ where: {
+ verified: true,
+ },
+ },
+ sentEmails: true,
+ createdAt: true,
+ },
+ });
+ if (!project) {
+ console.log("No project found");
+ return;
+ }
+ const topLinks = await getStats({
+ domain: project.domains.map((domain) => domain.slug).join(","),
+ endpoint: "top_links",
+ interval: "30d",
+ }).then((data) =>
+ data
+ .slice(0, 5)
+ .map(
+ ({
+ domain,
+ key,
+ clicks,
+ }: {
+ domain: string;
+ key: string;
+ clicks: number;
+ }) => ({
+ link: linkConstructor({ domain, key, pretty: true }),
+ clicks,
+ }),
+ ),
+ );
+
+ console.table(topLinks);
+}
+
+main();
diff --git a/apps/web/ui/stats/top-links.tsx b/apps/web/ui/stats/top-links.tsx
index d40cf93163..cd9c79f9cb 100644
--- a/apps/web/ui/stats/top-links.tsx
+++ b/apps/web/ui/stats/top-links.tsx
@@ -7,8 +7,7 @@ import { StatsContext } from ".";
import BarList from "./bar-list";
export default function TopLinks() {
- const { basePath, baseApiPath, queryString, totalClicks, interval, modal } =
- useContext(StatsContext);
+ const { baseApiPath, queryString, modal } = useContext(StatsContext);
const { data } = useSWR<{ domain: string; key: string; clicks: number }[]>(
`${baseApiPath}/top_links?${queryString}`,