/
tally.go
90 lines (72 loc) · 1.62 KB
/
tally.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
package main
type Tally struct {
brokers map[int32]int
topics map[string]int
partitions map[string]map[int32]int
groups map[string]int
}
type brokerBucket map[int32]int
type topicBucket map[string]int
type groupBucket map[string]int
type partitionBucket map[string]map[int32]int
func NewTally() *Tally {
return &Tally{
brokers: make(brokerBucket),
topics: make(topicBucket),
groups: make(groupBucket),
partitions: make(partitionBucket),
}
}
func (t *Tally) Add(broker int32, topic, group string, partition int32) {
t.brokers[broker] = 1
t.groups[group] = 1
t.topics[topic] = 1
partitionMap := t.partitions[topic+group]
if partitionMap == nil {
partitionMap = make(map[int32]int)
t.partitions[topic+group] = partitionMap
}
partitionMap[partition] = 1
}
func (t *Tally) BrokerCount() (count int) {
for _, v := range t.brokers {
count = count + v
}
return
}
func (t *Tally) GroupCount() (count int) {
for _, v := range t.groups {
count = count + v
}
return
}
func (t *Tally) TopicCount() (count int) {
for _, v := range t.topics {
count = count + v
}
return
}
func (t *Tally) PartitionCount() (count int) {
for _, partitionMap := range t.partitions {
for _, partitionFlag := range partitionMap {
count = count + partitionFlag
}
}
return
}
func (t *Tally) Reset() {
for broker, _ := range t.brokers {
t.brokers[broker] = 0
}
for group, _ := range t.groups {
t.groups[group] = 0
}
for topic, _ := range t.topics {
t.topics[topic] = 0
}
for _, partitionMap := range t.partitions {
for partition, _ := range partitionMap {
partitionMap[partition] = 0
}
}
}