@terrafirmagreg-team/data 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/dump-status.ts +91 -0
package/package.json
CHANGED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export type DumpDatasetState = {
|
|
2
|
+
status: 'ok' | 'failed';
|
|
3
|
+
fails: number;
|
|
4
|
+
lastFail: number;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type DumpStatusSnapshot = {
|
|
8
|
+
statuses: Record<string, DumpDatasetState>;
|
|
9
|
+
retryNonce: number;
|
|
10
|
+
dismissed: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type Store = {
|
|
14
|
+
state: Map<string, DumpDatasetState>;
|
|
15
|
+
listeners: Set<() => void>;
|
|
16
|
+
retryNonce: number;
|
|
17
|
+
dismissedSignature: string;
|
|
18
|
+
snapshot: DumpStatusSnapshot;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const store = ((globalThis as Record<symbol, unknown>)[
|
|
22
|
+
Symbol.for('terrafirmagreg.dump-status')
|
|
23
|
+
] ??= {
|
|
24
|
+
state: new Map(),
|
|
25
|
+
listeners: new Set(),
|
|
26
|
+
retryNonce: 0,
|
|
27
|
+
dismissedSignature: '',
|
|
28
|
+
snapshot: { statuses: {}, retryNonce: 0, dismissed: false },
|
|
29
|
+
}) as Store;
|
|
30
|
+
|
|
31
|
+
function notify() {
|
|
32
|
+
store.snapshot = {
|
|
33
|
+
statuses: Object.fromEntries(store.state),
|
|
34
|
+
retryNonce: store.retryNonce,
|
|
35
|
+
dismissed: dumpFailuresDismissed(),
|
|
36
|
+
};
|
|
37
|
+
for (const listener of store.listeners) listener();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function markDumpResult(id: string, ok: boolean) {
|
|
41
|
+
const prev = store.state.get(id);
|
|
42
|
+
if (ok) {
|
|
43
|
+
if (!prev || prev.status === 'failed') {
|
|
44
|
+
store.state.set(id, { status: 'ok', fails: 0, lastFail: 0 });
|
|
45
|
+
if (prev?.status === 'failed') store.retryNonce += 1;
|
|
46
|
+
notify();
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const fails = (prev?.fails ?? 0) + 1;
|
|
51
|
+
store.state.set(id, { status: 'failed', fails, lastFail: Date.now() });
|
|
52
|
+
notify();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function dumpStatusSnapshot(): DumpStatusSnapshot {
|
|
56
|
+
return store.snapshot;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function subscribeDumpStatus(listener: () => void): () => void {
|
|
60
|
+
store.listeners.add(listener);
|
|
61
|
+
return () => store.listeners.delete(listener);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function requestDumpRetry() {
|
|
65
|
+
store.retryNonce += 1;
|
|
66
|
+
notify();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function dumpRetryDelay(fails: number): number {
|
|
70
|
+
return fails <= 1 ? 10_000 : fails === 2 ? 30_000 : 60_000;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function dumpFailureSignature(): string {
|
|
74
|
+
const failed = [...store.state.entries()].filter(
|
|
75
|
+
([, state]) => state.status === 'failed',
|
|
76
|
+
);
|
|
77
|
+
if (!failed.length) return '';
|
|
78
|
+
const ids = failed.map(([id]) => id).sort().join(',');
|
|
79
|
+
const fails = failed.reduce((sum, [, state]) => sum + state.fails, 0);
|
|
80
|
+
return `${ids}#${fails}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function dismissDumpFailures() {
|
|
84
|
+
store.dismissedSignature = dumpFailureSignature();
|
|
85
|
+
notify();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function dumpFailuresDismissed(): boolean {
|
|
89
|
+
const signature = dumpFailureSignature();
|
|
90
|
+
return signature !== '' && signature === store.dismissedSignature;
|
|
91
|
+
}
|