From cf1cdab58625f93ef795b5d5a41dc84e1767a1c8 Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 10 Aug 2022 21:08:34 +0500 Subject: [PATCH] Make params public --- consts.go | 8 ++++++++ eta.go | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 consts.go 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] } }