-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
40 lines (35 loc) · 937 Bytes
/
progress.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
31
32
33
34
35
36
37
38
39
40
package util
import (
"fmt"
"github.com/sirupsen/logrus"
"math"
"time"
)
type Sizeable interface {
Size() (int64, error)
}
func TrackProgress(
sizeable Sizeable,
logger *logrus.Entry,
) (updateProgress func(position int64)) {
totalSize, err := sizeable.Size()
if err != nil {
logger.Errorf("Cannot determine the total size: '%+v'. The progress will not be displayed.", err)
} else if totalSize == 0 {
logger.Infoln("The total size is unknown. The progress will not be displayed.")
}
nextProgress := time.Now()
return func(position int64) {
if totalSize != 0 {
if now := time.Now(); now.After(nextProgress) {
progress := float64(position) / float64(totalSize)
progressPercent := fmt.Sprintf("%d%%", int(math.Floor(progress*100)))
logger.
WithField("progress", progressPercent).
WithField("offset", position).
Infof("Indexing...")
nextProgress = now.Add(time.Second)
}
}
}
}