-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
195 lines (178 loc) · 4.61 KB
/
parser.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package beanstalkg
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
type InstanceStats struct {
RusageUtime float64
Uptime int64
BinlogCurrentIndex int64
BinlogRecordsMigrated int64
CmdStatsJob int64
CmdListTubes int64
CmdStats int64
CmdPeekReady int64
CmdDelete int64
CmdReserveWithTimeout int64
JobTimeouts int64
MaxJobSize int64
RusageStime float64
Id string
CurrentJobsBuried int64
CmdPeekDelayed int64
CmdTouch int64
CurrentConnections int64
BinlogRecordsWritten int64
CurrentJobsReady int64
CmdReserve int64
CmdPeek int64
CmdRelease int64
CmdBury int64
TotalJobs int64
Version int64
CurrentJobsUrgent int64
CmdPut int64
CmdStatsTube int64
CmdListTubesWatched int64
CurrentWorkers int64
BinlogOldestIndex int64
BinlogMaxSize int64
Hostname string
CurrentJobsReserved int64
CmdWatch int64
CmdIgnore int64
TotalConnections int64
CurrentJobsDelayed int64
CmdPeekBuried int64
CmdListTubeUsed int64
CmdPauseTube int64
CurrentTubes int64
CurrentProducers int64
CurrentWaiting int64
Pid int64
CmdUse int64
CmdKick int64
}
func (s *InstanceStats) FillStruct(m map[string]interface{}) error {
for k, v := range m {
key := normaliseKey(k)
_ = SetField(s, key, v)
}
return nil
}
func normaliseKey(key string) string {
noHashes := strings.Replace(key, "-", " ", -1)
titleCase := strings.Title(noHashes)
return strings.Replace(titleCase, " ", "", -1)
}
var stringFields = map[string]bool{
"id": true,
"hostname": true,
}
var floatFields = map[string]bool{
"rusage-utime": true,
"rusage-stime": true,
}
func isFieldType(key string, fields map[string]bool) bool {
_, ok := fields[key]
return ok
}
func SetField(obj interface{}, name string, value interface{}) error {
structValue := reflect.ValueOf(obj).Elem()
structFieldValue := structValue.FieldByName(name)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type()
val := reflect.ValueOf(value)
if structFieldType != val.Type() {
return errors.New("Provided value type didn't match obj field type")
}
structFieldValue.Set(val)
return nil
}
func statsParser(res []byte) *InstanceStats {
instanceStats := make(map[string]interface{})
results := strings.Split(string(res), "\n")
for _, r := range results {
stat := strings.Split(r, ":")
if len(stat) == 2 {
key := stat[0]
value := strings.TrimSpace(stat[1])
switch {
case isFieldType(key, stringFields):
instanceStats[key] = value
case isFieldType(key, floatFields):
floatValue, _ := strconv.ParseFloat(value, 64)
instanceStats[key] = floatValue
default:
intValue, _ := strconv.ParseInt(value, 10, 64)
instanceStats[key] = intValue
}
}
}
result := &InstanceStats{}
result.FillStruct(instanceStats)
return result
}
func listParser(res []byte) []string {
tubes := []string{}
results := strings.Split(string(res), "\n")
for _, res := range results {
if len(res) > 2 && res[:2] == "- " {
tubes = append(tubes, strings.TrimSpace(res[2:]))
}
}
return tubes
}
type TubeStats struct {
Name string
CurrentJobsUrgent int64
CurrentJobsReady int64
CurrentJobsReserved int64
CurrentJobsBuried int64
CurrentJobsDelayed int64
TotalJobs int64
CurrentUsing int64
CurrentWatching int64
CurrentWaiting int64
CmdDelete int64
CmdPauseTube int64
Pause int64
PauseTimeLeft int64
}
func (s *TubeStats) FillStruct(m map[string]interface{}) error {
for k, v := range m {
key := normaliseKey(k)
SetField(s, key, v)
}
return nil
}
func tubeStatsParser(res []byte) *TubeStats {
tubeStats := make(map[string]interface{})
fmt.Println(string(res))
results := strings.Split(string(res), "\n")
for _, r := range results {
stat := strings.Split(r, ":")
if len(stat) == 2 {
key := stat[0]
val := stat[1]
if key == "name" {
tubeStats[key] = val
} else {
intValue, _ := strconv.ParseInt(strings.TrimSpace(val), 10, 64)
fmt.Println(intValue)
tubeStats[key] = intValue
}
}
}
result := &TubeStats{}
result.FillStruct(tubeStats)
return result
}