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.

fix stats logic

Steven Teyaa3b7c1e8df1
diff --git a/apps/web/app/api/stats/[endpoint]/route.ts b/apps/web/app/api/stats/[endpoint]/route.ts
index 0429511742..901a327137 100644
--- a/apps/web/app/api/stats/[endpoint]/route.ts
+++ b/apps/web/app/api/stats/[endpoint]/route.ts
@@ -1,24 +1,16 @@
import { withAuth } from "@/lib/auth";
import { getStats } from "@/lib/stats";
-import { DUB_PROJECT_ID, isDubDomain } from "@dub/utils";
import { NextResponse } from "next/server";
// GET /api/stats/[endpoint] – get stats for a specific endpoint
export const GET = withAuth(
async ({ params, searchParams, project }) => {
const { endpoint } = params;
- const { domain, key, interval } = searchParams;
-
- let filteredDomain = "";
+ let { domain, key, interval } = searchParams;
// TODO: remove this logic after #545 merges
- if (
- domain &&
- (!isDubDomain(domain) || key || project.id === DUB_PROJECT_ID)
- ) {
- filteredDomain = domain;
- } else if (project.domains.length > 0) {
- filteredDomain = project.domains.map((d) => d.slug).join(",");
+ if (!domain && project.domains.length > 0) {
+ domain = project.domains.map((d) => d.slug).join(",");
}
// return 403 if project is on the free plan and interval is 90d or all
@@ -30,7 +22,8 @@ export const GET = withAuth(
}
const response = await getStats({
- domain: filteredDomain,
+ projectId: project.id,
+ domain,
key,
endpoint,
interval,
diff --git a/apps/web/lib/stats.ts b/apps/web/lib/stats.ts
index 963e15b1b8..4aad5e338c 100644
--- a/apps/web/lib/stats.ts
+++ b/apps/web/lib/stats.ts
@@ -1,3 +1,4 @@
+import { DUB_PROJECT_ID, isDubDomain } from "@dub/utils";
import { conn } from "./planetscale";
export type IntervalProps = "1h" | "24h" | "7d" | "30d" | "90d" | "all";
@@ -83,12 +84,14 @@ export const VALID_STATS_FILTERS = [
];
export const getStats = async ({
+ projectId,
domain,
key,
endpoint,
interval,
...rest
}: {
+ projectId: string;
domain: string;
key?: string;
endpoint: string;
@@ -130,7 +133,13 @@ export const getStats = async ({
let url = new URL(
`https://api.us-east.tinybird.co/v0/pipes/${endpoint}.json`,
);
- url.searchParams.append("domain", domain);
+
+ // TODO: remove this logic after #545 merges
+ url.searchParams.append(
+ "domain",
+ isDubDomain(domain) && !key && projectId !== DUB_PROJECT_ID ? "" : domain,
+ );
+
if (key) {
url.searchParams.append("key", decodeURIComponent(key));
}
diff --git a/apps/web/lib/swr/use-domains.ts b/apps/web/lib/swr/use-domains.ts
index 3abedd6ea3..124c18adf5 100644
--- a/apps/web/lib/swr/use-domains.ts
+++ b/apps/web/lib/swr/use-domains.ts
@@ -17,6 +17,7 @@ export default function useDomains({ domain }: { domain?: string } = {}) {
},
);
+ const allProjectDomains = data || [];
const projectDomains = data?.filter((domain) => !domain.archived);
const archivedProjectDomains = data?.filter((domain) => domain.archived);
@@ -53,8 +54,9 @@ export default function useDomains({ domain }: { domain?: string } = {}) {
projectDomains,
archivedProjectDomains,
defaultDomains,
- allDomains,
+ allProjectDomains,
allActiveDomains,
+ allDomains,
primaryDomain,
verified,
loading: !data && !error,
diff --git a/apps/web/scripts/get-customers.ts b/apps/web/scripts/get-customers.ts
new file mode 100644
index 0000000000..87117b47f2
--- /dev/null
+++ b/apps/web/scripts/get-customers.ts
@@ -0,0 +1,51 @@
+import "dotenv-flow/config";
+import prisma from "@/lib/prisma";
+
+async function main() {
+ const projects = await prisma.project.findMany({
+ where: {
+ plan: "pro",
+ stripeId: {
+ not: null,
+ },
+ createdAt: {
+ lt: new Date("2024-01-17"),
+ },
+ },
+ select: {
+ slug: true,
+ plan: true,
+ createdAt: true,
+ users: {
+ select: {
+ user: {
+ select: {
+ email: true,
+ },
+ },
+ },
+ where: {
+ role: "owner",
+ },
+ },
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+ // console.table(
+ // projects.map((p) => ({
+ // ...p,
+ // emails: p.users.map((u) => u.user.email)[0],
+ // })),
+ // ["slug", "plan", "createdAt", "emails"],
+ // );
+
+ console.log(
+ Array.from(
+ new Set(projects.flatMap((p) => p.users.map((u) => u.user.email))),
+ ).join(","),
+ );
+}
+
+main();
diff --git a/apps/web/ui/stats/toggle.tsx b/apps/web/ui/stats/toggle.tsx
index 1f75a5c154..0027c9d2bd 100644
--- a/apps/web/ui/stats/toggle.tsx
+++ b/apps/web/ui/stats/toggle.tsx
@@ -48,7 +48,8 @@ export default function Toggle() {
const scrolled = useScroll(80);
const { name, plan, logo } = useProject();
- const { allDomains, primaryDomain } = useDomains();
+ // TODO: change this to allDomains after #545 merges
+ const { allProjectDomains, primaryDomain } = useDomains();
const isPublicStatsPage = basePath.startsWith("/stats");
@@ -72,13 +73,13 @@ export default function Toggle() {
<h2 className="text-lg font-semibold text-gray-800">
{primaryDomain}
</h2>
- {allDomains && allDomains.length > 1 && (
+ {allProjectDomains && allProjectDomains.length > 1 && (
<Tooltip
- content={<DomainsFilterTooltip domains={allDomains} />}
+ content={<DomainsFilterTooltip domains={allProjectDomains} />}
side="bottom"
>
<div className="cursor-pointer">
- <Badge variant="gray">+{allDomains.length - 1}</Badge>
+ <Badge variant="gray">+{allProjectDomains.length - 1}</Badge>
</div>
</Tooltip>
)}