diff --git a/consts.go b/consts.go new file mode 100644 index 0000000..63f2858 --- /dev/null +++ b/consts.go @@ -0,0 +1,8 @@ +package eta + +import "time" + +const ( + defaultPeriodCount = 10 + defaultPeriodDuration = time.Minute +) diff --git a/eta.go b/eta.go index 7bca084..cebd372 100644 --- a/eta.go +++ b/eta.go @@ -13,6 +13,9 @@ type Calculator struct { // Expected processing count TotalCount int + // Number of periods to store + PeriodCount int + periodDuration time.Duration currentPeriod time.Time currentProcessed int @@ -22,12 +25,18 @@ type Calculator struct { } // New return new ETA calculator -func New(periodDuration time.Duration, totalCount int) *Calculator { +func New(totalCount int) *Calculator { + return NewCustom(totalCount, defaultPeriodDuration) +} + +// NewCustom return new ETA calculator with custom params +func NewCustom(totalCount int, periodDuration time.Duration) *Calculator { now := time.Now() etaCalc := &Calculator{ startTime: now, TotalCount: totalCount, + PeriodCount: defaultPeriodCount, currentPeriod: now.Truncate(periodDuration), periodDuration: periodDuration} @@ -59,8 +68,8 @@ func (ec *Calculator) Increment(n int) { ec.currentPeriod = period } - if len(ec.stats) > 10 { - ec.stats = ec.stats[:10] + if len(ec.stats) > ec.PeriodCount { + ec.stats = ec.stats[:ec.PeriodCount] } }