-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtopics.go
210 lines (196 loc) · 5.9 KB
/
topics.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
// Subscribe to the Kafka topics and read the JSON messages.
// Create a counter entry for the timestamp and increment for each bid, win, pixel, click message.
//
package main
import (
"encoding/json"
"fmt"
"sync"
"time"
cluster "github.com/bsm/sarama-cluster"
)
// BidFields - message format of Kafka bids topic
type BidFields struct {
CampaignID int64 `json:"adid,string"`
CreativeID int64 `json:"crid,string"`
AdType string `json:"adtype"`
Domain string `json:"domain"`
Exchange string `json:"exchange"`
Cost float64 `json:"cost"`
Timestamp int64 `json:"timestamp"`
}
// WinFields - message format of Kafka wins topic
type WinFields struct {
CampaignID int64 `json:"adId,string"`
CreativeID int64 `json:"cridId,string"`
AdType string `json:"adtype"`
Exchange string `json:"pubId"`
Cost float64 `json:"cost,string"`
Price float64 `json:"price,string"`
Timestamp int64 `json:"timestamp"`
Domain string `json:"domain"`
}
// PixelFields - message format of Kafka pixels topic
type PixelFields struct {
CampaignID int64 `json:"ad_id,string"`
CreativeID int64 `json:"creative_id,string"`
AdType string `json:"adtype"` // Not in msg
Exchange string `json:"exchange"`
Timestamp int64 `json:"timestamp"`
Domain string `json:"domain"`
}
// ClickFields - message format of Kafka clicks topic
type ClickFields struct {
CampaignID int64 `json:"ad_id,string"`
CreativeID int64 `json:"creative_id,string"`
AdType string `json:"adtype"` //Not in msg
Exchange string `json:"exchange"`
Timestamp int64 `json:"timestamp"`
Domain string `json:"domain"`
}
// Separate go routine for each topic
func getTopic(config *cluster.Config, brokers []string, topics []string) {
log1 := logger.GetLogger("getTopic")
log1.Info("Connect to brokers ", brokers, " topics ", topics)
consumer, err2 := cluster.NewConsumer(brokers, "rtb-consumer-group-1", topics, config)
if err2 != nil {
log1.Alert(err2.Error())
panic(err2)
}
//defer consumer.Close()
go func() {
log1 := logger.GetLogger("getTopic go func")
for {
select {
case part, ok := <-consumer.Partitions():
if !ok {
log1.Error("consumer.Partions error.")
return
}
// start a separate goroutine to consume messages
go func(pc cluster.PartitionConsumer) {
log1 := logger.GetLogger("getTopic kafka partition")
for msg := range pc.Messages() {
log1.Debug(fmt.Sprintf("%s/%d/%d\t%s\t%s", msg.Topic, msg.Partition, msg.Offset, msg.Key, msg.Value))
switch msg.Topic {
case "bids":
aggBids.addCount(msg.Topic, msg.Value)
case "wins":
aggWins.addCount(msg.Topic, msg.Value)
case "pixels":
aggPixels.addCount(msg.Topic, msg.Value)
case "clicks":
aggClicks.addCount(msg.Topic, msg.Value)
default:
log1.Alert(fmt.Sprintf("Unexpected topic %s.", msg.Topic))
return
}
consumer.MarkOffset(msg, "") // mark message as processed
}
}(part)
}
}
}()
return
}
// Update counters
func (agg OutputCounts) addCount(topic string, msg []byte) {
log1 := logger.GetLogger("addCount")
var key RecordKey
var ts string
var tsMs int64
var tm time.Time
switch topic {
case "bids":
field := BidFields{}
if err := json.Unmarshal(msg, &field); err != nil {
logger.Error(fmt.Sprintf("JSON unmarshaling failed: %s", err))
return
}
ts, tsMs, tm = intervalTimestamp(field.Timestamp, intervalSecs)
// Create unique aggregation key
key = RecordKey{
CampaignID: field.CampaignID,
CreativeID: field.CreativeID,
IntervalStr: intervalStr,
IntervalTs: ts,
}
case "wins":
field := WinFields{}
if err := json.Unmarshal(msg, &field); err != nil {
logger.Error(fmt.Sprintf("JSON unmarshaling failed: %s", err))
return
}
ts, tsMs, tm = intervalTimestamp(field.Timestamp, intervalSecs)
key = RecordKey{
CampaignID: field.CampaignID,
CreativeID: field.CreativeID,
IntervalStr: intervalStr,
IntervalTs: ts,
}
case "pixels":
field := PixelFields{}
if err := json.Unmarshal(msg, &field); err != nil {
logger.Error(fmt.Sprintf("JSON unmarshaling failed: %s", err))
return
}
ts, tsMs, tm = intervalTimestamp(field.Timestamp, intervalSecs)
key = RecordKey{
CampaignID: field.CampaignID,
CreativeID: field.CreativeID,
IntervalStr: intervalStr,
IntervalTs: ts,
}
case "clicks":
field := ClickFields{}
if err := json.Unmarshal(msg, &field); err != nil {
logger.Error(fmt.Sprintf("JSON unmarshaling failed: %s", err))
return
}
ts, tsMs, tm = intervalTimestamp(field.Timestamp, intervalSecs)
key = RecordKey{
CampaignID: field.CampaignID,
CreativeID: field.CreativeID,
IntervalStr: intervalStr,
IntervalTs: ts,
}
default:
log1.Alert(fmt.Sprintf("Unexpected topic %s.", topic))
return
}
// Check if agg record already exists for this camp/creat/interval
_, ok := agg[key]
if !ok {
// Doesn't exists so initialize
agg[key] = CountFields{new(sync.Mutex), 0, tsMs, tm}
}
agg[key].lock.Lock() // mutex lock
tmp := agg[key] // increment count
tmp.count++
agg[key] = tmp
agg[key].lock.Unlock() // mutex unlock
}
// Compute interval timestamp - string and epoch milliseconds
func intervalTimestamp(timestamp int64, interval int64) (string, int64, time.Time) {
log1 := logger.GetLogger("intervalTimestamp")
// Round off to interval
timestamp = timestamp / 1000 // convert to epoch secs
timestamp = int64(timestamp/interval) * interval
tm := time.Unix(int64(timestamp), 0)
str := tm.UTC().Format(time.RFC3339)
epochms := int64(tm.Unix()) * 1000
log1.Debug(fmt.Sprintf("Interval Time stamp string: %s, %d", str, epochms))
return str, epochms, tm
}
// Lock counter entry
func (agg OutputCounts) Lock(key RecordKey) {
if _, found := agg[key]; found {
agg[key].lock.Lock()
}
}
// Unlock counter entry
func (agg OutputCounts) Unlock(key RecordKey) {
if _, found := agg[key]; found {
agg[key].lock.Unlock()
}
}