Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

improvement

Ashish Viradiya150334941cd0
diff --git a/lib/swr/use-viewers.ts b/lib/swr/use-viewers.ts
index 583ebd19..1e4ca450 100644
--- a/lib/swr/use-viewers.ts
+++ b/lib/swr/use-viewers.ts
@@ -60,6 +60,7 @@ export default function useViewers(
data: response,
isValidating,
error,
+ mutate,
} = useSWR<ViewersResponse>(
teamId
? `/api/teams/${teamId}/viewers?${queryString}`
@@ -67,8 +68,13 @@ export default function useViewers(
fetcher,
{
revalidateOnFocus: false,
+ revalidateIfStale: false,
+ revalidateOnReconnect: false,
dedupingInterval: 30000,
keepPreviousData: true,
+ refreshInterval: 0,
+ errorRetryCount: 2,
+ errorRetryInterval: 5000,
},
);
@@ -80,5 +86,6 @@ export default function useViewers(
loading: !response && !error,
isFiltered: !!searchQuery,
error,
+ mutate,
};
}
diff --git a/pages/api/teams/[teamId]/viewers/[id]/index.ts b/pages/api/teams/[teamId]/viewers/[id]/index.ts
index b1bfc773..a316681e 100644
--- a/pages/api/teams/[teamId]/viewers/[id]/index.ts
+++ b/pages/api/teams/[teamId]/viewers/[id]/index.ts
@@ -23,8 +23,7 @@ async function fetchAndCacheDurations(
const parsedDurations = typeof cachedDurations === 'string' ? JSON.parse(cachedDurations) : cachedDurations;
durationsMap = parsedDurations;
} else {
- const batchSize = 5;
-
+ const batchSize = 10;
for (let i = 0; i < groupedViews.length; i += batchSize) {
const batch = groupedViews.slice(i, i + batchSize);
@@ -82,7 +81,7 @@ export default async function handle(
// Parse pagination parameters
const currentPage = parseInt(page || "1", 10);
- const limit = parseInt(pageSize || "10", 10);
+ const limit = Math.min(parseInt(pageSize || "10", 10), 100); // Cap at 100 for performance
const offset = (currentPage - 1) * limit;
// Parse sorting parameters
@@ -103,6 +102,7 @@ export default async function handle(
},
},
},
+ select: { id: true, plan: true },
});
if (!team || team.plan === "free") {
@@ -123,34 +123,9 @@ export default async function handle(
return res.status(404).json({ message: "Viewer not found" });
}
- // Create cache key for this specific query
- const cacheKey = `viewer-data:${teamId}:${id}:${currentPage}:${limit}:${sort}:${order}`;
-
- // If withDuration=true, try to get cached data from Redis first
- if (withDuration === "true") {
- const cachedData = await redis.get(cacheKey);
-
- if (cachedData) {
- try {
- const groupedViews = typeof cachedData === 'string' ? JSON.parse(cachedData) : cachedData;
-
- if (!groupedViews || groupedViews.length === 0) {
- return res.status(200).json({ durations: {} });
- }
-
- // Try to get cached durations from Redis
- const durationCacheKey = `durations:${teamId}:${id}:${currentPage}:${limit}:${sort}:${order}`;
- const durationsMap = await fetchAndCacheDurations(groupedViews, teamId, id, durationCacheKey);
-
- return res.status(200).json({ durations: durationsMap });
-
- } catch (error) {
- console.error("Error processing cached data:", error);
- }
- }
- }
+ // Enhanced cache key
+ const cacheKey = `viewer-details:${teamId}:${id}:${currentPage}:${limit}:${sort}:${order}:v2`;
- // Optimize: Use different approaches based on sort type
let groupedViews: Array<{
documentId: string;
viewCount: number;
@@ -162,23 +137,38 @@ export default async function handle(
totalDuration?: number;
}>;
- const orderDirection = order?.toUpperCase() || "DESC";
+ let orderByClause: Prisma.Sql;
+ if (sort === "lastViewed") {
+ orderByClause = order === "desc"
+ ? Prisma.sql`"lastViewed" DESC`
+ : Prisma.sql`"lastViewed" ASC`;
+ } else if (sort === "viewCount") {
+ orderByClause = order === "desc"
+ ? Prisma.sql`"viewCount" DESC, "lastViewed" DESC`
+ : Prisma.sql`"viewCount" ASC, "lastViewed" DESC`;
+ } else {
+ orderByClause = Prisma.sql`"lastViewed" DESC`;
+ }
if (sort === "totalDuration") {
const allDocuments = await prisma.$queryRaw`
- SELECT
- v."documentId",
- COUNT(v.id)::int as "viewCount",
- MAX(v."viewedAt") as "lastViewed",
- d.name as "documentName",
- d.type as "documentType",
- d."contentType" as "documentContentType",
- ARRAY_AGG(v.id) as "viewIds"
- FROM "View" v
- INNER JOIN "Document" d ON v."documentId" = d.id
- WHERE v."viewerId" = ${id}
- AND v."documentId" IS NOT NULL
- GROUP BY v."documentId", d.name, d.type, d."contentType"
+ WITH viewer_documents AS (
+ SELECT
+ v."documentId",
+ COUNT(v.id)::int as "viewCount",
+ MAX(v."viewedAt") as "lastViewed",
+ d.name as "documentName",
+ d.type as "documentType",
+ d."contentType" as "documentContentType",
+ ARRAY_AGG(v.id ORDER BY v."viewedAt" DESC) as "viewIds"
+ FROM "View" v
+ INNER JOIN "Document" d ON v."documentId" = d.id
+ WHERE v."viewerId" = ${id}
+ AND v."documentId" IS NOT NULL
+ GROUP BY v."documentId", d.name, d.type, d."contentType"
+ )
+ SELECT * FROM viewer_documents
+ ORDER BY "lastViewed" DESC
` as Array<{
documentId: string;
viewCount: number;
@@ -189,8 +179,7 @@ export default async function handle(
viewIds: string[];
}>;
- // Check for cached durations in Redis for totalDuration sorting
- const durationCacheKey = `durations:${teamId}:${id}:all:${sort}:${order}`;
+ const durationCacheKey = `durations:${teamId}:${id}:all:duration-sort`;
const durationsMap = await fetchAndCacheDurations(allDocuments, teamId, id, durationCacheKey);
const documentsWithDurations = allDocuments.map(doc => ({
@@ -198,7 +187,7 @@ export default async function handle(
totalDuration: durationsMap[doc.documentId] || 0
}));
- // Sort by duration
+ // Sort by duration efficiently
documentsWithDurations.sort((a, b) => {
return order === "asc"
? a.totalDuration - b.totalDuration
@@ -209,33 +198,25 @@ export default async function handle(
groupedViews = documentsWithDurations.slice(offset, offset + limit);
} else {
- let orderClause;
- switch (sort) {
- case "lastViewed":
- orderClause = `MAX(v."viewedAt") ${orderDirection === "ASC" ? "ASC" : "DESC"}`;
- break;
- case "viewCount":
- orderClause = `COUNT(v.id) ${orderDirection === "ASC" ? "ASC" : "DESC"}`;
- break;
- default:
- orderClause = `MAX(v."viewedAt") ${orderDirection === "ASC" ? "ASC" : "DESC"}`;
- }
-
+ // Optimized query leveraging our performance indexes
groupedViews = await prisma.$queryRaw`
- SELECT
- v."documentId",
- COUNT(v.id)::int as "viewCount",
- MAX(v."viewedAt") as "lastViewed",
- d.name as "documentName",
- d.type as "documentType",
- d."contentType" as "documentContentType",
- ARRAY_AGG(v.id) as "viewIds"
- FROM "View" v
- INNER JOIN "Document" d ON v."documentId" = d.id
- WHERE v."viewerId" = ${id}
- AND v."documentId" IS NOT NULL
- GROUP BY v."documentId", d.name, d.type, d."contentType"
- ORDER BY ${Prisma.raw(orderClause)}
+ WITH viewer_documents AS (
+ SELECT
+ v."documentId",
+ COUNT(v.id)::int as "viewCount",
+ MAX(v."viewedAt") as "lastViewed",
+ d.name as "documentName",
+ d.type as "documentType",
+ d."contentType" as "documentContentType",
+ ARRAY_AGG(v.id ORDER BY v."viewedAt" DESC) as "viewIds"
+ FROM "View" v
+ INNER JOIN "Document" d ON v."documentId" = d.id
+ WHERE v."viewerId" = ${id}
+ AND v."documentId" IS NOT NULL
+ GROUP BY v."documentId", d.name, d.type, d."contentType"
+ )
+ SELECT * FROM viewer_documents
+ ORDER BY ${orderByClause}
LIMIT ${limit}
OFFSET ${offset}
` as Array<{
@@ -249,7 +230,6 @@ export default async function handle(
}>;
}
- // Get total count for pagination
const totalCountResult = await prisma.$queryRaw`
SELECT COUNT(DISTINCT v."documentId")::int as count
FROM "View" v
@@ -260,17 +240,14 @@ export default async function handle(
const totalItems = totalCountResult[0]?.count || 0;
const totalPages = Math.ceil(totalItems / limit);
- // Cache the grouped views to Redis (only if not withDuration call)
- if (withDuration !== "true") {
- await redis.set(cacheKey, JSON.stringify(groupedViews), { ex: 300 });
- }
-
// If withDuration=true, return only duration data for faster response
if (withDuration === "true") {
try {
const durationCacheKey = `durations:${teamId}:${id}:${currentPage}:${limit}:${sort}:${order}`;
const durationsMap = await fetchAndCacheDurations(groupedViews, teamId, id, durationCacheKey);
+ // Add cache headers
+ res.setHeader('Cache-Control', 'public, s-maxage=60, stale-while-revalidate=300');
return res.status(200).json({ durations: durationsMap });
} catch (error) {
@@ -314,8 +291,14 @@ export default async function handle(
},
};
+ if (withDuration !== "true") {
+ await redis.set(cacheKey, JSON.stringify(formattedViews), { ex: 600 }); // 10 min cache
+ }
+ res.setHeader('Cache-Control', 'public, s-maxage=30, stale-while-revalidate=300');
+
return res.status(200).json(newViewer);
} catch (error) {
+ console.error('Viewer Details API Error:', error);
errorhandler(error, res);
}
} else {
diff --git a/pages/api/teams/[teamId]/viewers/index.ts b/pages/api/teams/[teamId]/viewers/index.ts
index e5d481f3..ea912d69 100644
--- a/pages/api/teams/[teamId]/viewers/index.ts
+++ b/pages/api/teams/[teamId]/viewers/index.ts
@@ -30,7 +30,7 @@ export default async function handle(
const { teamId } = req.query as { teamId: string };
const currentPage = parseInt(page || "1", 10);
- const limit = parseInt(pageSize || "10", 10);
+ const limit = Math.min(parseInt(pageSize || "10", 10), 100);
const offset = (currentPage - 1) * limit;
const validSortFields = ["lastViewed", "totalVisits"];
@@ -50,17 +50,59 @@ export default async function handle(
},
},
},
+ select: { id: true, plan: true },
});
if (!team || team.plan === "free") {
return res.status(404).json({ error: "Team not found" });
}
- const searchCondition = query
+ const searchCondition = query
? Prisma.sql`AND LOWER(v.email) LIKE LOWER(${`%${query}%`})`
: Prisma.empty;
- let viewers: Array<{
+ let orderByClause: Prisma.Sql;
+ if (sort === 'totalVisits') {
+ orderByClause = order === 'desc'
+ ? Prisma.sql`"totalVisits" DESC, "createdAt" DESC`
+ : Prisma.sql`"totalVisits" ASC, "createdAt" DESC`;
+ } else if (sort === 'lastViewed') {
+ orderByClause = order === 'desc'
+ ? Prisma.sql`"lastViewed" DESC NULLS LAST, "createdAt" DESC`
+ : Prisma.sql`"lastViewed" ASC NULLS LAST, "createdAt" DESC`;
+ } else {
+ orderByClause = order === 'desc'
+ ? Prisma.sql`"createdAt" DESC`
+ : Prisma.sql`"createdAt" ASC`;
+ }
+
+ const viewersWithStats = await prisma.$queryRaw`
+ WITH viewer_stats AS (
+ SELECT
+ v.id,
+ v.email,
+ v."createdAt",
+ v."updatedAt",
+ COALESCE(vs.total_visits, 0)::int as "totalVisits",
+ vs.last_viewed as "lastViewed"
+ FROM "Viewer" v
+ LEFT JOIN (
+ SELECT
+ "viewerId",
+ COUNT(*)::int as total_visits,
+ MAX("viewedAt") as last_viewed
+ FROM "View"
+ WHERE "documentId" IS NOT NULL
+ GROUP BY "viewerId"
+ ) vs ON v.id = vs."viewerId"
+ WHERE v."teamId" = ${teamId}
+ ${searchCondition}
+ )
+ SELECT * FROM viewer_stats
+ ORDER BY ${orderByClause}
+ LIMIT ${limit}
+ OFFSET ${offset}
+ ` as Array<{
id: string;
email: string;
createdAt: Date;
@@ -69,46 +111,6 @@ export default async function handle(
lastViewed: Date | null;
}>;
- const getOrderClause = (sortField: string, sortOrder: string): Prisma.Sql => {
- const key = `${sortField}_${sortOrder}`;
-
- const orderByWhitelist: Record<string, Prisma.Sql> = {
- lastViewed_asc: Prisma.sql`view_stats."lastViewed" ASC NULLS LAST`,
- lastViewed_desc: Prisma.sql`view_stats."lastViewed" DESC NULLS LAST`,
- totalVisits_asc: Prisma.sql`COALESCE(view_stats."totalVisits", 0) ASC, v."createdAt" DESC`,
- totalVisits_desc: Prisma.sql`COALESCE(view_stats."totalVisits", 0) DESC, v."createdAt" DESC`,
- };
-
- return orderByWhitelist[key] || orderByWhitelist.lastViewed_desc;
- };
- const orderClause = getOrderClause(sort || 'lastViewed', order || 'desc');
-
- viewers = await prisma.$queryRaw`
- SELECT
- v.id,
- v.email,
- v."createdAt",
- v."updatedAt",
- COALESCE(view_stats."totalVisits", 0)::int as "totalVisits",
- view_stats."lastViewed"
- FROM "Viewer" v
- LEFT JOIN (
- SELECT
- "viewerId",
- COUNT(*)::int as "totalVisits",
- MAX("viewedAt") as "lastViewed"
- FROM "View"
- WHERE "documentId" IS NOT NULL
- GROUP BY "viewerId"
- ) view_stats ON v.id = view_stats."viewerId"
- WHERE v."teamId" = ${teamId}
- ${searchCondition}
- ORDER BY ${orderClause}
- LIMIT ${limit}
- OFFSET ${offset}
- `;
-
- // Get total count for pagination
const totalCountResult = await prisma.$queryRaw`
SELECT COUNT(*)::int as count
FROM "Viewer" v
@@ -119,8 +121,7 @@ export default async function handle(
const totalCount = totalCountResult[0]?.count || 0;
const totalPages = Math.ceil(totalCount / limit);
- // Return clean, essential data only
- const formattedViewers = viewers.map((viewer) => ({
+ const formattedViewers = viewersWithStats.map((viewer) => ({
id: viewer.id,
email: viewer.email,
createdAt: viewer.createdAt,
@@ -145,6 +146,9 @@ export default async function handle(
},
};
+
+ res.setHeader('Cache-Control', 'public, s-maxage=30, stale-while-revalidate=300');
+
return res.status(200).json(response);
} catch (error) {
errorhandler(error, res);
diff --git a/prisma/migrations/20250624020507_indexed_view_modal/migration.sql b/prisma/migrations/20250624020507_indexed_view_modal/migration.sql
deleted file mode 100644
index ab204897..00000000
--- a/prisma/migrations/20250624020507_indexed_view_modal/migration.sql
+++ /dev/null
@@ -1,2 +0,0 @@
--- CreateIndex
-CREATE INDEX "View_viewerId_idx" ON "View"("viewerId");
diff --git a/prisma/migrations/20250624042156_viewers_performance_optimization/migration.sql b/prisma/migrations/20250624042156_viewers_performance_optimization/migration.sql
new file mode 100644
index 00000000..8fd5c507
--- /dev/null
+++ b/prisma/migrations/20250624042156_viewers_performance_optimization/migration.sql
@@ -0,0 +1,17 @@
+CREATE EXTENSION IF NOT EXISTS pg_trgm;
+
+CREATE INDEX IF NOT EXISTS "View_viewerId_idx" ON "View"("viewerId");
+
+CREATE INDEX IF NOT EXISTS "View_viewedAt_idx" ON "View"("viewedAt" DESC);
+
+CREATE INDEX IF NOT EXISTS "View_viewerId_documentId_idx" ON "View"("viewerId", "documentId");
+
+-- Index for viewer email search
+CREATE INDEX IF NOT EXISTS "Viewer_email_trgm_idx" ON "Viewer" USING gin(lower("email") gin_trgm_ops);
+
+CREATE INDEX IF NOT EXISTS "Viewer_teamId_email_idx" ON "Viewer"("teamId", lower("email"));
+
+-- Additional indexes for sorting optimization
+CREATE INDEX IF NOT EXISTS "Viewer_teamId_createdAt_idx" ON "Viewer"("teamId", "createdAt" DESC);
+
+CREATE INDEX IF NOT EXISTS "View_viewerId_viewedAt_idx" ON "View"("viewerId", "viewedAt" DESC) WHERE "documentId" IS NOT NULL;
diff --git a/prisma/schema/schema.prisma b/prisma/schema/schema.prisma
index d319a3a2..8a8242c1 100644
--- a/prisma/schema/schema.prisma
+++ b/prisma/schema/schema.prisma
@@ -298,6 +298,8 @@ model View {
@@index([dataroomViewId])
@@index([viewerId])
@@index([teamId])
+ @@index([viewedAt(sort: Desc)]) // Performance optimization for date aggregations
+ @@index([viewerId, documentId]) // Performance optimization for joins with filtering
}
enum ViewType {