forked from madcowfred/GoPostStuff
-
Notifications
You must be signed in to change notification settings - Fork 3
/
status.go
71 lines (57 loc) · 1.52 KB
/
status.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"github.com/f4n4t/gopoststuff/simplenntp"
"time"
"github.com/dustin/go-humanize"
)
func StatusLogger(ticker *time.Ticker, tdchan chan *simplenntp.TimeData) {
var tds []*simplenntp.TimeData
totalPosted := int64(0)
for t := range ticker.C {
stamp := t.UnixNano() / 1e6
tds = append(tds, &simplenntp.TimeData{stamp, 0})
// Fetch any new TimeData entries
var breakNow bool
for {
breakNow = false
select {
case td := <-tdchan:
// New item, add it to our list
tds = append(tds, td)
totalPosted += int64(td.Bytes)
default:
// Nothing else in the channel, done for now
breakNow = true
}
if breakNow {
break
}
}
// Calculate current speed
if len(tds) > 0 {
active := float64(tds[len(tds)-1].Milliseconds-tds[0].Milliseconds) / 1000
totalBytes := 0
for _, td := range tds {
totalBytes += td.Bytes
}
speed := float64(totalBytes) / float64(active)
// Total posted
posted := float64(totalPosted) / 1024 / 1024
// Print it
//fmt.Printf("Posted \033[1m%.1f\033[0mMiB - Current speed: \033[1m%.1f\033[0mKiB/s \r", posted, speed)
fmt.Printf("Posted \033[1m%.1f\033[0mMiB - Current speed: \033[1m%s/s \r", posted, humanize.Bytes(uint64(speed)))
//log.Debug("Current speed: %.1fKB/s", speed)
}
// Trim slice to only use the last 5 seconds
earliest := stamp - 5000
start := 0
for i, td := range tds {
if td.Milliseconds >= earliest {
start = i
break
}
}
tds = tds[start:]
}
}