-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtracker.go
72 lines (61 loc) · 1.26 KB
/
tracker.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
72
package main
import (
"fmt"
"sort"
"sync"
"time"
)
type Tracker struct {
mtx sync.Mutex
actual map[string]interface{}
fields []string
startTime int64
}
func (t *Tracker) prepare() {
t.startTime = time.Now().Unix()
t.saveTime()
}
func (t *Tracker) track(update *map[string]interface{}) {
if t.actual == nil {
t.actual = make(map[string]interface{})
}
for k, v := range *update {
t.trackOne(k, v)
}
}
func (t *Tracker) trackOne(key string, value interface{}) {
t.mtx.Lock()
defer t.mtx.Unlock()
if t.actual == nil {
t.actual = make(map[string]interface{})
}
t.actual[key] = value
}
func (t *Tracker) prepareAndPrintHeader() {
t.fields = make([]string, 0)
for k := range t.actual {
t.fields = append(t.fields, k)
}
sort.Strings(t.fields)
for _, j := range t.fields {
fmt.Printf("%*s, ", len(j), j)
}
fmt.Println("")
}
func (t *Tracker) printData() {
t.mtx.Lock()
defer t.mtx.Unlock()
for n, k := range t.fields {
switch t.actual[k].(type) {
default:
fmt.Printf("%*v, ", len(t.fields[n]), t.actual[k])
case float64:
fmt.Printf("%*.2f, ", len(t.fields[n]), t.actual[k].(float64))
}
}
fmt.Println("")
}
func (t *Tracker) saveTime() {
actualTime := time.Now().Unix() - t.startTime
t.trackOne("time", actualTime)
}