Support more history entry types

This commit is contained in:
Andras Schmelczer 2025-05-24 18:51:59 +01:00
commit 063d78fad5
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 69 additions and 19 deletions

View file

@ -2,7 +2,12 @@ export {
SyncType, SyncType,
SyncStatus, SyncStatus,
type HistoryStats, type HistoryStats,
type HistoryEntry type HistoryEntry,
type SyncDetails,
type SyncCreateDetails,
type SyncUpdateDetails,
type SyncMovedDetails,
type SyncDeleteDetails
} from "./tracing/sync-history"; } from "./tracing/sync-history";
export { Logger, LogLevel, LogLine } from "./tracing/logger"; export { Logger, LogLevel, LogLine } from "./tracing/logger";
export { type SyncSettings, DEFAULT_SETTINGS } from "./persistence/settings"; export { type SyncSettings, DEFAULT_SETTINGS } from "./persistence/settings";

View file

@ -1,17 +1,55 @@
import type { RelativePath } from "../persistence/database"; import type { RelativePath } from "../persistence/database";
import type { Logger } from "./logger"; 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 { export interface CommonHistoryEntry {
status: SyncStatus; status: SyncStatus;
relativePath: RelativePath;
message: string; message: string;
type?: SyncType; details: SyncDetails;
} }
export enum SyncType { export enum SyncType {
CREATE = "CREATE", CREATE = "CREATE",
UPDATE = "UPDATE", UPDATE = "UPDATE",
DELETE = "DELETE" DELETE = "DELETE",
MOVE = "MOVE",
SKIPPED = "SKIPPED"
} }
export enum SyncStatus { export enum SyncStatus {
@ -28,8 +66,8 @@ export interface HistoryStats {
} }
export class SyncHistory { export class SyncHistory {
private static readonly MAX_ENTRIES = 500; private static readonly MAX_ENTRIES = 5000;
private static readonly TIMEOUT_FOR_MERGING_ENTRIES_IN_SECONDS = 15; private static readonly TIMEOUT_FOR_MERGING_ENTRIES_IN_SECONDS = 60;
private _entries: HistoryEntry[] = []; private _entries: HistoryEntry[] = [];
@ -60,7 +98,7 @@ export class SyncHistory {
timestamp: new Date() timestamp: new Date()
}; };
const candidate = this.findSimilarRecentEntry(historyEntry); const candidate = this.findSimilarRecentUpdateEntry(historyEntry);
if (candidate !== undefined) { if (candidate !== undefined) {
this._entries = this._entries.filter((e) => e !== candidate); this._entries = this._entries.filter((e) => e !== candidate);
} }
@ -93,11 +131,17 @@ export class SyncHistory {
}); });
} }
private findSimilarRecentEntry( private findSimilarRecentUpdateEntry(
entry: HistoryEntry entry: HistoryEntry
): HistoryEntry | undefined { ): HistoryEntry | undefined {
if (entry.details.type !== SyncType.UPDATE) {
return;
}
const candidate = this._entries.find( const candidate = this._entries.find(
(e) => e.relativePath === entry.relativePath (e) =>
e.details.type === SyncType.UPDATE &&
e.details.relativePath === entry.details.relativePath
); );
if ( if (
candidate !== undefined && candidate !== undefined &&
@ -111,17 +155,18 @@ export class SyncHistory {
} }
private updateSuccessCount(entry: HistoryEntry): void { private updateSuccessCount(entry: HistoryEntry): void {
if (entry.status === SyncStatus.SUCCESS) { const message = `${entry.details.relativePath} - ${entry.message} (${entry.details.type.toLocaleLowerCase()})`;
switch (entry.status) {
case SyncStatus.SUCCESS:
this.status.success++; this.status.success++;
this.logger.info( this.logger.info(`History entry: ${message}`);
`History entry: ${entry.relativePath} - ${entry.message}` break;
); case SyncStatus.ERROR:
} else {
this.status.error++; this.status.error++;
this.logger.error( this.logger.error(`Cannot sync file: ${message}`);
`Cannot sync file: ${entry.relativePath} - ${entry.message}` break;
);
} }
this.syncHistoryUpdateListeners.forEach((listener) => { this.syncHistoryUpdateListeners.forEach((listener) => {
listener(this.status); listener(this.status);
}); });