From 7da4439b8c92b61a7cf7404231ed9ffc221a433f Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Tue, 7 Oct 2025 21:12:10 +0200 Subject: [PATCH] refactor: streamline log clearing process and enhance error handling in DebugContext and DebugService --- src/components/debug/DebugInfo.tsx | 11 +------- src/contexts/DebugContext.tsx | 13 +++++++-- src/lib/services/debug.service.ts | 44 +++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/components/debug/DebugInfo.tsx b/src/components/debug/DebugInfo.tsx index 00e1356..caadf68 100644 --- a/src/components/debug/DebugInfo.tsx +++ b/src/components/debug/DebugInfo.tsx @@ -51,7 +51,7 @@ function formatDuration(duration: number) { type FilterType = "all" | "current-page" | "api" | "cache" | "mongodb" | "page-render"; export function DebugInfo() { - const { logs, setLogs, clearLogs: clearLogsContext, isRefreshing, setIsRefreshing } = useDebug(); + const { logs, setLogs, clearLogs, isRefreshing, setIsRefreshing } = useDebug(); const [isMinimized, setIsMinimized] = useState(false); const [filter, setFilter] = useState("all"); const [showFilters, setShowFilters] = useState(false); @@ -73,15 +73,6 @@ export function DebugInfo() { } }; - const clearLogs = async () => { - try { - await fetch("/api/debug", { method: "DELETE" }); - clearLogsContext(); - } catch (error) { - console.error("Erreur lors de la suppression des logs:", error); - } - }; - // Fonction pour déterminer si une requête appartient à la page courante const isCurrentPageRequest = (log: RequestTiming): boolean => { if (log.pageRender) { diff --git a/src/contexts/DebugContext.tsx b/src/contexts/DebugContext.tsx index 0ec8625..93617f8 100644 --- a/src/contexts/DebugContext.tsx +++ b/src/contexts/DebugContext.tsx @@ -38,8 +38,17 @@ export function DebugProvider({ children }: DebugProviderProps) { }); }; - const clearLogs = () => { - setLogs([]); + const clearLogs = async () => { + try { + // Vider le fichier côté serveur + await fetch("/api/debug", { method: "DELETE" }); + // Vider le state côté client + setLogs([]); + } catch (error) { + console.error("Erreur lors de la suppression des logs:", error); + // Même en cas d'erreur, vider le state côté client + setLogs([]); + } }; // Charger les logs au montage du provider et les rafraîchir périodiquement diff --git a/src/lib/services/debug.service.ts b/src/lib/services/debug.service.ts index 1ab158d..6feab14 100644 --- a/src/lib/services/debug.service.ts +++ b/src/lib/services/debug.service.ts @@ -234,12 +234,54 @@ export class DebugService { try { const userId = await this.getCurrentUserId(); const filePath = this.getLogFilePath(userId); - await this.writeLogs(filePath, []); + await this.clearFile(filePath); } catch (error) { if (error instanceof AppError) throw error; } } + private static async clearFile(filePath: string): Promise { + try { + // Obtenir la queue existante ou créer une nouvelle + const existingQueue = this.writeQueues.get(filePath); + + // Créer une nouvelle promesse qui attend la queue précédente + const newQueue = existingQueue + ? existingQueue.then(() => this.performClear(filePath)) + : this.performClear(filePath); + + // Mettre à jour la queue + this.writeQueues.set(filePath, newQueue); + + try { + await newQueue; + } finally { + // Nettoyer la queue si c'est la dernière opération + if (this.writeQueues.get(filePath) === newQueue) { + this.writeQueues.delete(filePath); + } + } + } catch (error) { + console.error(`Erreur lors du vidage du fichier ${filePath}:`, error); + } + } + + private static async performClear(filePath: string): Promise { + try { + // Créer une sauvegarde avant de vider + try { + await fs.copyFile(filePath, filePath + '.backup'); + } catch { + // Ignorer si le fichier n'existe pas encore + } + + // Écrire un tableau vide pour vider le fichier + await fs.writeFile(filePath, JSON.stringify([], null, 2), { flag: 'w' }); + } catch (error) { + console.error(`Erreur lors du vidage du fichier ${filePath}:`, error); + } + } + static async logPageRender(page: string, duration: number) { try { if (!(await this.isDebugEnabled())) return;