The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add date range filter to request logs, hide DELETE body (#3742)
Kiran Kd772304ba127
diff --git a/apps/web/app/api/logs/[logId]/route.ts b/apps/web/app/api/logs/[logId]/route.tsindex 0ff378573f..49a4458501 100644--- a/apps/web/app/api/logs/[logId]/route.ts+++ b/apps/web/app/api/logs/[logId]/route.ts@@ -20,9 +20,11 @@ export const GET = withWorkspace(});}- const { start } = getApiLogsDateRange(workspace.plan);+ const { startDate } = getApiLogsDateRange({+ plan: workspace.plan,+ });- if (new Date(log.timestamp) < new Date(start)) {+ if (new Date(log.timestamp) < new Date(startDate)) {throw new DubApiError({code: "not_found",message:diff --git a/apps/web/app/api/logs/count/route.ts b/apps/web/app/api/logs/count/route.tsindex 84b077a487..10587da0b1 100644--- a/apps/web/app/api/logs/count/route.ts+++ b/apps/web/app/api/logs/count/route.ts@@ -7,11 +7,20 @@ import { NextResponse } from "next/server";// GET /api/logs/countexport const GET = withWorkspace(async ({ workspace, searchParams }) => {- const filters = getApiLogsCountQuerySchema.parse(searchParams);+ const { start, end, interval, ...filters } =+ getApiLogsCountQuerySchema.parse(searchParams);++ const { startDate, endDate } = getApiLogsDateRange({+ plan: workspace.plan,+ start,+ end,+ interval,+ });const rows = await getApiLogsCount({...filters,- ...getApiLogsDateRange(workspace.plan),+ start: startDate,+ end: endDate,workspaceId: workspace.id,});diff --git a/apps/web/app/api/logs/route.ts b/apps/web/app/api/logs/route.tsindex 1c85fae202..484eb44ea2 100644--- a/apps/web/app/api/logs/route.ts+++ b/apps/web/app/api/logs/route.ts@@ -8,11 +8,20 @@ import { NextResponse } from "next/server";// GET /api/logsexport const GET = withWorkspace(async ({ workspace, searchParams }) => {- const filters = getApiLogsQuerySchema.parse(searchParams);+ const { start, end, interval, ...filters } =+ getApiLogsQuerySchema.parse(searchParams);++ const { startDate, endDate } = getApiLogsDateRange({+ plan: workspace.plan,+ start,+ end,+ interval,+ });const logs = await getApiLogs({...filters,- ...getApiLogsDateRange(workspace.plan),+ start: startDate,+ end: endDate,workspaceId: workspace.id,});diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/logs/[logId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/logs/[logId]/page-client.tsxindex 640882d7bb..80f15e64da 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/logs/[logId]/page-client.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/logs/[logId]/page-client.tsx@@ -193,21 +193,23 @@ function LogDetailContent({ log }: { log: EnrichedApiLog }) {</div>)}</div>- <div className="flex flex-col gap-2">- <h3 className="text-content-emphasis text-lg font-semibold">- Request body- </h3>- {highlightedRequest ? (- <div- className="shiki-wrapper max-h-[800px] overflow-auto rounded-xl border border-neutral-200 bg-white p-4 text-sm"- dangerouslySetInnerHTML={{ __html: highlightedRequest }}- />- ) : (- <div className="rounded-xl border border-neutral-200 bg-white p-4 font-mono text-xs text-neutral-500">- No request body- </div>- )}- </div>+ {log.method !== "DELETE" && (+ <div className="flex flex-col gap-2">+ <h3 className="text-content-emphasis text-lg font-semibold">+ Request body+ </h3>+ {highlightedRequest ? (+ <div+ className="shiki-wrapper max-h-[800px] overflow-auto rounded-xl border border-neutral-200 bg-white p-4 text-sm"+ dangerouslySetInnerHTML={{ __html: highlightedRequest }}+ />+ ) : (+ <div className="rounded-xl border border-neutral-200 bg-white p-4 font-mono text-xs text-neutral-500">+ No request body+ </div>+ )}+ </div>+ )}</div></div>diff --git a/apps/web/lib/api-logs/api-log-retention.ts b/apps/web/lib/api-logs/api-log-retention.tsindex adc727ddf5..186b447565 100644--- a/apps/web/lib/api-logs/api-log-retention.ts+++ b/apps/web/lib/api-logs/api-log-retention.ts@@ -1,16 +1,51 @@import { formatUTCDateTimeClickhouse } from "@/lib/analytics/utils/format-utc-datetime-clickhouse";import { PlanProps } from "@/lib/types";import { subDays } from "date-fns";+import { getStartEndDates } from "../analytics/utils/get-start-end-dates";import { API_LOG_RETENTION_DAYS, DEFAULT_RETENTION_DAYS } from "./constants";-export function getApiLogsDateRange(plan: PlanProps) {- const days = API_LOG_RETENTION_DAYS[plan] ?? DEFAULT_RETENTION_DAYS;+export function getApiLogsDateRange({+ plan,+ start,+ end,+ interval,+}: {+ plan: PlanProps;+ start?: string | Date | null;+ end?: string | Date | null;+ interval?: string;+}) {+ const retentionDays = API_LOG_RETENTION_DAYS[plan] ?? DEFAULT_RETENTION_DAYS;+ const retentionBoundary = subDays(new Date(), retentionDays);- const end = new Date();- const start = subDays(end, days);+ // If interval or custom start/end provided, resolve them+ if (interval || (start && end)) {+ const { startDate, endDate } = getStartEndDates({+ interval,+ start,+ end,+ });+ // Clamp start to retention boundary+ const clampedStart =+ startDate < retentionBoundary ? retentionBoundary : startDate;+ const clampedEnd =+ endDate < retentionBoundary ? retentionBoundary : endDate;++ const normalizedStart =+ clampedStart <= clampedEnd ? clampedStart : clampedEnd;+ const normalizedEnd =+ clampedStart <= clampedEnd ? clampedEnd : clampedStart;++ return {+ startDate: formatUTCDateTimeClickhouse(normalizedStart),+ endDate: formatUTCDateTimeClickhouse(normalizedEnd),+ };+ }++ // Default: full retention windowreturn {- start: formatUTCDateTimeClickhouse(start),- end: formatUTCDateTimeClickhouse(end),+ startDate: formatUTCDateTimeClickhouse(retentionBoundary),+ endDate: formatUTCDateTimeClickhouse(new Date()),};}diff --git a/apps/web/lib/api-logs/constants.ts b/apps/web/lib/api-logs/constants.tsindex c9acb399f3..c62d3578a0 100644--- a/apps/web/lib/api-logs/constants.ts+++ b/apps/web/lib/api-logs/constants.ts@@ -63,15 +63,12 @@ export const HTTP_STATUS_CODES = [{ value: 500, label: "500 Server Error" },] as const;-export const HTTP_MUTATION_METHODS = [- "POST",- "PATCH",- "PUT",- "DELETE",-] as const;-export const HTTP_METHODS = ["POST", "PATCH", "PUT", "DELETE", "GET"] as const;+export const HTTP_MUTATION_METHODS = HTTP_METHODS.filter(+ (method) => method !== "GET",+);+export const API_LOGS_MAX_PAGE_SIZE = 50;export const API_LOG_RETENTION_DAYS: Record<PlanProps, number> = {@@ -85,6 +82,12 @@ export const API_LOG_RETENTION_DAYS: Record<PlanProps, number> = {enterprise: 90,};+export const API_LOGS_PRESETS_BY_RETENTION: Record<number, string[]> = {+ 30: ["24h", "7d", "30d"],+ 60: ["24h", "7d", "30d", "60d"],+ 90: ["24h", "7d", "30d", "60d", "90d"],+};+// Default when plan is missing from workspace data (should not happen for known plans).export const DEFAULT_RETENTION_DAYS = 30;diff --git a/apps/web/lib/api-logs/get-api-logs-count.ts b/apps/web/lib/api-logs/get-api-logs-count.tsindex 14e82e4645..86a8d0a334 100644--- a/apps/web/lib/api-logs/get-api-logs-count.ts+++ b/apps/web/lib/api-logs/get-api-logs-count.ts@@ -7,7 +7,10 @@ import {getApiLogsCountQuerySchema,} from "./schemas";-type GetApiLogsCountParams = z.infer<typeof getApiLogsCountQuerySchema> & {+type GetApiLogsCountParams = Omit<+ z.infer<typeof getApiLogsCountQuerySchema>,+ "start" | "end"+> & {workspaceId: string;start: string;end: string;diff --git a/apps/web/lib/api-logs/get-api-logs.ts b/apps/web/lib/api-logs/get-api-logs.tsindex 4d2471779c..bc2fe0533b 100644--- a/apps/web/lib/api-logs/get-api-logs.ts+++ b/apps/web/lib/api-logs/get-api-logs.ts@@ -6,7 +6,10 @@ import {getApiLogsQuerySchema,} from "./schemas";-type GetApiLogsParams = z.infer<typeof getApiLogsQuerySchema> & {+type GetApiLogsParams = Omit<+ z.infer<typeof getApiLogsQuerySchema>,+ "start" | "end"+> & {workspaceId: string;start: string;end: string;diff --git a/apps/web/lib/api-logs/schemas.ts b/apps/web/lib/api-logs/schemas.tsindex 9d52b8ccee..2a113d3691 100644--- a/apps/web/lib/api-logs/schemas.ts+++ b/apps/web/lib/api-logs/schemas.ts@@ -2,6 +2,7 @@ import { getPaginationQuerySchema } from "@/lib/zod/schemas/misc";import { tokenSchema } from "@/lib/zod/schemas/token";import { UserSchema } from "@/lib/zod/schemas/users";import * as z from "zod/v4";+import { parseDateSchema } from "../zod/schemas/utils";import { API_LOGS_MAX_PAGE_SIZE } from "./constants";export const requestTypeSchema = z.enum(["api", "webhook"]);@@ -85,6 +86,9 @@ export const getApiLogsQuerySchema = ztokenId: z.string().optional(),requestId: z.string().optional(),requestType: requestTypeSchema.optional(),+ start: parseDateSchema.optional(),+ end: parseDateSchema.optional(),+ interval: z.enum(["24h", "7d", "30d", "60d", "90d"]).optional(),}).extend(getPaginationQuerySchema({diff --git a/apps/web/lib/api/payouts/get-eligible-payouts.ts b/apps/web/lib/api/payouts/get-eligible-payouts.tsindex c49109211f..9f25a4811b 100644--- a/apps/web/lib/api/payouts/get-eligible-payouts.ts+++ b/apps/web/lib/api/payouts/get-eligible-payouts.ts@@ -10,9 +10,8 @@ import { getEffectivePayoutMode } from "./get-effective-payout-mode";import { getPayoutEligibilityFilter } from "./payout-eligibility-filter";import { payoutIdSelectionWhere } from "./payout-id-selection-where";-interface GetEligiblePayoutsProps extends z.output<- typeof eligiblePayoutsQuerySchema-> {+interface GetEligiblePayoutsProps+ extends z.output<typeof eligiblePayoutsQuerySchema> {program: Pick<Program, "id" | "name" | "minPayoutAmount" | "payoutMode">;workspace: Pick<Project, "plan">;}diff --git a/apps/web/lib/swr/use-api-logs-count.ts b/apps/web/lib/swr/use-api-logs-count.tsindex 25d89b4284..c86a90e281 100644--- a/apps/web/lib/swr/use-api-logs-count.ts+++ b/apps/web/lib/swr/use-api-logs-count.ts@@ -27,6 +27,9 @@ export function useApiLogsCount({"tokenId","requestId","requestType",+ "start",+ "end",+ "interval",],},);diff --git a/apps/web/ui/logs/logs-table.tsx b/apps/web/ui/logs/logs-table.tsxindex 533ca2b372..0407855312 100644--- a/apps/web/ui/logs/logs-table.tsx+++ b/apps/web/ui/logs/logs-table.tsx@@ -2,14 +2,19 @@import {API_LOGS_MAX_PAGE_SIZE,+ API_LOGS_PRESETS_BY_RETENTION,+ API_LOG_RETENTION_DAYS,+ DEFAULT_RETENTION_DAYS,METHOD_BADGE_VARIANTS,} from "@/lib/api-logs/constants";import { useApiLogsCount } from "@/lib/swr/use-api-logs-count";import useWorkspace from "@/lib/swr/use-workspace";+import type { PlanProps } from "@/lib/types";import { EnrichedApiLog } from "@/lib/types";import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";import { FilterButtonTableRow } from "@/ui/shared/filter-button-table-row";import { SearchBoxPersisted } from "@/ui/shared/search-box";+import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";import { UserAvatar } from "@/ui/users/user-avatar";import {AnimatedSizeContainer,@@ -26,6 +31,7 @@ import {import { StackY3 } from "@dub/ui/icons";import { fetcher } from "@dub/utils";import { Cell, Row } from "@tanstack/react-table";+import { subDays } from "date-fns";import { useRouter } from "next/navigation";import { useMemo } from "react";import useSWR from "swr";@@ -54,7 +60,7 @@ const LOGS_TABLE_COLUMNS = {export function LogsTable() {const router = useRouter();- const { id: workspaceId, slug } = useWorkspace();+ const { id: workspaceId, slug, plan } = useWorkspace();const { searchParamsObj } = useRouterStuff();const {@@ -302,6 +308,7 @@ export function LogsTable() {onRemove={onRemove}onRemoveAll={onRemoveAll}setSelectedFilter={setSelectedFilter}+ plan={plan || "free"}/>{logs?.length !== 0 ? (<Table {...tableProps} table={table} />@@ -332,6 +339,7 @@ function LogsFilters({onRemove,onRemoveAll,setSelectedFilter,+ plan,}: {filters: any[];activeFilters: any[];@@ -339,18 +347,34 @@ function LogsFilters({onRemove: (key: string) => void;onRemoveAll: () => void;setSelectedFilter: (f: string | null) => void;+ plan: PlanProps;}) {+ const retentionDays = API_LOG_RETENTION_DAYS[plan];++ const presets =+ API_LOGS_PRESETS_BY_RETENTION[retentionDays] ??+ API_LOGS_PRESETS_BY_RETENTION[DEFAULT_RETENTION_DAYS];+return (<div><div className="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">- <Filter.Select- className="w-full md:w-fit"- filters={filters}- activeFilters={activeFilters}- onSelect={onSelect}- onRemove={onRemove}- onSelectedFilterChange={setSelectedFilter}- />+ <div className="flex flex-col items-start gap-2 md:flex-row md:items-center">+ <Filter.Select+ className="w-full md:w-fit"+ filters={filters}+ activeFilters={activeFilters}+ onSelect={onSelect}+ onRemove={onRemove}+ onSelectedFilterChange={setSelectedFilter}+ />+ <SimpleDateRangePicker+ className="w-full md:w-fit"+ align="start"+ defaultInterval="30d"+ presets={presets as any}+ fromDate={subDays(new Date(), retentionDays)}+ />+ </div><SearchBoxPersistedurlParam="requestId"placeholder="Search by request ID"diff --git a/apps/web/ui/logs/use-log-filters.ts b/apps/web/ui/logs/use-log-filters.tsindex d31d6fe554..6ce4c0aaf3 100644--- a/apps/web/ui/logs/use-log-filters.ts+++ b/apps/web/ui/logs/use-log-filters.ts@@ -135,7 +135,16 @@ export function useLogFilters() {const onRemoveAll = useCallback(() =>queryParams({- del: ["method", "statusCode", "routePattern", "tokenId", "requestType"],+ del: [+ "method",+ "statusCode",+ "routePattern",+ "tokenId",+ "requestType",+ "start",+ "end",+ "interval",+ ],}),[queryParams],);@@ -152,8 +161,27 @@ export function useLogFilters() {params.requestId = searchParamsObj.requestId;}+ if (searchParamsObj.start) {+ params.start = searchParamsObj.start;+ }++ if (searchParamsObj.end) {+ params.end = searchParamsObj.end;+ }++ if (searchParamsObj.interval) {+ params.interval = searchParamsObj.interval;+ }+return new URLSearchParams(params).toString();- }, [activeFilters, workspaceId, searchParamsObj.requestId]);+ }, [+ activeFilters,+ workspaceId,+ searchParamsObj.requestId,+ searchParamsObj.start,+ searchParamsObj.end,+ searchParamsObj.interval,+ ]);return {filters,diff --git a/apps/web/ui/partners/confirm-payouts-sheet.tsx b/apps/web/ui/partners/confirm-payouts-sheet.tsxindex a2f062390d..0c6eec2c62 100644--- a/apps/web/ui/partners/confirm-payouts-sheet.tsx+++ b/apps/web/ui/partners/confirm-payouts-sheet.tsx@@ -185,8 +185,8 @@ function ConfirmPayoutsSheetContent() {);const eligiblePayoutsTableRowCount = isExplicitSelectionMode- ? (eligiblePayoutsSummaryCount?.count ?? 0)- : (eligiblePayoutsTableTotalCount?.count ?? 0);+ ? eligiblePayoutsSummaryCount?.count ?? 0+ : eligiblePayoutsTableTotalCount?.count ?? 0;const { data: payoutsCount } = useSWR<{@@ -233,10 +233,12 @@ function ConfirmPayoutsSheetContent() {isLoading: eligiblePayoutsLoading,} = useSWR<PayoutResponse[]>(defaultProgramId- ? `/api/programs/${defaultProgramId}/payouts/eligible?${new URLSearchParams({- ...tableQuery,- page: pagination.pageIndex.toString(),- }).toString()}`+ ? `/api/programs/${defaultProgramId}/payouts/eligible?${new URLSearchParams(+ {+ ...tableQuery,+ page: pagination.pageIndex.toString(),+ },+ ).toString()}`: null,fetcher,{@@ -594,7 +596,7 @@ function ConfirmPayoutsSheetContent() {),},...(payoutsIncludedInInvoice.length > 0 &&- payoutsIncludedInInvoice.some(isExternalPayout)+ payoutsIncludedInInvoice.some(isExternalPayout)? [{key: "External Amount",diff --git a/apps/web/ui/shared/inline-badge-popover.tsx b/apps/web/ui/shared/inline-badge-popover.tsxindex 1b9f78d637..61932f8462 100644--- a/apps/web/ui/shared/inline-badge-popover.tsx+++ b/apps/web/ui/shared/inline-badge-popover.tsx@@ -62,7 +62,7 @@ export function InlineBadgePopover({align="start"content={<InlineBadgePopoverContext.Provider value={{ isOpen, setIsOpen }}>- <ScrollContainer className="w-full min-w-32 max-h-[50dvh] min-h-0 overscroll-contain p-1 text-sm sm:w-auto">+ <ScrollContainer className="max-h-[50dvh] min-h-0 w-full min-w-32 overscroll-contain p-1 text-sm sm:w-auto">{children}</ScrollContainer></InlineBadgePopoverContext.Provider>diff --git a/apps/web/ui/shared/simple-date-range-picker.tsx b/apps/web/ui/shared/simple-date-range-picker.tsxindex c56e934865..c1e64dac8e 100644--- a/apps/web/ui/shared/simple-date-range-picker.tsx+++ b/apps/web/ui/shared/simple-date-range-picker.tsx@@ -18,6 +18,7 @@ export default function SimpleDateRangePicker({values,disabled,presets,+ fromDate,}: {className?: string;align?: "start" | "center" | "end";@@ -25,6 +26,7 @@ export default function SimpleDateRangePicker({values?: Values;disabled?: boolean;presets?: (typeof DATE_RANGE_INTERVAL_PRESETS)[number][];+ fromDate?: Date;}) {const { queryParams, searchParamsObj } = useRouterStuff();const { start, end, interval } = values ?? (searchParamsObj as Values);@@ -88,6 +90,7 @@ export default function SimpleDateRangePicker({};})}disabled={disabled}+ fromDate={fromDate}/>);}