Make params public

This commit is contained in:
nxshock 2022-08-10 21:08:34 +05:00
parent a2724902bf
commit cf1cdab586
2 changed files with 20 additions and 3 deletions

8
consts.go Normal file
View File

@ -0,0 +1,8 @@
package eta
import "time"
const (
defaultPeriodCount = 10
defaultPeriodDuration = time.Minute
)

15
eta.go
View File

@ -13,6 +13,9 @@ type Calculator struct {
// Expected processing count // Expected processing count
TotalCount int TotalCount int
// Number of periods to store
PeriodCount int
periodDuration time.Duration periodDuration time.Duration
currentPeriod time.Time currentPeriod time.Time
currentProcessed int currentProcessed int
@ -22,12 +25,18 @@ type Calculator struct {
} }
// New return new ETA calculator // 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() now := time.Now()
etaCalc := &Calculator{ etaCalc := &Calculator{
startTime: now, startTime: now,
TotalCount: totalCount, TotalCount: totalCount,
PeriodCount: defaultPeriodCount,
currentPeriod: now.Truncate(periodDuration), currentPeriod: now.Truncate(periodDuration),
periodDuration: periodDuration} periodDuration: periodDuration}
@ -59,8 +68,8 @@ func (ec *Calculator) Increment(n int) {
ec.currentPeriod = period ec.currentPeriod = period
} }
if len(ec.stats) > 10 { if len(ec.stats) > ec.PeriodCount {
ec.stats = ec.stats[:10] ec.stats = ec.stats[:ec.PeriodCount]
} }
} }