reconcile/frontend/sync-client/src/tracing/sync-history.ts
Andras Schmelczer 8b8f1d91d9
Fix syncing when network latency is present (#4)
* WIP

* Add debug

* Dedupe inserts

* Add deterministic ordering

* Fix whitespaces

* Update insta

* Add integration test script

* Rename

* Add test

* Working for non-deletes

* omg it mostly works for deletes

* Isdeleted fix

* remove created dates

* update api

* Take document id

* No max attempt

* works

* Use string uuids

* .

* working!!!! (hopefully)

* Improve bundling

* Add module

* lint

* .

* lint

* Fix CI

* use toolchain

* clean up

* Add useSlowFileEvents

* Delete fuzz

* Fix CI

* use docker

* fix script

* clean up

* Clean up

* change node version

* Build docker image on every commit

* fix ci

* 1 db per vault

* Add scritps folder

* Bump versions

* Lint

* .

* Fix tests for real

* Style

* .

* try

* Consistent ordering

* Fix tests

* hmm

* .

* Clean up diff

* Fixes

* .

* Fix version bump

* .

* .

* .
2025-03-16 20:13:49 +00:00

100 lines
2 KiB
TypeScript

import type { RelativePath } from "../persistence/database";
import type { Logger } from "./logger";
export interface CommonHistoryEntry {
status: SyncStatus;
relativePath: RelativePath;
message: string;
type?: SyncType;
source?: SyncSource;
}
export enum SyncType {
CREATE = "CREATE",
UPDATE = "UPDATE",
DELETE = "DELETE"
}
export enum SyncSource {
PUSH = "PUSH",
PULL = "PULL"
}
export enum SyncStatus {
SUCCESS = "SUCCESS",
ERROR = "ERROR"
}
export type HistoryEntry = CommonHistoryEntry & { timestamp: Date };
export interface HistoryStats {
success: number;
error: number;
}
export class SyncHistory {
private static readonly MAX_ENTRIES = 5000;
private readonly entries: HistoryEntry[] = [];
private readonly syncHistoryUpdateListeners: ((
status: HistoryStats
) => void)[] = [];
private status: HistoryStats = {
success: 0,
error: 0
};
public constructor(private readonly logger: Logger) {}
public getEntries(): HistoryEntry[] {
return [...this.entries];
}
public reset(): void {
this.entries.length = 0;
this.status = {
success: 0,
error: 0
};
this.syncHistoryUpdateListeners.forEach((listener) => {
listener(this.status);
});
}
public addSyncHistoryUpdateListener(
listener: (stats: HistoryStats) => void
): void {
this.syncHistoryUpdateListeners.push(listener);
listener({ ...this.status });
}
public addHistoryEntry(entry: CommonHistoryEntry): void {
const historyEntry = {
...entry,
timestamp: new Date()
};
this.entries.push(historyEntry);
if (entry.status === SyncStatus.SUCCESS) {
this.status.success++;
this.logger.info(
`History entry: ${entry.relativePath} - ${entry.message}`
);
} else {
this.status.error++;
this.logger.error(
`Error syncing file: ${entry.relativePath} - ${entry.message}`
);
}
this.syncHistoryUpdateListeners.forEach((listener) => {
listener(this.status);
});
if (this.entries.length > SyncHistory.MAX_ENTRIES) {
this.entries.shift();
}
}
}