forked from randomtask1155/firehose-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay.go
123 lines (114 loc) · 2.34 KB
/
replay.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bufio"
"os"
"strconv"
"strings"
"time"
"github.com/cloudfoundry/sonde-go/events"
)
func runReplay() {
fh, err := os.Open(*replay)
if err != nil {
logger.Fatalln(err)
}
defer fh.Close()
timestamp := 0
origin := 1
jobIndex := 2
metric := 3
value := 4
eventType := 5
unit := 6
totalLines := 0.0
lineScanner := bufio.NewScanner(fh)
for lineScanner.Scan() {
totalLines++
}
_, err = fh.Seek(0, 0)
if err != nil {
logger.Printf("could not rewinde file: %s", err)
}
currentLine := 0.0
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
currentLine++
d := strings.Split(scanner.Text(), ",")
if len(d) != 7 {
logger.Printf("Bad Data in line : %v", d)
continue
}
if d[timestamp] == "time" {
continue // skip header
}
ts, err := time.Parse("2006-01-02T15:04:05-07:00", d[timestamp])
if err != nil {
logger.Printf("Can not parse timestamp: %s", err)
continue
}
tu := ts.Unix()
var et events.Envelope_EventType
var e *events.Envelope
ji := strings.Split(d[jobIndex], "/")
if len(ji) != 2 {
logger.Println("Bad job index data")
continue
}
if d[eventType] == "ValueMetric" {
val, err := strconv.ParseFloat(d[value], 64)
if err != nil {
logger.Printf("parsing value failed: %v: %s", d, err)
continue
}
et = events.Envelope_ValueMetric
e = &events.Envelope{
Timestamp: &tu,
Origin: &d[origin],
Job: &ji[0],
Index: &ji[1],
EventType: &et,
ValueMetric: &events.ValueMetric{
Name: &d[metric],
Value: &val,
Unit: &d[unit],
},
}
} else {
var v, u int
var err error
var vv, uu uint64
v, err = strconv.Atoi(d[value])
if err != nil {
logger.Println(err)
continue
}
u, err = strconv.Atoi(d[unit])
if err != nil {
logger.Println(err)
continue
}
vv = uint64(v)
uu = uint64(u)
et = events.Envelope_CounterEvent
e = &events.Envelope{
Timestamp: &tu,
Origin: &d[origin],
Job: &ji[0],
Index: &ji[1],
EventType: &et,
CounterEvent: &events.CounterEvent{
Name: &d[metric],
Delta: &vv,
Total: &uu,
},
}
}
updateProgressBar(currentLine / totalLines)
mc.parseEnvelope(e)
if *speed > 0 {
time.Sleep(time.Duration(100 / *speed) * time.Millisecond)
}
//TODO CONFIGURE SPEED
}
updateProgressBar(1)
}