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.

Add `v4_usage` with granularity support, improve `v4_timeseries`

Steven Tey7f8f07723b68
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 cdf4d2f502..0d31082fb8 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
@@ -1,3 +1,4 @@
+import { formatDateTooltip } from "@/lib/analytics/format-date-tooltip";
import useDomains from "@/lib/swr/use-domains";
import useFolders from "@/lib/swr/use-folders";
import { useUsageTimeseries } from "@/lib/swr/use-usage-timeseries";
@@ -16,7 +17,7 @@ import {
} from "@dub/ui";
import { Bars, TimeSeriesChart, XAxis, YAxis } from "@dub/ui/charts";
import { CursorRays, Folder, Globe2, Hyperlink } from "@dub/ui/icons";
-import { cn, formatDate, GOOGLE_FAVICON_URL, nFormatter } from "@dub/utils";
+import { cn, GOOGLE_FAVICON_URL, nFormatter } from "@dub/utils";
import NumberFlow, { NumberFlowGroup } from "@number-flow/react";
import {
ComponentProps,
@@ -398,7 +399,7 @@ export function UsageChart() {
<>
<div className="flex items-center justify-between gap-4 px-4 py-3 text-xs">
<span className="text-content-emphasis font-semibold">
- {formatDate(d.date, { month: "short" })}
+ {formatDateTooltip(d.date, { interval, start, end })}
</span>
<span className="text-content-default font-medium">
{nFormatter(d.values.usage, { full: true })}
@@ -440,7 +441,12 @@ export function UsageChart() {
);
}}
>
- <XAxis highlightLast={false} />
+ <XAxis
+ highlightLast={false}
+ tickFormat={(date) =>
+ formatDateTooltip(date, { interval, start, end })
+ }
+ />
<YAxis showGridLines tickFormat={nFormatter} />
<Bars />
</TimeSeriesChart>
diff --git a/apps/web/lib/analytics/format-date-tooltip.ts b/apps/web/lib/analytics/format-date-tooltip.ts
index e36f1a2423..d2d1906e22 100644
--- a/apps/web/lib/analytics/format-date-tooltip.ts
+++ b/apps/web/lib/analytics/format-date-tooltip.ts
@@ -34,7 +34,7 @@ export const formatDateTooltip = (
hour: "numeric",
minute: "numeric",
});
- else if (daysDifference > 180)
+ else if (daysDifference > 90)
return targetDate.toLocaleDateString("en-US", {
month: "short",
year: "numeric",
diff --git a/apps/web/lib/analytics/utils/get-interval-data.ts b/apps/web/lib/analytics/utils/get-interval-data.ts
index 47d156edd8..94e9a211c8 100644
--- a/apps/web/lib/analytics/utils/get-interval-data.ts
+++ b/apps/web/lib/analytics/utils/get-interval-data.ts
@@ -15,7 +15,7 @@ const INTERVAL_DATA: Record<
({ timezone }: { timezone?: string }) => {
startDate: TZDate;
endDate: TZDate;
- granularity: "minute" | "hour" | "day" | "month";
+ granularity: "hour" | "day" | "month";
}
> = {
"24h": ({ timezone }) => ({
diff --git a/apps/web/lib/analytics/utils/get-start-end-dates.ts b/apps/web/lib/analytics/utils/get-start-end-dates.ts
index e86fd93418..28de2e9656 100644
--- a/apps/web/lib/analytics/utils/get-start-end-dates.ts
+++ b/apps/web/lib/analytics/utils/get-start-end-dates.ts
@@ -20,7 +20,7 @@ export const getStartEndDates = ({
let startDate: TZDate;
let endDate: TZDate;
- let granularity: "minute" | "hour" | "day" | "month" = "day";
+ let granularity: "hour" | "day" | "month" = "day";
if (start || (interval === "all" && dataAvailableFrom)) {
startDate = startOfDay(
@@ -34,7 +34,7 @@ export const getStartEndDates = ({
if (daysDifference <= 2) {
granularity = "hour";
- } else if (daysDifference > 180) {
+ } else if (daysDifference > 90) {
granularity = "month";
}
diff --git a/apps/web/lib/tinybird/get-workspace-usage.ts b/apps/web/lib/tinybird/get-workspace-usage.ts
index ac62602d21..0416818de2 100644
--- a/apps/web/lib/tinybird/get-workspace-usage.ts
+++ b/apps/web/lib/tinybird/get-workspace-usage.ts
@@ -6,9 +6,10 @@ import * as z from "zod/v4";
import { tb } from "./client";
const usagePipe = tb.buildPipe({
- pipe: "v3_usage",
+ pipe: "v4_usage",
parameters: usageQuerySchema.extend({
workspaceId: z.string(),
+ granularity: z.enum(["minute", "hour", "day", "month"]).optional(),
}),
data: usageResponse,
});
@@ -38,7 +39,7 @@ export async function getWorkspaceUsage({
end,
timezone = "UTC",
}: GetWorkspaceUsageParams) {
- const { startDate, endDate } = getStartEndDates({
+ const { startDate, endDate, granularity } = getStartEndDates({
interval,
start,
end,
@@ -51,6 +52,7 @@ export async function getWorkspaceUsage({
start: formatUTCDateTimeClickhouse(startDate),
end: formatUTCDateTimeClickhouse(endDate),
timezone,
+ granularity,
...(folderId && { folderId }),
...(domain && { domain }),
...(groupBy && { groupBy }),
diff --git a/packages/tinybird/pipes/v4_timeseries.pipe b/packages/tinybird/pipes/v4_timeseries.pipe
index 573cd2b321..0aefe2a91e 100644
--- a/packages/tinybird/pipes/v4_timeseries.pipe
+++ b/packages/tinybird/pipes/v4_timeseries.pipe
@@ -161,7 +161,7 @@ SQL >
)
{% else %} toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3)
{% end %} AS interval,
- uniq(click_id) as clicks
+ count(*) as clicks
FROM
{% if defined(customerId) %} dub_click_events_id
{% else %} dub_click_events_mv
@@ -319,7 +319,7 @@ SQL >
)
{% else %} toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3)
{% end %} AS interval,
- uniq(*) as leads
+ count(*) as leads
FROM dub_lead_events_mv
WHERE
true
@@ -536,7 +536,7 @@ SQL >
{% else %}
toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3)
{% end %} AS interval,
- uniq(*) AS sales,
+ count(*) AS sales,
sum(amount) AS amount
FROM
(
diff --git a/packages/tinybird/pipes/v3_usage_latest.pipe b/packages/tinybird/pipes/v4_usage.pipe
similarity index 73%
rename from packages/tinybird/pipes/v3_usage_latest.pipe
rename to packages/tinybird/pipes/v4_usage.pipe
index 8f6fa33ad7..cbc8895de5 100644
--- a/packages/tinybird/pipes/v3_usage_latest.pipe
+++ b/packages/tinybird/pipes/v4_usage.pipe
@@ -1,7 +1,32 @@
+NODE month_intervals
+SQL >
+
+%
+ WITH
+ toStartOfMonth(
+ toDateTime64({{ DateTime64(start, '2025-09-03 00:00:00.000') }}, 3),
+ {{ String(timezone, 'UTC') }}
+ ) AS start,
+ toStartOfMonth(
+ toDateTime64({{ DateTime64(end, '2025-10-03 00:00:00.000') }}, 3),
+ {{ String(timezone, 'UTC') }}
+ ) AS
+ end,
+ dateDiff('month', start, end) + 1 AS months_diff
+ SELECT
+ arrayJoin(
+ arrayMap(
+ x -> toDateTime64(start + toIntervalMonth(x), 3, {{ String(timezone, 'UTC') }}),
+ range(0, months_diff)
+ )
+ ) as interval
+
+
+
NODE day_intervals
SQL >
- %
+%
WITH
toStartOfDay(
toDateTime64({{ DateTime64(start, '2025-09-03 00:00:00.000') }}, 3),
@@ -27,11 +52,18 @@ SQL >
NODE usage_events_data
SQL >
- %
+%
SELECT
- toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) AS interval,
+ {% if granularity == "month" %}
+ toDateTime64(
+ toStartOfMonth(timestamp, {{ String(timezone, 'UTC') }}),
+ 3,
+ {{ String(timezone, 'UTC') }}
+ )
+ {% else %} toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3)
+ {% end %} AS interval,
{% if defined(groupBy) %} {% if groupBy == 'folder_id' %} folder_id {% else %} domain {% end %}, {% end %}
- uniq(*) AS events
+ count(*) AS events
FROM
(
-- Click events
@@ -52,9 +84,7 @@ SQL >
AND timestamp < {{ DateTime(end, '2025-10-03 00:00:00') }}
{% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %}
{% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
-
UNION ALL
-
-- Lead events
SELECT e.timestamp{% if defined(groupBy) %}, {% if groupBy == 'folder_id' %} m.folder_id {% else %} e.domain {% end %}, {% end %}
FROM dub_lead_events_mv e
@@ -65,9 +95,7 @@ SQL >
AND timestamp < {{ DateTime(end, '2025-10-03 00:00:00') }}
{% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %}
{% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
-
UNION ALL
-
-- Sale events
SELECT e.timestamp{% if defined(groupBy) %}, {% if groupBy == 'folder_id' %} m.folder_id {% else %} e.domain {% end %}, {% end %}
FROM dub_sale_events_mv e
@@ -87,12 +115,15 @@ SQL >
NODE usage_events
SQL >
- %
+%
SELECT
formatDateTime(interval, '%FT%T.000%z') as date,
{% if defined(groupBy) %} {% if groupBy == 'folder_id' %} folder_id {% else %} domain {% end %}, {% end %}
events as value
- FROM day_intervals
+ FROM
+ {% if granularity == "month" %} month_intervals
+ {% else %} day_intervals
+ {% end %}
LEFT JOIN usage_events_data USING interval
@@ -100,11 +131,18 @@ SQL >
NODE usage_links_data
SQL >
- %
+%
SELECT
- toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) AS interval,
+ {% if granularity == "month" %}
+ toDateTime64(
+ toStartOfMonth(timestamp, {{ String(timezone, 'UTC') }}),
+ 3,
+ {{ String(timezone, 'UTC') }}
+ )
+ {% else %} toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3)
+ {% end %} AS interval,
{% if defined(groupBy) %} {% if groupBy == 'folder_id' %} folder_id {% else %} domain {% end %}, {% end %}
- uniq(*) as links
+ count(*) as links
FROM dub_links_metadata_latest FINAL
WHERE
workspace_id
@@ -116,8 +154,8 @@ SQL >
required=True,
)
}}
- AND created_at >= {{ DateTime(start, '2025-09-03 00:00:00') }}
- AND created_at < {{ DateTime(end, '2025-10-03 00:00:00') }}
+ AND toDateTime64(timestamp, 3) >= {{ DateTime(start, '2025-09-03 00:00:00') }}
+ AND toDateTime64(timestamp, 3) < {{ DateTime(end, '2025-10-03 00:00:00') }}
{% if defined(folderId) %} AND folder_id = {{ folderId }}
{% end %}
{% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
@@ -129,12 +167,15 @@ SQL >
NODE usage_links
SQL >
- %
+%
SELECT
formatDateTime(interval, '%FT%T.000%z') as date,
{% if defined(groupBy) %} {% if groupBy == 'folder_id' %} folder_id {% else %} domain {% end %}, {% end %}
links as value
- FROM day_intervals
+ FROM
+ {% if granularity == "month" %} month_intervals
+ {% else %} day_intervals
+ {% end %}
LEFT JOIN usage_links_data USING interval
@@ -142,7 +183,7 @@ SQL >
NODE endpoint
SQL >
- %
+%
SELECT * FROM {% if resource == 'events' %} usage_events {% else %} usage_links {% end %}
TYPE ENDPOINT