refactor: streamline log clearing process and enhance error handling in DebugContext and DebugService
This commit is contained in:
@@ -51,7 +51,7 @@ function formatDuration(duration: number) {
|
|||||||
type FilterType = "all" | "current-page" | "api" | "cache" | "mongodb" | "page-render";
|
type FilterType = "all" | "current-page" | "api" | "cache" | "mongodb" | "page-render";
|
||||||
|
|
||||||
export function DebugInfo() {
|
export function DebugInfo() {
|
||||||
const { logs, setLogs, clearLogs: clearLogsContext, isRefreshing, setIsRefreshing } = useDebug();
|
const { logs, setLogs, clearLogs, isRefreshing, setIsRefreshing } = useDebug();
|
||||||
const [isMinimized, setIsMinimized] = useState(false);
|
const [isMinimized, setIsMinimized] = useState(false);
|
||||||
const [filter, setFilter] = useState<FilterType>("all");
|
const [filter, setFilter] = useState<FilterType>("all");
|
||||||
const [showFilters, setShowFilters] = useState(false);
|
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
|
// Fonction pour déterminer si une requête appartient à la page courante
|
||||||
const isCurrentPageRequest = (log: RequestTiming): boolean => {
|
const isCurrentPageRequest = (log: RequestTiming): boolean => {
|
||||||
if (log.pageRender) {
|
if (log.pageRender) {
|
||||||
|
|||||||
@@ -38,8 +38,17 @@ export function DebugProvider({ children }: DebugProviderProps) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const clearLogs = () => {
|
const clearLogs = async () => {
|
||||||
setLogs([]);
|
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
|
// Charger les logs au montage du provider et les rafraîchir périodiquement
|
||||||
|
|||||||
@@ -234,12 +234,54 @@ export class DebugService {
|
|||||||
try {
|
try {
|
||||||
const userId = await this.getCurrentUserId();
|
const userId = await this.getCurrentUserId();
|
||||||
const filePath = this.getLogFilePath(userId);
|
const filePath = this.getLogFilePath(userId);
|
||||||
await this.writeLogs(filePath, []);
|
await this.clearFile(filePath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AppError) throw error;
|
if (error instanceof AppError) throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async clearFile(filePath: string): Promise<void> {
|
||||||
|
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<void> {
|
||||||
|
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) {
|
static async logPageRender(page: string, duration: number) {
|
||||||
try {
|
try {
|
||||||
if (!(await this.isDebugEnabled())) return;
|
if (!(await this.isDebugEnabled())) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user