lib/progress.ts: get an external reviewer for the default estimation algorithm #76

Open
opened 2026-01-28 02:35:05 -08:00 by clo · 1 comment
Owner

i think it is implemented in a buggy way. i gave it some tests in an isolated place, but not too much on real world cases.

Lines 1679 to 1721 in 8b330ca
/**
* Exponential Moving Average.
* this is the algorithm used for the default progress estimator.
* https://en.wikipedia.org/wiki/Exponential_smoothing
*/
export class Ema implements EstimationAlgorithm {
start: number | null = null;
estimate: number | null = null;
/** smoothing factor (0-1) */
alpha: number = 0.25;
constructor(alpha: number = 0.1) {
this.alpha = alpha;
}
/**
* given a timestamp `time` and a progress value `progress`, return the
* timestamp that the action will be finished on.
*/
sample(time: number, progress: number): number | null {
ASSERT(
progress >= 0 && progress < 1,
`progress must be a number between 0 and 1, got ${progress}`,
);
if (this.start == null) {
this.start = time;
return null;
}
const value = (time - this.start) / progress;
this.estimate = this.estimate != null
? ((1 - this.alpha) * this.estimate +
this.alpha * value)
: value;
return time + (1 - progress) * this.estimate;
}
reset(): void {
this.start = null;
this.estimate = null;
}
}

i think it is implemented in a buggy way. i gave it some tests in an isolated place, but not too much on real world cases. https://git.paperclover.net/clo/sitegen/src/commit/8b330ca47d47e63d0c107691ecf75236fd112ba1/lib/progress.ts#L1679-L1721
Author
Owner

my suspicion is that the api interface should be sample(time: number, current: number, total: number) where both numbers must be >= 0 and current < total

my suspicion is that the api interface should be `sample(time: number, current: number, total: number)` where both numbers must be >= 0 and current < total
clo added this to the progress blog post milestone 2026-02-07 01:58:37 -08:00
Sign in to join this conversation.
No labels
bug
chore
feat
lib
site
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
clo/sitegen#76
No description provided.