-
Notifications
You must be signed in to change notification settings - Fork 672
/
eta.go
30 lines (25 loc) · 1006 Bytes
/
eta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package timer
import (
"encoding/binary"
"time"
)
// ProgressFromHash returns the progress out of MaxUint64 assuming [b] is a key
// in a uniformly distributed sequence that is being iterated lexicographically.
func ProgressFromHash(b []byte) uint64 {
// binary.BigEndian.Uint64 will panic if the input length is less than 8, so
// pad 0s as needed.
var progress [8]byte
copy(progress[:], b)
return binary.BigEndian.Uint64(progress[:])
}
// EstimateETA attempts to estimate the remaining time for a job to finish given
// the [startTime] and it's current progress.
func EstimateETA(startTime time.Time, progress, end uint64) time.Duration {
timeSpent := time.Since(startTime)
percentExecuted := float64(progress) / float64(end)
estimatedTotalDuration := time.Duration(float64(timeSpent) / percentExecuted)
eta := estimatedTotalDuration - timeSpent
return eta.Round(time.Second)
}