Add more debug info

This commit is contained in:
2025-08-26 08:40:46 +02:00
parent 9dd2f02633
commit 9ffb3b85fa

View File

@@ -20,19 +20,6 @@ const debugTiming = (operation: string, startTime: number) => {
return duration; return duration;
}; };
// Enhanced fetch with timing
const fetchWithTiming = async (url: string, options: RequestInit, operation: string) => {
const startTime = Date.now();
try {
const response = await fetch(url, options);
debugTiming(operation, startTime);
return response;
} catch (error) {
debugTiming(`${operation} (FAILED)`, startTime);
throw error;
}
};
interface Note { interface Note {
id: string; id: string;
epochTime: number; epochTime: number;
@@ -105,6 +92,29 @@ const Index = () => {
setDebugInfo(prev => [...prev.slice(-19), debugMessage]); // Keep last 20 messages setDebugInfo(prev => [...prev.slice(-19), debugMessage]); // Keep last 20 messages
}; };
// Enhanced fetch with timing
const fetchWithTiming = async (url: string, options: RequestInit, operation: string) => {
const startTime = Date.now();
try {
addDebugInfo(`Starting ${operation}...`);
const response = await fetch(url, options);
const duration = Date.now() - startTime;
debugTiming(operation, startTime);
// Add network diagnostics for slow requests
if (duration > 1000) {
addDebugInfo(`Slow request: ${operation} took ${duration}ms`);
}
return response;
} catch (error) {
const duration = Date.now() - startTime;
debugTiming(`${operation} (FAILED)`, startTime);
addDebugInfo(`Network error: ${operation} failed after ${duration}ms`);
throw error;
}
};
const previousNoteRef = useRef<HTMLDivElement>(null); const previousNoteRef = useRef<HTMLDivElement>(null);
const currentNoteRef = useRef<HTMLTextAreaElement>(null); const currentNoteRef = useRef<HTMLTextAreaElement>(null);
const scratchRef = useRef<HTMLTextAreaElement>(null); const scratchRef = useRef<HTMLTextAreaElement>(null);
@@ -447,7 +457,7 @@ const Index = () => {
setIsLoading(true); setIsLoading(true);
if (offset === 0) { if (offset === 0) {
setCacheMode('global'); setCacheMode('global');
addDebugInfo(`📝 Loading notes (offset: ${offset}, limit: ${limit})`); addDebugInfo(`Loading notes (offset: ${offset}, limit: ${limit})`);
} }
const response = await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { const response = await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, {
method: 'POST', method: 'POST',
@@ -477,16 +487,16 @@ const Index = () => {
setCurrentNoteIndex(0); setCurrentNoteIndex(0);
} }
const loadTime = Date.now() - loadStartTime; const loadTime = Date.now() - loadStartTime;
addDebugInfo(`Loaded ${notes.length} notes in ${loadTime}ms`); addDebugInfo(`Loaded ${notes.length} notes in ${loadTime}ms`);
} else { } else {
setNoteCache(prev => [...prev, ...notes]); setNoteCache(prev => [...prev, ...notes]);
const loadTime = Date.now() - loadStartTime; const loadTime = Date.now() - loadStartTime;
addDebugInfo(`Loaded ${notes.length} additional notes in ${loadTime}ms`); addDebugInfo(`Loaded ${notes.length} additional notes in ${loadTime}ms`);
} }
} catch (error) { } catch (error) {
console.error('Error loading notes:', error); console.error('Error loading notes:', error);
const loadTime = Date.now() - loadStartTime; const loadTime = Date.now() - loadStartTime;
addDebugInfo(`Failed to load notes after ${loadTime}ms`); addDebugInfo(`Failed to load notes after ${loadTime}ms`);
toast({ toast({
title: "Error", title: "Error",
description: "Failed to load notes. Please check your connection.", description: "Failed to load notes. Please check your connection.",
@@ -605,8 +615,8 @@ const Index = () => {
} }
const searchStartTime = Date.now(); const searchStartTime = Date.now();
console.log(`🔍 Starting search for: "${query}"`); console.log(`Starting search for: "${query}"`);
addDebugInfo(`🔍 Searching for: "${query}"`); addDebugInfo(`Searching for: "${query}"`);
try { try {
const response = await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { const response = await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, {
@@ -640,13 +650,13 @@ const Index = () => {
setSearchResults(results); setSearchResults(results);
const searchTime = Date.now() - searchStartTime; const searchTime = Date.now() - searchStartTime;
debugTiming(`Search Results (${results.length} found)`, searchStartTime); debugTiming(`Search Results (${results.length} found)`, searchStartTime);
addDebugInfo(`Search complete: ${results.length} results in ${searchTime}ms`); addDebugInfo(`Search complete: ${results.length} results in ${searchTime}ms`);
console.log(`Search complete: ${results.length} results found`); console.log(`Search complete: ${results.length} results found`);
} catch (error) { } catch (error) {
console.error('Error searching notes:', error); console.error('Error searching notes:', error);
const searchTime = Date.now() - searchStartTime; const searchTime = Date.now() - searchStartTime;
debugTiming('Search (FAILED)', searchStartTime); debugTiming('Search (FAILED)', searchStartTime);
addDebugInfo(`Search failed after ${searchTime}ms`); addDebugInfo(`Search failed after ${searchTime}ms`);
toast({ toast({
title: "Error", title: "Error",
description: "Failed to search notes. Please try again.", description: "Failed to search notes. Please try again.",
@@ -890,7 +900,7 @@ const Index = () => {
// Only restore focus if no modal is open and the current note field exists // Only restore focus if no modal is open and the current note field exists
if (!isSearchOpen && !isGotoOpen && !isCleanupOpen && currentNoteRef.current) { if (!isSearchOpen && !isGotoOpen && !isCleanupOpen && currentNoteRef.current) {
currentNoteRef.current.focus(); currentNoteRef.current.focus();
console.log('🎯 Focus restored on window focus'); console.log('Focus restored on window focus');
} }
}; };
@@ -1017,15 +1027,20 @@ const Index = () => {
// Check if an index exists // Check if an index exists
const indexExists = async (index: string): Promise<boolean> => { const indexExists = async (index: string): Promise<boolean> => {
const checkStart = Date.now();
try { try {
addDebugInfo(`Checking if ${index} index exists...`);
const response = await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${index}`, { const response = await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${index}`, {
headers: { headers: {
'Authorization': `Bearer ${MEILISEARCH_API_KEY}`, 'Authorization': `Bearer ${MEILISEARCH_API_KEY}`,
}, },
}, `Check Index ${index}`); }, `Check Index ${index}`);
return response.status === 200; const exists = response.status === 200;
addDebugInfo(`${index} index ${exists ? 'exists' : 'does not exist'}: ${Date.now() - checkStart}ms`);
return exists;
} catch (error) { } catch (error) {
console.error(`Error checking if index ${index} exists:`, error); console.error(`Error checking if index ${index} exists:`, error);
addDebugInfo(`Failed to check ${index} index: ${Date.now() - checkStart}ms`);
return false; return false;
} }
}; };
@@ -1034,12 +1049,13 @@ const Index = () => {
const initializeIndexes = async () => { const initializeIndexes = async () => {
const initStartTime = Date.now(); const initStartTime = Date.now();
try { try {
console.log('🚀 Starting initialization...'); console.log('Starting initialization...');
addDebugInfo('🚀 Starting initialization...'); addDebugInfo('Starting initialization...');
// Initialize notes index // Initialize notes index
if (!(await indexExists(NOTE_INDEX))) { if (!(await indexExists(NOTE_INDEX))) {
const indexStart = Date.now(); const indexStart = Date.now();
addDebugInfo('Creating notes index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -1051,10 +1067,14 @@ const Index = () => {
primaryKey: 'id', primaryKey: 'id',
}), }),
}, 'Initialize Notes Index'); }, 'Initialize Notes Index');
addDebugInfo(`⏱️ Initialize Notes Index: ${Date.now() - indexStart}ms`); addDebugInfo(`Notes index created: ${Date.now() - indexStart}ms`);
} else {
addDebugInfo('Notes index already exists');
} }
// Configure notes index settings // Configure notes index settings
addDebugInfo('Configuring notes sortable attributes...');
const sortableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/sortable-attributes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/sortable-attributes`, {
method: 'PUT', method: 'PUT',
headers: { headers: {
@@ -1063,8 +1083,11 @@ const Index = () => {
}, },
body: JSON.stringify(['date']), body: JSON.stringify(['date']),
}, 'Configure Notes Sortable Attributes'); }, 'Configure Notes Sortable Attributes');
addDebugInfo(`Sortable attributes configured: ${Date.now() - sortableStart}ms`);
// Set ranking rules to prioritize sorting // Set ranking rules to prioritize sorting
addDebugInfo('Configuring notes ranking rules...');
const rankingStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/ranking-rules`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/ranking-rules`, {
method: 'PUT', method: 'PUT',
headers: { headers: {
@@ -1073,7 +1096,10 @@ const Index = () => {
}, },
body: JSON.stringify(['sort', 'words', 'typo', 'proximity', 'attribute', 'exactness']), body: JSON.stringify(['sort', 'words', 'typo', 'proximity', 'attribute', 'exactness']),
}, 'Configure Notes Ranking Rules'); }, 'Configure Notes Ranking Rules');
addDebugInfo(`Ranking rules configured: ${Date.now() - rankingStart}ms`);
addDebugInfo('Configuring notes filterable attributes...');
const filterableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/filterable-attributes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/settings/filterable-attributes`, {
method: 'PUT', method: 'PUT',
headers: { headers: {
@@ -1082,9 +1108,12 @@ const Index = () => {
}, },
body: JSON.stringify(['date', 'topLetter', 'letterCount', 'topLetterFrequency']), body: JSON.stringify(['date', 'topLetter', 'letterCount', 'topLetterFrequency']),
}, 'Configure Notes Filterable Attributes'); }, 'Configure Notes Filterable Attributes');
addDebugInfo(`Filterable attributes configured: ${Date.now() - filterableStart}ms`);
// Initialize scratch index // Initialize scratch index
if (!(await indexExists(SCRATCH_INDEX))) { if (!(await indexExists(SCRATCH_INDEX))) {
const scratchStart = Date.now();
addDebugInfo('Creating scratch index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -1096,9 +1125,14 @@ const Index = () => {
primaryKey: 'id', primaryKey: 'id',
}), }),
}, 'Initialize Scratch Index'); }, 'Initialize Scratch Index');
addDebugInfo(`Scratch index created: ${Date.now() - scratchStart}ms`);
} else {
addDebugInfo('Scratch index already exists');
} }
// Configure scratch index settings // Configure scratch index settings
addDebugInfo('Configuring scratch sortable attributes...');
const scratchSortableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/sortable-attributes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/sortable-attributes`, {
method: 'PUT', method: 'PUT',
headers: { headers: {
@@ -1107,7 +1141,10 @@ const Index = () => {
}, },
body: JSON.stringify(['date']), body: JSON.stringify(['date']),
}, 'Configure Scratch Sortable Attributes'); }, 'Configure Scratch Sortable Attributes');
addDebugInfo(`Scratch sortable attributes configured: ${Date.now() - scratchSortableStart}ms`);
addDebugInfo('Configuring scratch filterable attributes...');
const scratchFilterableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/filterable-attributes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SCRATCH_INDEX}/settings/filterable-attributes`, {
method: 'PUT', method: 'PUT',
headers: { headers: {
@@ -1116,9 +1153,12 @@ const Index = () => {
}, },
body: JSON.stringify(['date']), body: JSON.stringify(['date']),
}, 'Configure Scratch Filterable Attributes'); }, 'Configure Scratch Filterable Attributes');
addDebugInfo(`Scratch filterable attributes configured: ${Date.now() - scratchFilterableStart}ms`);
// Initialize settings index // Initialize settings index
if (!(await indexExists(SETTINGS_INDEX))) { if (!(await indexExists(SETTINGS_INDEX))) {
const settingsStart = Date.now();
addDebugInfo('Creating settings index...');
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -1130,9 +1170,14 @@ const Index = () => {
primaryKey: 'key', primaryKey: 'key',
}), }),
}, 'Initialize Settings Index'); }, 'Initialize Settings Index');
addDebugInfo(`Settings index created: ${Date.now() - settingsStart}ms`);
} else {
addDebugInfo('Settings index already exists');
} }
// Configure settings index // Configure settings index
addDebugInfo('Configuring settings filterable attributes...');
const settingsFilterableStart = Date.now();
await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SETTINGS_INDEX}/settings/filterable-attributes`, { await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${SETTINGS_INDEX}/settings/filterable-attributes`, {
method: 'PUT', method: 'PUT',
headers: { headers: {
@@ -1141,12 +1186,13 @@ const Index = () => {
}, },
body: JSON.stringify(['key', 'value']), body: JSON.stringify(['key', 'value']),
}, 'Configure Settings Filterable Attributes'); }, 'Configure Settings Filterable Attributes');
addDebugInfo(`Settings filterable attributes configured: ${Date.now() - settingsFilterableStart}ms`);
setIsInitialized(true); setIsInitialized(true);
const totalTime = Date.now() - initStartTime; const totalTime = Date.now() - initStartTime;
debugTiming('Total Initialization', initStartTime); debugTiming('Total Initialization', initStartTime);
addDebugInfo(`Total Initialization: ${totalTime}ms`); addDebugInfo(`Total Initialization: ${totalTime}ms`);
console.log('Initialization complete'); console.log('Initialization complete');
toast({ toast({
title: "Initialization complete", title: "Initialization complete",
description: "All indexes have been configured successfully.", description: "All indexes have been configured successfully.",
@@ -1155,7 +1201,7 @@ const Index = () => {
console.error('Error initializing indexes:', error); console.error('Error initializing indexes:', error);
const totalTime = Date.now() - initStartTime; const totalTime = Date.now() - initStartTime;
debugTiming('Initialization (FAILED)', initStartTime); debugTiming('Initialization (FAILED)', initStartTime);
addDebugInfo(`Initialization FAILED: ${totalTime}ms`); addDebugInfo(`Initialization FAILED: ${totalTime}ms`);
toast({ toast({
title: "Initialization failed", title: "Initialization failed",
description: "Failed to initialize indexes. Please check your connection.", description: "Failed to initialize indexes. Please check your connection.",
@@ -1169,7 +1215,7 @@ const Index = () => {
// Focus the current note field immediately with a small delay to ensure DOM is ready // Focus the current note field immediately with a small delay to ensure DOM is ready
const focusTimer = setTimeout(() => { const focusTimer = setTimeout(() => {
currentNoteRef.current?.focus(); currentNoteRef.current?.focus();
console.log('🎯 Focus set on current note field'); console.log('Focus set on current note field');
}, 100); }, 100);
return () => clearTimeout(focusTimer); return () => clearTimeout(focusTimer);
@@ -1186,8 +1232,8 @@ const Index = () => {
const loadInitialData = async () => { const loadInitialData = async () => {
if (isInitialized) { if (isInitialized) {
const dataLoadStartTime = Date.now(); const dataLoadStartTime = Date.now();
console.log('📊 Starting data loading...'); console.log('Starting data loading...');
addDebugInfo('📊 Starting data loading...'); addDebugInfo('Starting data loading...');
await loadNotes(); await loadNotes();
await loadLatestScratch(); await loadLatestScratch();
@@ -1195,8 +1241,8 @@ const Index = () => {
const totalTime = Date.now() - dataLoadStartTime; const totalTime = Date.now() - dataLoadStartTime;
debugTiming('Total Data Loading', dataLoadStartTime); debugTiming('Total Data Loading', dataLoadStartTime);
addDebugInfo(`Total Data Loading: ${totalTime}ms`); addDebugInfo(`Total Data Loading: ${totalTime}ms`);
console.log('Data loading complete'); console.log('Data loading complete');
// Re-focus after data loading to ensure focus is maintained // Re-focus after data loading to ensure focus is maintained
currentNoteRef.current?.focus(); currentNoteRef.current?.focus();