Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ui/koko/tests/sftp/fileTransferRate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("file transfer rate helper", () => {
expect(remainingSeconds(10_240, 2_048, 2048)).toBe(4);
});

it("drops stale samples and ignores zero-size remaining", () => {
it("keeps the last two samples when a chunk is slower than the window", () => {
const samples = pushTransferRateSample(
[
{ t: 1_000, bytes: 0 },
Expand All @@ -31,7 +31,11 @@ describe("file transfer rate helper", () => {
200,
7_000
);
expect(samples).toEqual([{ t: 7_000, bytes: 200 }]);
expect(samples).toEqual([
{ t: 2_000, bytes: 100 },
{ t: 7_000, bytes: 200 }
]);
expect(bytesPerSecond(samples)).toBe(20);
expect(remainingSeconds(0, 0, 100)).toBeNull();
expect(remainingSeconds(100, 100, 50)).toBe(0);
});
Expand Down
5 changes: 4 additions & 1 deletion ui/koko/utils/file-transfer/rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export function pushTransferRateSample(
bytes: number,
now = Date.now()
): TransferRateSample[] {
return [...samples, { t: now, bytes }].filter((sample) => now - sample.t <= sampleWindowMs).slice(-12);
const next = [...samples, { t: now, bytes }];
const recent = next.filter((sample) => now - sample.t <= sampleWindowMs);
if (recent.length >= 2) return recent.slice(-12);
return next.slice(-2);
}

export function bytesPerSecond(samples: TransferRateSample[] | null | undefined): number | null {
Expand Down
Loading