Fix: Add batch processing to prevent timeout on large song libraries
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user