forked from adjust/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
248 lines (214 loc) · 6.6 KB
/
stats.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package rmq
import (
"bytes"
"fmt"
"sort"
)
type ConnectionStat struct {
active bool
unackedCount int
consumers []string
}
func (stat ConnectionStat) String() string {
return fmt.Sprintf("[unacked:%d consumers:%d]",
stat.unackedCount,
len(stat.consumers),
)
}
type ConnectionStats map[string]ConnectionStat
type QueueStat struct {
ReadyCount int `json:"ready"`
ReadyNormalCount int `json:readyNormal"`
ReadyPriorityCount int `json:"readyPriority"`
RejectedCount int `json:"rejected"`
connectionStats ConnectionStats
}
func NewQueueStat(readyCount, readyNormalCount, readyPriorityCount, rejectedCount int) QueueStat {
return QueueStat{
ReadyCount: readyCount,
ReadyNormalCount: readyNormalCount,
ReadyPriorityCount: readyPriorityCount,
RejectedCount: rejectedCount,
connectionStats: ConnectionStats{},
}
}
func (stat QueueStat) String() string {
return fmt.Sprintf("[ready:%d (%d+%d) rejected:%d conn:%s",
stat.ReadyCount,
stat.ReadyNormalCount,
stat.ReadyPriorityCount,
stat.RejectedCount,
stat.connectionStats,
)
}
func (stat QueueStat) UnackedCount() int {
unacked := 0
for _, connectionStat := range stat.connectionStats {
unacked += connectionStat.unackedCount
}
return unacked
}
func (stat QueueStat) ConsumerCount() int {
consumer := 0
for _, connectionStat := range stat.connectionStats {
consumer += len(connectionStat.consumers)
}
return consumer
}
func (stat QueueStat) ConnectionCount() int {
return len(stat.connectionStats)
}
type QueueStats map[string]QueueStat
type Stats struct {
QueueStats QueueStats `json:"queues"`
otherConnections map[string]bool // non consuming connections, active or not
}
func NewStats() Stats {
return Stats{
QueueStats: QueueStats{},
otherConnections: map[string]bool{},
}
}
func CollectStats(queueList []string, mainConnection *redisConnection) Stats {
stats := NewStats()
for _, queueName := range queueList {
queue := mainConnection.openQueue(queueName)
stats.QueueStats[queueName] = NewQueueStat(queue.ReadyCount(), queue.ReadyNormalCount(), queue.ReadyPriorityCount(), queue.RejectedCount())
}
connectionNames := mainConnection.GetConnections()
for _, connectionName := range connectionNames {
connection := mainConnection.hijackConnection(connectionName)
connectionActive := connection.Check()
queueNames := connection.GetConsumingQueues()
if len(queueNames) == 0 {
stats.otherConnections[connectionName] = connectionActive
continue
}
for _, queueName := range queueNames {
queue := connection.openQueue(queueName)
consumers := queue.GetConsumers()
openQueueStat, ok := stats.QueueStats[queueName]
if !ok {
continue
}
openQueueStat.connectionStats[connectionName] = ConnectionStat{
active: connectionActive,
unackedCount: queue.UnackedCount(),
consumers: consumers,
}
}
}
return stats
}
func (stats Stats) String() string {
var buffer bytes.Buffer
for queueName, queueStat := range stats.QueueStats {
buffer.WriteString(fmt.Sprintf(" queue:%s ready:%d (%d+%d) rejected:%d unacked:%d consumers:%d\n",
queueName, queueStat.ReadyCount, queueStat.ReadyNormalCount, queueStat.ReadyPriorityCount, queueStat.RejectedCount, queueStat.UnackedCount(), queueStat.ConsumerCount(),
))
for connectionName, connectionStat := range queueStat.connectionStats {
buffer.WriteString(fmt.Sprintf(" connection:%s unacked:%d consumers:%d active:%t\n",
connectionName, connectionStat.unackedCount, len(connectionStat.consumers), connectionStat.active,
))
}
}
for connectionName, active := range stats.otherConnections {
buffer.WriteString(fmt.Sprintf(" connection:%s active:%t\n",
connectionName, active,
))
}
return buffer.String()
}
func (stats Stats) GetHtml(layout, refresh string) string {
buffer := bytes.NewBufferString("<html>")
if refresh != "" {
buffer.WriteString(fmt.Sprintf(`<head><meta http-equiv="refresh" content="%s">`, refresh))
}
buffer.WriteString(`<body><table style="font-family:monospace">`)
buffer.WriteString(`<tr><td>` +
`queue</td><td></td><td>` +
`ready</td><td></td><td>` +
`rejected</td><td></td><td>` +
`</td><td></td><td>` +
`connections</td><td></td><td>` +
`unacked</td><td></td><td>` +
`consumers</td><td></td></tr>`,
)
for _, queueName := range stats.sortedQueueNames() {
queueStat := stats.QueueStats[queueName]
connectionNames := queueStat.connectionStats.sortedNames()
buffer.WriteString(fmt.Sprintf(`<tr><td>`+
`%s</td><td></td><td>`+
`%d (%d+%d)</td><td></td><td>`+
`%d</td><td></td><td>`+
`%s</td><td></td><td>`+
`%d</td><td></td><td>`+
`%d</td><td></td><td>`+
`%d</td><td></td></tr>`,
queueName, queueStat.ReadyCount, queueStat.ReadyNormalCount, queueStat.ReadyPriorityCount, queueStat.RejectedCount, "", len(connectionNames), queueStat.UnackedCount(), queueStat.ConsumerCount(),
))
if layout != "condensed" {
for _, connectionName := range connectionNames {
connectionStat := queueStat.connectionStats[connectionName]
buffer.WriteString(fmt.Sprintf(`<tr style="color:lightgrey"><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%d</td><td></td><td>`+
`%d</td><td></td></tr>`,
"", "", "", ActiveSign(connectionStat.active), connectionName, connectionStat.unackedCount, len(connectionStat.consumers),
))
}
}
}
if layout != "condensed" {
buffer.WriteString(`<tr><td>-----</td></tr>`)
for _, connectionName := range stats.sortedConnectionNames() {
active := stats.otherConnections[connectionName]
buffer.WriteString(fmt.Sprintf(`<tr style="color:lightgrey"><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td><td>`+
`%s</td><td></td></tr>`,
"", "", "", ActiveSign(active), connectionName, "", "",
))
}
}
buffer.WriteString(`</table></body></html>`)
return buffer.String()
}
func (stats ConnectionStats) sortedNames() []string {
var keys []string
for key := range stats {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func (stats Stats) sortedQueueNames() []string {
var keys []string
for key := range stats.QueueStats {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func (stats Stats) sortedConnectionNames() []string {
var keys []string
for key := range stats.otherConnections {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func ActiveSign(active bool) string {
if active {
return "✓"
}
return "✗"
}