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.

FEAT: timeseries chart for usage stats

Steven Teyfc298c238c51
diff --git a/apps/web/app/api/ai/completion/route.ts b/apps/web/app/api/ai/completion/route.ts
index 761eabbbcd..9d8433bb71 100644
--- a/apps/web/app/api/ai/completion/route.ts
+++ b/apps/web/app/api/ai/completion/route.ts
@@ -26,8 +26,6 @@ export const POST = withWorkspaceEdge(
model,
} = completionSchema.parse(await req.json());
- console.log({ prompt });
-
const result = await streamText({
model: anthropic(model),
messages: [
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts
new file mode 100644
index 0000000000..487baa1212
--- /dev/null
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts
@@ -0,0 +1,40 @@
+import { withWorkspace } from "@/lib/auth";
+import { tb } from "@/lib/tinybird";
+import z from "@/lib/zod";
+import { usageQuerySchema, usageResponse } from "@/lib/zod/schemas/usage";
+import { getFirstAndLastDay } from "@dub/utils";
+import { NextResponse } from "next/server";
+
+export const GET = withWorkspace(async ({ searchParams, workspace }) => {
+ const { resource } = usageQuerySchema.parse(searchParams);
+ const { billingCycleStart } = workspace;
+ const { firstDay } = getFirstAndLastDay(billingCycleStart);
+
+ const pipe = tb.buildPipe({
+ pipe: `v1_usage`,
+ parameters: usageQuerySchema.extend({
+ workspaceId: z
+ .string()
+ .optional()
+ .transform((v) => {
+ if (v && !v.startsWith("ws_")) {
+ return `ws_${v}`;
+ } else {
+ return v;
+ }
+ }),
+ start: z.string(),
+ end: z.string(),
+ }),
+ data: usageResponse,
+ });
+
+ const response = await pipe({
+ resource,
+ workspaceId: workspace.id,
+ start: firstDay.toISOString().replace("T", " ").replace("Z", ""),
+ end: new Date().toISOString().replace("T", " ").replace("Z", ""),
+ });
+
+ return NextResponse.json(response.data);
+});
diff --git a/apps/web/lib/swr/use-usage.ts b/apps/web/lib/swr/use-usage.ts
new file mode 100644
index 0000000000..d947686b0e
--- /dev/null
+++ b/apps/web/lib/swr/use-usage.ts
@@ -0,0 +1,26 @@
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+import { UsageResponse } from "../types";
+import useWorkspace from "./use-workspace";
+
+export default function useUsage({
+ resource,
+}: {
+ resource: "links" | "events";
+}) {
+ const { id } = useWorkspace();
+
+ const { data: usage, isValidating } = useSWR<UsageResponse[]>(
+ id && `/api/workspaces/${id}/billing/usage?resource=${resource}`,
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ },
+ );
+
+ return {
+ usage,
+ loading: usage ? false : true,
+ isValidating,
+ };
+}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 75c5dc638a..46433f5532 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -10,6 +10,7 @@ import { createLinkBodySchema } from "./zod/schemas/links";
import { createOAuthAppSchema, oAuthAppSchema } from "./zod/schemas/oauth";
import { trackSaleResponseSchema } from "./zod/schemas/sales";
import { tokenSchema } from "./zod/schemas/token";
+import { usageResponse } from "./zod/schemas/usage";
import {
createWebhookSchema,
webhookEventSchemaTB,
@@ -259,3 +260,5 @@ export type TrackCustomerResponse = z.infer<typeof trackCustomerResponseSchema>;
export type TrackLeadResponse = z.infer<typeof trackLeadResponseSchema>;
export type TrackSaleResponse = z.infer<typeof trackSaleResponseSchema>;
+
+export type UsageResponse = z.infer<typeof usageResponse>;
diff --git a/apps/web/lib/zod/schemas/usage.ts b/apps/web/lib/zod/schemas/usage.ts
new file mode 100644
index 0000000000..382a93a099
--- /dev/null
+++ b/apps/web/lib/zod/schemas/usage.ts
@@ -0,0 +1,10 @@
+import z from "@/lib/zod";
+
+export const usageQuerySchema = z.object({
+ resource: z.enum(["links", "events"]),
+});
+
+export const usageResponse = z.object({
+ date: z.string(),
+ value: z.number(),
+});