feat: higher accuracy progress tracking

This commit is contained in:
izzy
2025-11-24 15:19:38 +00:00
parent e2ca0c6f67
commit 3be039b953

View File

@@ -110,6 +110,7 @@ export async function buildPostgresLaunchArguments(
'ON_ERROR_STOP=on', 'ON_ERROR_STOP=on',
// used for progress monitoring // used for progress monitoring
'--echo-all', '--echo-all',
'--output=/dev/null',
); );
break; break;
} }
@@ -296,27 +297,34 @@ function createSqlProgressStreams(cb: (progress: number) => void) {
let readingStdin = false; let readingStdin = false;
let sequenceIdx = 0; let sequenceIdx = 0;
let bytesSent = 0; let linesSent = 0;
let bytesProcessed = 0; let linesProcessed = 0;
const startedAt = +Date.now(); const startedAt = +Date.now();
const cbDebounced = debounce( const cbDebounced = debounce(
() => { () => {
const progress = source.writableEnded const progress = source.writableEnded
? Math.max(1, bytesProcessed / bytesSent) ? Math.min(1, linesProcessed / linesSent)
: // progress simulation while we're in an indeterminate state : // progress simulation while we're in an indeterminate state
Math.min(0.3, 0.1 + (Date.now() - startedAt) / 1e4); Math.min(0.3, 0.1 + (Date.now() - startedAt) / 1e4);
cb(progress); cb(progress);
}, },
100, 100,
{ {
maxWait: 200, maxWait: 100,
}, },
); );
let lastByte = -1;
const source = new PassThrough({ const source = new PassThrough({
transform(chunk, _encoding, callback) { transform(chunk, _encoding, callback) {
for (const byte of chunk) { for (const byte of chunk) {
if (!readingStdin && byte === 10 && lastByte !== 10) {
linesSent += 1;
}
lastByte = byte;
const sequence = readingStdin ? STDIN_END_MARKER : STDIN_START_MARKER; const sequence = readingStdin ? STDIN_END_MARKER : STDIN_START_MARKER;
if (sequence[sequenceIdx] === byte) { if (sequence[sequenceIdx] === byte) {
sequenceIdx += 1; sequenceIdx += 1;
@@ -330,11 +338,7 @@ function createSqlProgressStreams(cb: (progress: number) => void) {
} }
} }
if (!readingStdin) { cbDebounced();
bytesSent += chunk.length;
cbDebounced();
}
this.push(chunk); this.push(chunk);
callback(); callback();
}, },
@@ -342,7 +346,13 @@ function createSqlProgressStreams(cb: (progress: number) => void) {
const sink = new Writable({ const sink = new Writable({
write(chunk, _encoding, callback) { write(chunk, _encoding, callback) {
bytesProcessed += chunk.length; for (const byte of chunk) {
if (byte === 10) {
linesProcessed++;
}
}
cbDebounced();
callback(); callback();
}, },
}); });