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
This commit is contained in:
sebastjanartic 2025-08-07 12:45:03 +00:00
parent 3c6378c8b9
commit 293b3a555e
2 changed files with 36 additions and 11 deletions

View File

@ -44,6 +44,7 @@ export default function Home() {
// Update videos when new data comes in // Update videos when new data comes in
useEffect(() => { useEffect(() => {
if (videosResponse) { if (videosResponse) {
console.log('Videos response:', videosResponse);
if (offset === 0) { if (offset === 0) {
setAllVideos(videosResponse.videos); setAllVideos(videosResponse.videos);
} else { } else {

View File

@ -177,13 +177,15 @@ class BunnyStorage implements IStorage {
async getVideos(limit = 20, offset = 0, search?: string): Promise<Video[]> { async getVideos(limit = 20, offset = 0, search?: string): Promise<Video[]> {
try { try {
// For search filtering, we need to get more videos than the requested limit console.log(`Fetching videos: limit=${limit}, offset=${offset}, search=${search}`);
// 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;
// Get videos from Bunny API // For simple pagination, get a larger batch and slice on our side
const { videos } = await this.bunnyService.getVideos(page, fetchLimit); // 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 // Apply client-side filtering
let filteredVideos = videos; let filteredVideos = videos;
@ -195,6 +197,7 @@ class BunnyStorage implements IStorage {
video.title.toLowerCase().includes(searchLower) || video.title.toLowerCase().includes(searchLower) ||
(video.description && video.description.toLowerCase().includes(searchLower)) (video.description && video.description.toLowerCase().includes(searchLower))
); );
console.log(`After search filtering: ${filteredVideos.length} videos`);
} }
// Apply cached view counts // Apply cached view counts
@ -204,8 +207,14 @@ class BunnyStorage implements IStorage {
} }
}); });
// Apply pagination after filtering // Simple offset/limit slicing
return filteredVideos.slice(offset, offset + limit); 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) { } catch (error) {
console.error('Error fetching videos from Bunny:', error); console.error('Error fetching videos from Bunny:', error);
// Fallback to empty array on error // Fallback to empty array on error
@ -250,9 +259,24 @@ class BunnyStorage implements IStorage {
async getVideoCount(search?: string): Promise<number> { async getVideoCount(search?: string): Promise<number> {
try { try {
// For accurate count with client-side filtering, we need to get all videos and filter them // Get the total from Bunny API directly for better performance
const allVideos = await this.getVideos(1000, 0, search); const { total } = await this.bunnyService.getVideos(1, 1);
return allVideos.length;
// 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) { } catch (error) {
console.error('Error getting video count from Bunny:', error); console.error('Error getting video count from Bunny:', error);
return 0; return 0;