Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add 'lifetime' analytics range support

Richie McIlroycdead2a120f7
diff --git a/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx b/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
index 5b8b66392..5b1e31fdf 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
+++ b/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
@@ -15,6 +15,7 @@ const RANGE_OPTIONS: { value: AnalyticsRange; label: string }[] = [
{ value: "24h", label: "Last 24 hours" },
{ value: "7d", label: "Last 7 days" },
{ value: "30d", label: "Last 30 days" },
+ { value: "lifetime", label: "Lifetime" },
];
const formatNumber = (value: number) =>
diff --git a/apps/web/app/(org)/dashboard/analytics/components/Header.tsx b/apps/web/app/(org)/dashboard/analytics/components/Header.tsx
index 9605a1a5f..a0dd840c3 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/Header.tsx
+++ b/apps/web/app/(org)/dashboard/analytics/components/Header.tsx
@@ -41,10 +41,16 @@ const DATE_RANGE_OPTIONS = [
{ value: "30d", label: "Last 30 days" },
{ value: "wtd", label: "Week to date" },
{ value: "mtd", label: "Month to date" },
+ { value: "lifetime", label: "Lifetime" },
] as const;
const mapToBackendRange = (value: string): AnalyticsRange => {
- if (value === "24h" || value === "7d" || value === "30d") {
+ if (
+ value === "24h" ||
+ value === "7d" ||
+ value === "30d" ||
+ value === "lifetime"
+ ) {
return value as AnalyticsRange;
}
if (value === "today" || value === "yesterday") {
@@ -81,6 +87,7 @@ const getDisplayValue = (
return lastUISelection;
}
}
+ if (backendValue === "lifetime") return "lifetime";
if (backendValue === "24h") return "24h";
if (backendValue === "7d") return "7d";
if (backendValue === "30d") return "30d";
@@ -119,6 +126,8 @@ export default function Header({
setLastUISelection("7d");
} else if (value === "30d") {
setLastUISelection("30d");
+ } else if (value === "lifetime") {
+ setLastUISelection("lifetime");
}
}
}, [value, lastUISelection]);
diff --git a/apps/web/app/(org)/dashboard/analytics/data.ts b/apps/web/app/(org)/dashboard/analytics/data.ts
index debc4d6a4..98da6e853 100644
--- a/apps/web/app/(org)/dashboard/analytics/data.ts
+++ b/apps/web/app/(org)/dashboard/analytics/data.ts
@@ -15,6 +15,8 @@ import type {
type VideoRow = typeof videos.$inferSelect;
type OrgId = VideoRow["orgId"];
type VideoId = VideoRow["id"];
+type SpaceVideoRow = typeof spaceVideos.$inferSelect;
+type SpaceOrOrgId = SpaceVideoRow["spaceId"];
type CountSeriesRow = { bucket: string; count: number };
type ViewSeriesRow = { bucket: string; views: number };
@@ -31,8 +33,10 @@ type TinybirdAnalyticsData = {
topCapsRaw: TopCapRow[];
};
-const RANGE_CONFIG: Record<
- AnalyticsRange,
+type RollingAnalyticsRange = Exclude<AnalyticsRange, "lifetime">;
+
+const ROLLING_RANGE_CONFIG: Record<
+ RollingAnalyticsRange,
{ hours: number; bucket: "hour" | "day" }
> = {
"24h": { hours: 24, bucket: "hour" },
@@ -40,6 +44,8 @@ const RANGE_CONFIG: Record<
"30d": { hours: 30 * 24, bucket: "day" },
};
+const LIFETIME_FALLBACK_DAYS = 30;
+
const escapeLiteral = (value: string) => value.replace(/'/g, "''");
const toDateString = (date: Date) => date.toISOString().slice(0, 10);
const toDateTimeString = (date: Date) =>
@@ -80,6 +86,47 @@ const buildBuckets = (from: Date, to: Date, bucket: "hour" | "day") => {
return buckets;
};
+const getLifetimeRangeStart = async (
+ orgId: OrgId,
+ videoIds?: VideoId[],
+): Promise<Date | undefined> => {
+ const whereClause = videoIds && videoIds.length > 0
+ ? and(eq(videos.orgId, orgId), inArray(videos.id, videoIds))
+ : eq(videos.orgId, orgId);
+
+ const rows = await db()
+ .select({ minCreatedAt: sql<Date>`MIN(${videos.createdAt})` })
+ .from(videos)
+ .where(whereClause)
+ .limit(1);
+
+ const candidate = rows[0]?.minCreatedAt;
+ if (!candidate) return undefined;
+ return candidate instanceof Date ? candidate : new Date(candidate);
+};
+
+const resolveRangeBounds = async (
+ range: AnalyticsRange,
+ orgId: OrgId,
+ videoIds?: VideoId[],
+): Promise<{ from: Date; to: Date; bucket: "hour" | "day" }> => {
+ const to = new Date();
+
+ if (range === "lifetime") {
+ const lifetimeStart = await getLifetimeRangeStart(orgId, videoIds);
+ const fallbackFrom = new Date(
+ to.getTime() - LIFETIME_FALLBACK_DAYS * 24 * 60 * 60 * 1000,
+ );
+ const from =
+ lifetimeStart && lifetimeStart < to ? lifetimeStart : fallbackFrom;
+ return { from, to, bucket: "day" };
+ }
+
+ const config = ROLLING_RANGE_CONFIG[range];
+ const from = new Date(to.getTime() - config.hours * 60 * 60 * 1000);
+ return { from, to, bucket: config.bucket };
+};
+
const fallbackIfEmpty = <Row>(
primary: Effect.Effect<Row[], never, never>,
fallback?: Effect.Effect<Row[], never, never>,
@@ -101,27 +148,21 @@ const withTinybirdFallback = <Row>(
return Effect.succeed<{ data: Row[] }>({ data: [] as Row[] });
}),
Effect.map((res) => {
- console.log("tinybird raw response", JSON.stringify(res, null, 2));
- const response = res as { data: unknown[] };
+ const response = res as { data?: unknown[] };
const data = response.data ?? [];
- console.log("tinybird data array", JSON.stringify(data, null, 2));
- const filtered = data.filter((item): item is Row => {
- const isObject = typeof item === "object" && item !== null;
- if (!isObject) {
- console.log("filtered out non-object item", typeof item, item);
- }
- return isObject;
- }) as Row[];
- console.log("tinybird filtered rows", JSON.stringify(filtered, null, 2));
- return filtered;
+ return data.filter((item): item is Row =>
+ typeof item === "object" && item !== null,
+ ) as Row[];
}),
);
-const getSpaceVideoIds = async (spaceId: string): Promise<VideoId[]> => {
+const getSpaceVideoIds = async (
+ spaceId: SpaceOrOrgId,
+): Promise<VideoId[]> => {
const rows = await db()
.select({ videoId: spaceVideos.videoId })
.from(spaceVideos)
- .where(eq(spaceVideos.spaceId, spaceId as any));
+ .where(eq(spaceVideos.spaceId, spaceId));
return rows.map((row) => row.videoId);
};
@@ -131,16 +172,21 @@ export const getOrgAnalyticsData = async (
spaceId?: string,
capId?: string,
): Promise<OrgAnalyticsResponse> => {
- const rangeConfig = RANGE_CONFIG[range];
- const to = new Date();
- const from = new Date(to.getTime() - rangeConfig.hours * 60 * 60 * 1000);
- const buckets = buildBuckets(from, to, rangeConfig.bucket);
const typedOrgId = orgId as OrgId;
- const spaceVideoIds = spaceId ? await getSpaceVideoIds(spaceId) : undefined;
+ const spaceVideoIds = spaceId
+ ? await getSpaceVideoIds(spaceId as SpaceOrOrgId)
+ : undefined;
const capVideoIds = capId ? [capId as VideoId] : undefined;
const videoIds = capVideoIds || spaceVideoIds;
+ const { from, to, bucket } = await resolveRangeBounds(
+ range,
+ typedOrgId,
+ videoIds,
+ );
+ const buckets = buildBuckets(from, to, bucket);
+
if (
(spaceId && spaceVideoIds && spaceVideoIds.length === 0) ||
(capId && !capVideoIds)
@@ -177,13 +223,13 @@ export const getOrgAnalyticsData = async (
}
const [capsSeries, commentSeries, reactionSeries] = await Promise.all([
- queryVideoSeries(typedOrgId, from, to, rangeConfig.bucket, videoIds),
+ queryVideoSeries(typedOrgId, from, to, bucket, videoIds),
queryCommentsSeries(
typedOrgId,
from,
to,
"text",
- rangeConfig.bucket,
+ bucket,
videoIds,
),
queryCommentsSeries(
@@ -191,34 +237,23 @@ export const getOrgAnalyticsData = async (
from,
to,
"emoji",
- rangeConfig.bucket,
+ bucket,
videoIds,
),
]);
- const tinybirdData = await runPromise(
- Effect.gen(function* () {
- const tinybird = yield* Tinybird;
- console.log("getOrgAnalyticsData - orgId:", orgId, "range:", range);
- console.log(
- "getOrgAnalyticsData - from:",
- from.toISOString(),
- "to:",
- to.toISOString(),
- );
+ const tinybirdData = await runPromise(
+ Effect.gen(function* () {
+ const tinybird = yield* Tinybird;
const viewSeries = yield* queryViewSeries(
tinybird,
typedOrgId,
from,
to,
- rangeConfig.bucket,
+ bucket,
videoIds,
);
- console.log(
- "getOrgAnalyticsData - viewSeries:",
- JSON.stringify(viewSeries, null, 2),
- );
const countries = yield* queryCountries(
tinybird,
@@ -227,10 +262,6 @@ export const getOrgAnalyticsData = async (
to,
videoIds,
);
- console.log(
- "getOrgAnalyticsData - countries:",
- JSON.stringify(countries, null, 2),
- );
const cities = yield* queryCities(
tinybird,
@@ -239,10 +270,6 @@ export const getOrgAnalyticsData = async (
to,
videoIds,
);
- console.log(
- "getOrgAnalyticsData - cities:",
- JSON.stringify(cities, null, 2),
- );
const browsers = yield* queryBrowsers(
tinybird,
@@ -251,10 +278,6 @@ export const getOrgAnalyticsData = async (
to,
videoIds,
);
- console.log(
- "getOrgAnalyticsData - browsers:",
- JSON.stringify(browsers, null, 2),
- );
const devices = yield* queryDevices(
tinybird,
@@ -263,10 +286,6 @@ export const getOrgAnalyticsData = async (
to,
videoIds,
);
- console.log(
- "getOrgAnalyticsData - devices:",
- JSON.stringify(devices, null, 2),
- );
const operatingSystems = yield* queryOperatingSystems(
tinybird,
@@ -275,18 +294,10 @@ export const getOrgAnalyticsData = async (
to,
videoIds,
);
- console.log(
- "getOrgAnalyticsData - operatingSystems:",
- JSON.stringify(operatingSystems, null, 2),
- );
const topCapsRaw = capId
? []
: yield* queryTopCaps(tinybird, typedOrgId, from, to, videoIds);
- console.log(
- "getOrgAnalyticsData - topCapsRaw:",
- JSON.stringify(topCapsRaw, null, 2),
- );
return {
viewSeries,
diff --git a/apps/web/app/(org)/dashboard/analytics/types.ts b/apps/web/app/(org)/dashboard/analytics/types.ts
index 67752e386..44293f10b 100644
--- a/apps/web/app/(org)/dashboard/analytics/types.ts
+++ b/apps/web/app/(org)/dashboard/analytics/types.ts
@@ -1,4 +1,4 @@
-export type AnalyticsRange = "24h" | "7d" | "30d";
+export type AnalyticsRange = "24h" | "7d" | "30d" | "lifetime";
export interface BreakdownRow {
name: string;
diff --git a/apps/web/app/api/analytics/track/route.ts b/apps/web/app/api/analytics/track/route.ts
index e02abd76b..4cdca5e76 100644
--- a/apps/web/app/api/analytics/track/route.ts
+++ b/apps/web/app/api/analytics/track/route.ts
@@ -81,8 +81,7 @@ export async function POST(request: NextRequest) {
return;
}
- // @ts-expect-error - Tinybird service can be yielded directly
- const tinybird = yield* Tinybird;
+ const tinybird = yield* Tinybird;
yield* tinybird.appendEvents([
{
timestamp: timestamp.toISOString(),
diff --git a/apps/web/app/api/dashboard/analytics/route.ts b/apps/web/app/api/dashboard/analytics/route.ts
index f31b4122c..4f942b41e 100644
--- a/apps/web/app/api/dashboard/analytics/route.ts
+++ b/apps/web/app/api/dashboard/analytics/route.ts
@@ -4,7 +4,7 @@ import type { NextRequest } from "next/server";
import { getOrgAnalyticsData } from "@/app/(org)/dashboard/analytics/data";
import type { AnalyticsRange } from "@/app/(org)/dashboard/analytics/types";
-const RANGE_VALUES: AnalyticsRange[] = ["24h", "7d", "30d"];
+const RANGE_VALUES: AnalyticsRange[] = ["24h", "7d", "30d", "lifetime"];
export const dynamic = "force-dynamic";
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 828606d40..32ff0ea39 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -118,7 +118,7 @@ importers:
version: 0.14.10([email protected])
'@solidjs/start':
specifier: ^1.1.3
'@tanstack/solid-query':
specifier: ^5.51.21
version: 5.75.4([email protected])
@@ -205,7 +205,7 @@ importers:
version: 9.0.1
vinxi:
specifier: ^0.5.6
webcodecs:
specifier: ^0.1.0
version: 0.1.0
@@ -20011,11 +20011,11 @@ snapshots:
dependencies:
solid-js: 1.9.6
dependencies:
'@tanstack/server-functions-plugin': 1.119.2(@types/[email protected])([email protected])([email protected])([email protected])
defu: 6.1.4
error-stack-parser: 2.1.4
html-to-image: 1.11.13
@@ -20026,7 +20026,7 @@ snapshots:
source-map-js: 1.2.1
terracotta: 1.0.6([email protected])
tinyglobby: 0.2.13
transitivePeerDependencies:
- '@testing-library/jest-dom'
@@ -21294,7 +21294,7 @@ snapshots:
untun: 0.1.3
uqr: 0.1.2
dependencies:
'@babel/parser': 7.27.2
acorn: 8.14.1
@@ -21305,18 +21305,18 @@ snapshots:
magicast: 0.2.11
recast: 0.23.11
tslib: 2.8.1
dependencies:
acorn: 8.14.1
acorn-loose: 8.5.0
acorn-typescript: 1.4.13([email protected])
astring: 1.9.0
magicast: 0.2.11
recast: 0.23.11
'@virtual-grid/[email protected]': {}
@@ -25999,7 +25999,7 @@ snapshots:
cors: 2.8.5
dependencies:
'@cloudflare/kv-asset-handler': 0.4.0
'@netlify/functions': 3.1.5([email protected])([email protected])
@@ -26053,7 +26053,7 @@ snapshots:
pretty-bytes: 6.1.1
radix3: 1.1.2
rollup: 4.40.2
- rollup-plugin-visualizer: 5.14.0([email protected])
+ rollup-plugin-visualizer: 5.14.0([email protected])([email protected])
scule: 1.3.0
semver: 7.7.2
serve-placeholder: 2.0.2
@@ -27429,13 +27429,14 @@ snapshots:
dependencies:
rollup-plugin-inject: 3.0.2
dependencies:
open: 8.4.2
picomatch: 4.0.3
source-map: 0.7.4
yargs: 17.7.2
optionalDependencies:
+ rolldown: 1.0.0-beta.50
rollup: 4.40.2
@@ -29158,7 +29159,7 @@ snapshots:
d3-time: 3.1.0
d3-timer: 3.0.1
dependencies:
'@babel/core': 7.27.1
'@babel/plugin-syntax-jsx': 7.27.1(@babel/[email protected])
@@ -29180,7 +29181,7 @@ snapshots:
hookable: 5.5.3
http-proxy: 1.18.1
micromatch: 4.0.8
node-fetch-native: 1.6.6
path-to-regexp: 6.3.0
pathe: 1.1.2