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.

getWorkspaceUsage pipe

Steven Tey5bf31203c371
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts
index 12a86eecb7..d64594e0c9 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts
@@ -1,111 +1,20 @@
-import { formatUTCDateTimeClickhouse } from "@/lib/analytics/utils/format-utc-datetime-clickhouse";
-import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
import { withWorkspace } from "@/lib/auth";
-import { prisma } from "@/lib/prisma";
-import { tb } from "@/lib/tinybird";
-import { usageQuerySchema, usageResponse } from "@/lib/zod/schemas/usage";
-import { subYears } from "date-fns";
+import { getWorkspaceUsage } from "@/lib/tinybird/get-workspace-usage";
+import { usageQuerySchema } from "@/lib/zod/schemas/usage";
import { NextResponse } from "next/server";
-import * as z from "zod/v4";
export const GET = withWorkspace(
async ({ searchParams, workspace }) => {
- const {
- resource,
- folderId,
- domain,
- groupBy,
- interval,
- start,
- end,
- timezone,
- } = usageQuerySchema.parse(searchParams);
+ const params = usageQuerySchema.parse(searchParams);
- const pipe = tb.buildPipe({
- pipe: "v3_usage",
- // we extend this here since we don't need to include all the additional parameters
- // in the actual request query schema
- parameters: usageQuerySchema.extend({
- workspaceId: z.string(),
- }),
- data: usageResponse,
- });
-
- const { startDate, endDate } = getStartEndDates({
- interval,
- start,
- end,
- dataAvailableFrom: subYears(new Date(), 1),
- timezone,
- });
-
- const response = await pipe({
- resource,
+ const data = await getWorkspaceUsage({
workspaceId: workspace.id,
- start: formatUTCDateTimeClickhouse(startDate),
- end: formatUTCDateTimeClickhouse(endDate),
- timezone,
- ...(folderId && { folderId }),
- ...(domain && { domain }),
- ...(groupBy && { groupBy }),
+ ...params,
});
- let data = response.data;
-
- if (groupBy) {
- const dates = [...new Set(response.data.map((d) => d.date))];
- const groupIds = [...new Set(response.data.map((d) => d[groupBy] ?? ""))];
-
- const where = {
- projectId: workspace.id,
- id: {
- in: groupIds,
- },
- };
-
- const groupMeta = await (groupBy === "folder_id"
- ? prisma.folder.findMany({
- select: {
- id: true,
- name: true,
- },
- where,
- })
- : prisma.domain.findMany({
- select: {
- id: true,
- slug: true,
- },
- where,
- }));
-
- data = dates.map((date) => {
- const groups = groupIds.map((groupId) => ({
- id: groupId,
- name:
- groupMeta.find((g) => g.id === groupId)?.[
- groupBy === "folder_id" ? "name" : "slug"
- ] ?? groupId,
- usage: sum(
- response.data
- .filter((d) => d.date === date && d[groupBy] === groupId)
- .map((d) => d.value),
- ),
- }));
-
- return {
- date,
- value: sum(groups.map((g) => g.usage)),
- groups,
- };
- });
- }
-
return NextResponse.json(data);
},
{
requiredPermissions: ["workspaces.read"],
},
);
-
-const sum = (arr: number[]) => arr.reduce((acc, curr) => acc + curr, 0);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/usage-chart.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/usage-chart.tsx
index 20f07df6fe..cdf4d2f502 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/usage-chart.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/usage-chart.tsx
@@ -250,11 +250,6 @@ export function UsageChart() {
const deferredHoveredGroupsMeta = useDeferredValue(hoveredGroupsMeta);
const deferredHoveredTotalUsage = useDeferredValue(hoveredTotalUsage);
- const allZeroes = useMemo(
- () => chartData?.every(({ values }) => values.usage === 0),
- [chartData],
- );
-
const sortedGroupEntries = useMemo(
() =>
groupsMeta
@@ -366,8 +361,13 @@ export function UsageChart() {
<div
className={cn("h-64 transition-opacity", isValidating && "opacity-50")}
>
- {chartData && chartData.length > 0 ? (
- !allZeroes ? (
+ {chartData ? (
+ chartData.length === 0 ||
+ chartData.every(({ values }) => values.usage === 0) ? (
+ <div className="flex size-full items-center justify-center">
+ <EmptyState {...resourceEmptyStates[activeResource]} />
+ </div>
+ ) : (
<TimeSeriesChart
key={activeResource}
type="bar"
@@ -444,10 +444,6 @@ export function UsageChart() {
<YAxis showGridLines tickFormat={nFormatter} />
<Bars />
</TimeSeriesChart>
- ) : (
- <div className="flex size-full items-center justify-center">
- <EmptyState {...resourceEmptyStates[activeResource]} />
- </div>
)
) : (
<div className="flex size-full items-center justify-center text-sm text-neutral-500">
@@ -518,31 +514,23 @@ export function UsageChart() {
);
})}
</>
- ) : (
- <>
- {loading ? (
- [...Array(3)].map((_, idx) => (
- <div
- key={idx}
- className="flex animate-pulse items-center justify-between gap-4 px-3 py-2 text-xs font-medium"
- >
- <div className="flex items-center gap-2">
- <div className="size-2 rounded-full bg-neutral-200" />
- <div className="h-4 w-32 min-w-0 rounded-md bg-neutral-200" />
- </div>
- <div className="flex items-center gap-2">
- <div className="h-4 w-8 rounded-md bg-neutral-200" />
- <div className="h-4 w-6 rounded-md bg-neutral-200" />
- </div>
- </div>
- ))
- ) : (
- <p className="text-content-subtle flex size-full items-center justify-center py-5 text-sm">
- Failed to load usage data
- </p>
- )}
- </>
- )}
+ ) : loading ? (
+ [...Array(3)].map((_, idx) => (
+ <div
+ key={idx}
+ className="flex animate-pulse items-center justify-between gap-4 px-3 py-2 text-xs font-medium"
+ >
+ <div className="flex items-center gap-2">
+ <div className="size-2 rounded-full bg-neutral-200" />
+ <div className="h-4 w-32 min-w-0 rounded-md bg-neutral-200" />
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="h-4 w-8 rounded-md bg-neutral-200" />
+ <div className="h-4 w-6 rounded-md bg-neutral-200" />
+ </div>
+ </div>
+ ))
+ ) : null}
</NumberFlowGroup>
</div>
diff --git a/apps/web/lib/api/billing/recompute-workspace-usage.ts b/apps/web/lib/api/billing/recompute-workspace-usage.ts
index 8bf9d0d603..477e284543 100644
--- a/apps/web/lib/api/billing/recompute-workspace-usage.ts
+++ b/apps/web/lib/api/billing/recompute-workspace-usage.ts
@@ -1,32 +1,29 @@
-import { getAnalytics } from "@/lib/analytics/get-analytics";
+import { getWorkspaceUsage } from "@/lib/tinybird/get-workspace-usage";
import { prisma } from "@/lib/prisma";
-import { analyticsResponse } from "@/lib/zod/schemas/analytics-response";
import { getBillingStartDate } from "@dub/utils";
import { Project } from "@prisma/client";
+const sum = (arr: number[]) => arr.reduce((acc, curr) => acc + curr, 0);
+
export async function recomputeWorkspaceUsage(
workspace: Pick<Project, "id" | "billingCycleStart">,
) {
const billingStart = getBillingStartDate(workspace.billingCycleStart);
const billingEnd = new Date();
- const [clicksResponse, linksUsage, payoutsUsage] = await Promise.all([
- getAnalytics({
+ const [clicksData, linksData, payoutsUsage] = await Promise.all([
+ getWorkspaceUsage({
workspaceId: workspace.id,
- event: "clicks",
- groupBy: "count",
+ resource: "events",
start: billingStart,
end: billingEnd,
}),
- prisma.link.count({
- where: {
- projectId: workspace.id,
- createdAt: {
- gte: billingStart,
- lte: billingEnd,
- },
- },
+ getWorkspaceUsage({
+ workspaceId: workspace.id,
+ resource: "links",
+ start: billingStart,
+ end: billingEnd,
}),
prisma.invoice.aggregate({
@@ -45,11 +42,9 @@ export async function recomputeWorkspaceUsage(
}),
]);
- const { clicks: usage } = analyticsResponse.count.parse(clicksResponse);
-
return {
- usage,
- linksUsage,
+ usage: sum(clicksData.map((d) => d.value)),
+ linksUsage: sum(linksData.map((d) => d.value)),
payoutsUsage: payoutsUsage._sum.amount ?? 0,
};
}
diff --git a/apps/web/lib/tinybird/get-workspace-usage.ts b/apps/web/lib/tinybird/get-workspace-usage.ts
new file mode 100644
index 0000000000..ac62602d21
--- /dev/null
+++ b/apps/web/lib/tinybird/get-workspace-usage.ts
@@ -0,0 +1,111 @@
+import { formatUTCDateTimeClickhouse } from "@/lib/analytics/utils/format-utc-datetime-clickhouse";
+import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
+import { prisma } from "@/lib/prisma";
+import { usageQuerySchema, usageResponse } from "@/lib/zod/schemas/usage";
+import * as z from "zod/v4";
+import { tb } from "./client";
+
+const usagePipe = tb.buildPipe({
+ pipe: "v3_usage",
+ parameters: usageQuerySchema.extend({
+ workspaceId: z.string(),
+ }),
+ data: usageResponse,
+});
+
+const sum = (arr: number[]) => arr.reduce((acc, curr) => acc + curr, 0);
+
+export type GetWorkspaceUsageParams = {
+ workspaceId: string;
+ resource: z.infer<typeof usageQuerySchema>["resource"];
+ folderId?: string;
+ domain?: string;
+ groupBy?: "folder_id" | "domain";
+ interval?: z.infer<typeof usageQuerySchema>["interval"];
+ start?: string | Date;
+ end?: string | Date;
+ timezone?: string;
+};
+
+export async function getWorkspaceUsage({
+ workspaceId,
+ resource,
+ folderId,
+ domain,
+ groupBy,
+ interval,
+ start,
+ end,
+ timezone = "UTC",
+}: GetWorkspaceUsageParams) {
+ const { startDate, endDate } = getStartEndDates({
+ interval,
+ start,
+ end,
+ timezone,
+ });
+
+ const response = await usagePipe({
+ resource,
+ workspaceId,
+ start: formatUTCDateTimeClickhouse(startDate),
+ end: formatUTCDateTimeClickhouse(endDate),
+ timezone,
+ ...(folderId && { folderId }),
+ ...(domain && { domain }),
+ ...(groupBy && { groupBy }),
+ });
+
+ let data = response.data;
+
+ if (groupBy) {
+ const dates = [...new Set(response.data.map((d) => d.date))];
+ const groupIds = [...new Set(response.data.map((d) => d[groupBy] ?? ""))];
+
+ const where = {
+ projectId: workspaceId,
+ id: {
+ in: groupIds,
+ },
+ };
+
+ const groupMeta = await (groupBy === "folder_id"
+ ? prisma.folder.findMany({
+ select: {
+ id: true,
+ name: true,
+ },
+ where,
+ })
+ : prisma.domain.findMany({
+ select: {
+ id: true,
+ slug: true,
+ },
+ where,
+ }));
+
+ data = dates.map((date) => {
+ const groups = groupIds.map((groupId) => ({
+ id: groupId,
+ name:
+ groupMeta.find((g) => g.id === groupId)?.[
+ groupBy === "folder_id" ? "name" : "slug"
+ ] ?? groupId,
+ usage: sum(
+ response.data
+ .filter((d) => d.date === date && d[groupBy] === groupId)
+ .map((d) => d.value),
+ ),
+ }));
+
+ return {
+ date,
+ value: sum(groups.map((g) => g.usage)),
+ groups,
+ };
+ });
+ }
+
+ return data;
+}
diff --git a/apps/web/scripts/migrations/update-yearly-workspace-usage.ts b/apps/web/scripts/migrations/update-yearly-workspace-usage.ts
index ea6612768c..57e59dddd1 100644
--- a/apps/web/scripts/migrations/update-yearly-workspace-usage.ts
+++ b/apps/web/scripts/migrations/update-yearly-workspace-usage.ts
@@ -1,6 +1,6 @@
-import { getAnalytics } from "@/lib/analytics/get-analytics";
import { prisma } from "@/lib/prisma";
-import { chunk } from "@dub/utils";
+import { getWorkspaceUsage } from "@/lib/tinybird/get-workspace-usage";
+import { chunk, INFINITY_NUMBER } from "@dub/utils";
import { subYears } from "date-fns";
import "dotenv-flow/config";
@@ -9,12 +9,14 @@ async function main() {
while (true) {
const workspaces = await prisma.project.findMany({
where: {
- trialEndsAt: {
- not: null,
- },
planPeriod: "yearly",
- // Unmigrated yearly workspaces still have monthly AI limits
- aiLimit: 1000,
+ aiLimit: 1000, // Unmigrated yearly workspaces still have monthly AI limits
+ trialEndsAt: null, // ignore trial
+ // ignore free plan
+ plan: {
+ not: "free",
+ },
+ // shouldn't happen but just in case
billingCycleEndsAt: {
not: null,
},
@@ -27,30 +29,29 @@ async function main() {
break;
}
- const workspaceChunks = chunk(workspaces, 10);
+ const workspaceChunks = chunk(workspaces, 5);
for (const workspaceChunk of workspaceChunks) {
await Promise.allSettled(
workspaceChunk.map(async (workspace) => {
const billingStart = subYears(workspace.billingCycleEndsAt!, 1);
+ const billingEnd = new Date();
- const [clicksResponse, linksUsage, payoutsUsage] = await Promise.all([
- getAnalytics({
+ const [usage, linksUsage, payoutsUsage] = await Promise.all([
+ getWorkspaceUsage({
workspaceId: workspace.id,
- event: "clicks",
- groupBy: "count",
+ resource: "events",
start: billingStart,
- end: new Date(),
- dataAvailableFrom: workspace.createdAt,
- }),
- prisma.link.count({
- where: {
- projectId: workspace.id,
- createdAt: {
- gte: billingStart,
- },
- },
- }),
+ end: billingEnd,
+ }).then((data) => data.reduce((acc, curr) => acc + curr.value, 0)),
+
+ getWorkspaceUsage({
+ workspaceId: workspace.id,
+ resource: "links",
+ start: billingStart,
+ end: billingEnd,
+ }).then((data) => data.reduce((acc, curr) => acc + curr.value, 0)),
+
prisma.invoice.aggregate({
where: {
workspaceId: workspace.id,
@@ -66,16 +67,18 @@ async function main() {
}),
]);
- const usage =
- typeof clicksResponse === "object" && clicksResponse !== null
- ? (clicksResponse as { clicks?: number }).clicks ?? 0
- : typeof clicksResponse === "number"
- ? clicksResponse
- : 0;
-
- const newUsageLimit = workspace.usageLimit * 12;
- const newLinksLimit = workspace.linksLimit * 12;
- const newPayoutsLimit = workspace.payoutsLimit * 12;
+ const newUsageLimit = Math.min(
+ workspace.usageLimit * 12,
+ INFINITY_NUMBER,
+ );
+ const newLinksLimit = Math.min(
+ workspace.linksLimit * 12,
+ INFINITY_NUMBER,
+ );
+ const newPayoutsLimit = Math.min(
+ workspace.payoutsLimit * 12,
+ INFINITY_NUMBER,
+ );
const newAiLimit = workspace.aiLimit * 12;
const newPayoutsUsage = payoutsUsage._sum.amount ?? 0;