-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproject-sync-status.ts
More file actions
251 lines (221 loc) · 6.92 KB
/
Copy pathproject-sync-status.ts
File metadata and controls
251 lines (221 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import type { RemoteStatusResponse } from './types';
export type ProjectSyncState =
| 'unconfigured'
| 'clean'
| 'unavailable'
| 'behind'
| 'ahead'
| 'dirty'
| 'conflicted'
| 'syncing';
export type ProjectSyncTone = 'neutral' | 'good' | 'info' | 'warn' | 'danger';
export interface ProjectSyncView {
state: ProjectSyncState;
label: string;
tone: ProjectSyncTone;
summary: string;
nextAction?: string;
canSync: boolean;
}
function formatTimestamp(timestamp?: string): string | undefined {
if (!timestamp) {
return undefined;
}
const parsed = new Date(timestamp);
if (Number.isNaN(parsed.getTime())) {
return undefined;
}
return parsed.toLocaleString();
}
export function formatLastSynced(timestamp?: string): string {
const formatted = formatTimestamp(timestamp);
return formatted ? `Last synced ${formatted}` : 'Never synced';
}
export function formatRemoteRunCount(count?: number): string {
if (typeof count !== 'number' || !Number.isFinite(count)) {
return 'Remote runs unknown';
}
return `${count} remote run${count === 1 ? '' : 's'}`;
}
export function buildRemoteStatusItems(
status: RemoteStatusResponse | undefined,
projectName?: string,
): string[] {
if (status?.configured !== true) {
return [];
}
return [
projectName ? `Project: ${projectName}` : undefined,
formatRemoteRunCount(status.run_count),
formatLastSynced(status.last_synced_at),
status.repo ? `Repo: ${status.repo}` : undefined,
].filter((item): item is string => item !== undefined);
}
export function getProjectSyncView(
status: RemoteStatusResponse | undefined,
syncInFlight = false,
): ProjectSyncView {
if (syncInFlight || status?.sync_status === 'syncing') {
return {
state: 'syncing',
label: 'Syncing',
tone: 'info',
summary: 'Sync is in progress.',
canSync: false,
};
}
if (status?.configured !== true) {
return {
state: 'unconfigured',
label: 'Not configured',
tone: 'neutral',
summary: 'Remote results are not configured for this project.',
canSync: false,
};
}
if (status.available !== true || status.sync_status === 'unavailable') {
return {
state: 'unavailable',
label: 'Unavailable',
tone: 'warn',
summary: 'The remote results cache is not available locally.',
nextAction: 'Sync Project can clone or refresh the configured results repo.',
canSync: true,
};
}
const state = status.sync_status ?? 'clean';
if (state === 'conflicted' || state === 'diverged') {
return {
state: 'conflicted',
label: state === 'diverged' ? 'Conflicted' : 'Conflicted',
tone: 'danger',
summary:
status.block_reason ??
(state === 'diverged'
? 'Local and remote results histories have diverged.'
: 'The results repo has unresolved conflicts.'),
nextAction: 'Resolve the results repo conflicts manually, then sync the project again.',
canSync: false,
};
}
if (state === 'dirty') {
return {
state: 'dirty',
label: 'Dirty',
tone: 'warn',
summary: status.block_reason ?? 'Local result metadata has pending edits.',
nextAction:
status.auto_push === true
? 'Sync Project will commit safe result metadata changes before syncing.'
: 'Review or commit the pending result metadata; no reset will be performed.',
canSync: true,
};
}
if (state === 'behind') {
return {
state: 'behind',
label: 'Behind',
tone: 'info',
summary: `Remote has ${status.behind ?? 0} commit${status.behind === 1 ? '' : 's'} to pull.`,
nextAction: 'Sync Project will fast-forward when possible.',
canSync: true,
};
}
if (state === 'ahead') {
return {
state: 'ahead',
label: 'Ahead',
tone: 'info',
summary: `Local results are ${status.ahead ?? 0} commit${status.ahead === 1 ? '' : 's'} ahead.`,
nextAction:
status.auto_push === true
? 'Sync Project will push safe result changes.'
: 'Enable auto_push or push the results repo manually.',
canSync: true,
};
}
return {
state: 'clean',
label: 'Clean',
tone: 'good',
summary: 'Local and remote result metadata are in sync.',
canSync: true,
};
}
function formatSyncOutcomeRuns(count?: number): string {
if (typeof count !== 'number' || !Number.isFinite(count)) {
return 'remote results';
}
return formatRemoteRunCount(count);
}
function buildSyncOutcomeSentence(status: RemoteStatusResponse): string {
const parts = [`Synced ${formatSyncOutcomeRuns(status.run_count)}`];
if (status.repo) {
parts.push(`from ${status.repo}`);
}
const syncedAt = formatTimestamp(status.last_synced_at);
if (syncedAt) {
parts.push(`at ${syncedAt}`);
}
return `${parts.join(' ')}.`;
}
function buildCachedRemoteAvailability(
status: RemoteStatusResponse | undefined,
): string | undefined {
if (status?.available !== true) {
return undefined;
}
const runCount = status.run_count;
if (typeof runCount === 'number' && Number.isFinite(runCount) && runCount > 0) {
return `Cached ${formatRemoteRunCount(runCount)} ${
runCount === 1 ? 'remains' : 'remain'
} available.`;
}
return 'The remote results cache remains available.';
}
export function buildRemoteErrorAction(status: RemoteStatusResponse | undefined): string {
return [
buildCachedRemoteAvailability(status),
'Resolve the results repo issue, then sync remote results again.',
]
.filter((part): part is string => part !== undefined)
.join(' ');
}
export function buildProjectSyncFeedback(status: RemoteStatusResponse): {
kind: 'success' | 'warning';
message: string;
} {
if (status.blocked || status.sync_status === 'conflicted' || status.sync_status === 'diverged') {
const repo = status.repo ? ` for ${status.repo}` : '';
const reason = status.block_reason ?? 'Sync stopped before changing the results repo.';
return {
kind: 'warning',
message: `Sync stopped${repo}: ${reason}. ${buildRemoteErrorAction(status)}`,
};
}
const actions = [
status.commit_created ? 'committed pending metadata' : undefined,
status.pull_performed ? 'pulled remote results' : undefined,
status.push_performed ? 'pushed local results' : undefined,
].filter((action): action is string => action !== undefined);
return {
kind: 'success',
message:
actions.length > 0
? `${buildSyncOutcomeSentence(status)} Sync completed: ${actions.join(', ')}.`
: `${buildSyncOutcomeSentence(status)} Project results were already up to date.`,
};
}
export function buildProjectSyncErrorFeedback(
error: unknown,
status: RemoteStatusResponse | undefined,
): {
kind: 'error';
message: string;
} {
const message = error instanceof Error ? error.message : String(error);
return {
kind: 'error',
message: `Sync failed: ${message}. ${buildRemoteErrorAction(status)}`,
};
}