From 063d78fad5748f10943294605de27938aed86ea7 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 24 May 2025 18:51:59 +0100 Subject: [PATCH] Support more history entry types --- frontend/sync-client/src/index.ts | 7 +- .../sync-client/src/tracing/sync-history.ts | 81 ++++++++++++++----- 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/frontend/sync-client/src/index.ts b/frontend/sync-client/src/index.ts index 324312b5..7079f707 100644 --- a/frontend/sync-client/src/index.ts +++ b/frontend/sync-client/src/index.ts @@ -2,7 +2,12 @@ export { SyncType, SyncStatus, type HistoryStats, - type HistoryEntry + type HistoryEntry, + type SyncDetails, + type SyncCreateDetails, + type SyncUpdateDetails, + type SyncMovedDetails, + type SyncDeleteDetails } from "./tracing/sync-history"; export { Logger, LogLevel, LogLine } from "./tracing/logger"; export { type SyncSettings, DEFAULT_SETTINGS } from "./persistence/settings"; diff --git a/frontend/sync-client/src/tracing/sync-history.ts b/frontend/sync-client/src/tracing/sync-history.ts index 38f8320c..40a27ea8 100644 --- a/frontend/sync-client/src/tracing/sync-history.ts +++ b/frontend/sync-client/src/tracing/sync-history.ts @@ -1,17 +1,55 @@ import type { RelativePath } from "../persistence/database"; import type { Logger } from "./logger"; +export interface SyncCreateDetails { + type: SyncType.CREATE; + relativePath: RelativePath; + author?: string; +} + +export interface SyncUpdateDetails { + type: SyncType.UPDATE; + relativePath: RelativePath; + author?: string; +} + +export interface SyncMovedDetails { + type: SyncType.MOVE; + relativePath: RelativePath; + movedFrom: RelativePath; + author?: string; +} + +export interface SyncDeleteDetails { + type: SyncType.DELETE; + relativePath: RelativePath; + author?: string; +} + +export interface SyncSkippedDetails { + type: SyncType.SKIPPED; + relativePath: RelativePath; +} + +export type SyncDetails = + | SyncCreateDetails + | SyncUpdateDetails + | SyncDeleteDetails + | SyncMovedDetails + | SyncSkippedDetails; + export interface CommonHistoryEntry { status: SyncStatus; - relativePath: RelativePath; message: string; - type?: SyncType; + details: SyncDetails; } export enum SyncType { CREATE = "CREATE", UPDATE = "UPDATE", - DELETE = "DELETE" + DELETE = "DELETE", + MOVE = "MOVE", + SKIPPED = "SKIPPED" } export enum SyncStatus { @@ -28,8 +66,8 @@ export interface HistoryStats { } export class SyncHistory { - private static readonly MAX_ENTRIES = 500; - private static readonly TIMEOUT_FOR_MERGING_ENTRIES_IN_SECONDS = 15; + private static readonly MAX_ENTRIES = 5000; + private static readonly TIMEOUT_FOR_MERGING_ENTRIES_IN_SECONDS = 60; private _entries: HistoryEntry[] = []; @@ -60,7 +98,7 @@ export class SyncHistory { timestamp: new Date() }; - const candidate = this.findSimilarRecentEntry(historyEntry); + const candidate = this.findSimilarRecentUpdateEntry(historyEntry); if (candidate !== undefined) { this._entries = this._entries.filter((e) => e !== candidate); } @@ -93,11 +131,17 @@ export class SyncHistory { }); } - private findSimilarRecentEntry( + private findSimilarRecentUpdateEntry( entry: HistoryEntry ): HistoryEntry | undefined { + if (entry.details.type !== SyncType.UPDATE) { + return; + } + const candidate = this._entries.find( - (e) => e.relativePath === entry.relativePath + (e) => + e.details.type === SyncType.UPDATE && + e.details.relativePath === entry.details.relativePath ); if ( candidate !== undefined && @@ -111,17 +155,18 @@ export class SyncHistory { } private updateSuccessCount(entry: HistoryEntry): void { - if (entry.status === SyncStatus.SUCCESS) { - this.status.success++; - this.logger.info( - `History entry: ${entry.relativePath} - ${entry.message}` - ); - } else { - this.status.error++; - this.logger.error( - `Cannot sync file: ${entry.relativePath} - ${entry.message}` - ); + const message = `${entry.details.relativePath} - ${entry.message} (${entry.details.type.toLocaleLowerCase()})`; + switch (entry.status) { + case SyncStatus.SUCCESS: + this.status.success++; + this.logger.info(`History entry: ${message}`); + break; + case SyncStatus.ERROR: + this.status.error++; + this.logger.error(`Cannot sync file: ${message}`); + break; } + this.syncHistoryUpdateListeners.forEach((listener) => { listener(this.status); });