Fix: Add batch processing to prevent timeout on large song libraries

This commit is contained in:
Hördle Bot
2025-11-22 12:11:11 +01:00
parent 15746f404a
commit 10cab22cfe
2 changed files with 85 additions and 24 deletions

View File

@@ -134,19 +134,56 @@ export default function AdminPage() {
setCategorizationResults(null);
try {
const res = await fetch('/api/categorize', {
method: 'POST',
let offset = 0;
let hasMore = true;
let allResults: any[] = [];
let totalUncategorized = 0;
let totalProcessed = 0;
// Process in batches
while (hasMore) {
const res = await fetch('/api/categorize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ offset })
});
if (!res.ok) {
const error = await res.json();
alert(`Categorization failed: ${error.error || 'Unknown error'}`);
break;
}
const data = await res.json();
totalUncategorized = data.totalUncategorized;
totalProcessed = data.processed;
hasMore = data.hasMore;
offset = data.nextOffset || 0;
// Accumulate results
allResults = [...allResults, ...data.results];
// Update UI with progress
setCategorizationResults({
message: `Processing: ${totalProcessed} / ${totalUncategorized} songs...`,
totalProcessed: totalUncategorized,
totalCategorized: allResults.length,
results: allResults,
inProgress: hasMore
});
}
// Final update
setCategorizationResults({
message: `Completed! Processed ${totalUncategorized} songs, categorized ${allResults.length}`,
totalProcessed: totalUncategorized,
totalCategorized: allResults.length,
results: allResults,
inProgress: false
});
if (res.ok) {
const data = await res.json();
setCategorizationResults(data);
fetchSongs(); // Refresh song list
fetchGenres(); // Refresh genre counts
} else {
const error = await res.json();
alert(`Categorization failed: ${error.error || 'Unknown error'}`);
}
fetchSongs(); // Refresh song list
fetchGenres(); // Refresh genre counts
} catch (error) {
alert('Failed to categorize songs');
console.error(error);