Skip to content

Commit

Permalink
Refactor metrics (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jun 9, 2023
1 parent b98a9d6 commit 2f5bd09
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (
type Metric struct {
Name string
Type Type
Value *int64
Value func() int64
}

// DefaultStatus renders Status as a string.
Expand Down Expand Up @@ -79,12 +79,12 @@ func MetricsStatus(s Status) string {
for _, m := range s.Metrics {
switch m.Type {
case Bytes:
spdMBPS := float64(atomic.LoadInt64(m.Value)) / (s.Elapsed.Seconds() * 1024 * 1024)
spdMBPS := float64(m.Value()) / (s.Elapsed.Seconds() * 1024 * 1024)
metrics += fmt.Sprintf("%s: %.1f MB/s, ", m.Name, spdMBPS)
case Duration:
metrics += m.Name + ": " + time.Duration(atomic.LoadInt64(m.Value)).String() + ", "
metrics += m.Name + ": " + time.Duration(m.Value()).String() + ", "
case Gauge:
metrics += fmt.Sprintf("%s: %d, ", m.Name, atomic.LoadInt64(m.Value))
metrics += fmt.Sprintf("%s: %d, ", m.Name, m.Value())
}
}

Expand All @@ -95,7 +95,7 @@ func MetricsStatus(s Status) string {
return metrics
}

// Task describes long running process.
// Task describes a long-running process.
type Task struct {
TotalBytes func() int64
CurrentBytes func() int64
Expand Down Expand Up @@ -185,6 +185,15 @@ func (p *Progress) Stop() {
close(p.done)
}

// Lines returns current number of lines.
func (p *Progress) Lines() int64 {
if p.lines != nil {
return p.lines()
}

return 0
}

// CountingReader wraps io.Reader to count bytes.
type CountingReader struct {
Reader io.Reader
Expand Down

0 comments on commit 2f5bd09

Please sign in to comment.