From 293b3a555eacda7b916d20c2001896f4193e82ea Mon Sep 17 00:00:00 2001 From: sebastjanartic <45803536-sebastjanartic@users.noreply.replit.com> Date: Thu, 7 Aug 2025 12:45:03 +0000 Subject: [PATCH] Improve video fetching and counting for better platform performance Update server-side logic to optimize video retrieval from Bunny.net CDN by adjusting batch fetching and simplifying pagination and count mechanisms for improved efficiency. Replit-Commit-Author: Agent Replit-Commit-Session-Id: d7424866-83d1-4486-a212-ac12b4c7becf Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/d7424866-83d1-4486-a212-ac12b4c7becf/HJnSzeY --- client/src/pages/home.tsx | 1 + server/storage.ts | 46 +++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/client/src/pages/home.tsx b/client/src/pages/home.tsx index af9c65e..f3a5246 100644 --- a/client/src/pages/home.tsx +++ b/client/src/pages/home.tsx @@ -44,6 +44,7 @@ export default function Home() { // Update videos when new data comes in useEffect(() => { if (videosResponse) { + console.log('Videos response:', videosResponse); if (offset === 0) { setAllVideos(videosResponse.videos); } else { diff --git a/server/storage.ts b/server/storage.ts index 1472709..a0c1499 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -177,13 +177,15 @@ class BunnyStorage implements IStorage { async getVideos(limit = 20, offset = 0, search?: string): Promise { try { - // For search filtering, we need to get more videos than the requested limit - // because we'll filter client-side and then slice to the requested amount - const fetchLimit = search ? 100 : limit; - const page = Math.floor(offset / fetchLimit) + 1; + console.log(`Fetching videos: limit=${limit}, offset=${offset}, search=${search}`); - // Get videos from Bunny API - const { videos } = await this.bunnyService.getVideos(page, fetchLimit); + // For simple pagination, get a larger batch and slice on our side + // This is more reliable than complex page calculations + const batchSize = 100; // Get more videos to handle pagination properly + const page = Math.floor(offset / batchSize) + 1; + + const { videos } = await this.bunnyService.getVideos(page, batchSize); + console.log(`Bunny API returned ${videos.length} videos`); // Apply client-side filtering let filteredVideos = videos; @@ -195,6 +197,7 @@ class BunnyStorage implements IStorage { video.title.toLowerCase().includes(searchLower) || (video.description && video.description.toLowerCase().includes(searchLower)) ); + console.log(`After search filtering: ${filteredVideos.length} videos`); } // Apply cached view counts @@ -204,8 +207,14 @@ class BunnyStorage implements IStorage { } }); - // Apply pagination after filtering - return filteredVideos.slice(offset, offset + limit); + // Simple offset/limit slicing + const startIndex = offset % batchSize; + const endIndex = startIndex + limit; + const result = filteredVideos.slice(startIndex, endIndex); + + console.log(`Returning ${result.length} videos (slice ${startIndex}-${endIndex} from ${filteredVideos.length})`); + + return result; } catch (error) { console.error('Error fetching videos from Bunny:', error); // Fallback to empty array on error @@ -250,9 +259,24 @@ class BunnyStorage implements IStorage { async getVideoCount(search?: string): Promise { try { - // For accurate count with client-side filtering, we need to get all videos and filter them - const allVideos = await this.getVideos(1000, 0, search); - return allVideos.length; + // Get the total from Bunny API directly for better performance + const { total } = await this.bunnyService.getVideos(1, 1); + + // If no search, return total count + if (!search) { + return total; + } + + // For search, we need to get videos and count filtered results + // This is expensive but needed for accurate search counts + const { videos } = await this.bunnyService.getVideos(1, 1000); + const searchLower = search.toLowerCase(); + const filteredCount = videos.filter(video => + video.title.toLowerCase().includes(searchLower) || + (video.description && video.description.toLowerCase().includes(searchLower)) + ).length; + + return filteredCount; } catch (error) { console.error('Error getting video count from Bunny:', error); return 0;