-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathelastic.go
269 lines (256 loc) · 8.27 KB
/
elastic.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"context"
"fmt"
"strconv"
"strings"
"time"
neturl "net/url"
//"github.com/olivere/elastic"
// MUST USE COMPATIBLE ELASTICSEARCH VERSION
"gopkg.in/olivere/elastic.v6"
)
// AdSize - embedded structure for AggCounter Elasticsearch
type AdSize struct {
AdType string `json:"adtype"`
ExtCreativeID int64 `json:"ext_creative_id"`
WidthHeightList string `json:"width_height_list"`
}
// AggCounter - Elasticsearch RTB counts aggregation record format, used in send/index to Elasticsearch
type AggCounter struct {
CampaignID int64 `json:"campaignId"`
CreativeID int64 `json:"creativeId"`
AdSize AdSize `json:"adsize"`
AdType string `json:"adtype"`
Exchange string `json:"exchange"`
Interval string `json:"interval"`
Region string `json:"region"`
Timestamp time.Time `json:"timestamp"`
DbTimestamp time.Time `json:"dbTimestamp"`
Bids int `json:"bids"`
Wins int `json:"wins"`
Pixels int `json:"pixels"`
Clicks int `json:"clicks"`
Cost float64 `json:"cost"`
BidIDs []string `json:"bidIds"`
WinIDs []string `json:"winIds"`
PixelIDs []string `json:"pixelIds"`
ClickIDs []string `json:"clickIds"`
}
// DomainCounter - Elasticsearch domain aggregation record format, used in send/index to Elasticsearch
type DomainCounter struct {
CampaignID int64 `json:"campaignId"`
Domain string `json:"domain"`
Interval string `json:"interval"`
ClientID int64 `json:"clientId"`
Region string `json:"region"`
Timestamp time.Time `json:"timestamp"`
DbTimestamp time.Time `json:"dbTimestamp"`
Wins int `json:"wins"`
Pixels int `json:"pixels"`
Clicks int `json:"clicks"`
Cost float64 `json:"cost"`
}
func writeElastic(elasticSearchAggregationURL string, elasticSearchAggregationIndex string, elasticSearchAggregationDomainIndex string,
allkeys *map[string]struct{}, allkeysDom *map[string]struct{}) {
log1 := logger.GetLogger("writeElastic")
client, err := elastic.NewSimpleClient(elastic.SetURL(elasticSearchAggregationURL)) // US
if err != nil {
fmt.Println(err.Error())
return
}
defer client.Stop()
ctx := context.Background()
for k := range *allkeys {
aggBids.Lock(k)
aggWins.Lock(k)
aggClicks.Lock(k)
aggPixels.Lock(k)
aggCosts.Lock(k)
log1.Info(fmt.Sprintf("Writing entry key %s.", k))
s := strings.Split(k, "_")
campaignID, _ := strconv.ParseInt(s[0], 10, 64)
creativeID, _ := strconv.ParseInt(s[1], 10, 64)
exchange := s[2]
intervalStr := s[3]
//intervalTs := s[4]
adType := s[5]
campaignRec := CampaignFields{}
campaignRec = dbCampaignRecords.findID(campaignID)
found := false
var widthHeightList string
var extCreativeID int64
if adType != "video" { // Confirm or check this is a banner. If unknown, check if banner first.
_, found = dbBannerRecords.findID(campaignID, creativeID)
if found {
adType = "banner"
log1.Info("Found banner.")
}
}
if !found { // Not found or adtype is video
_, found = dbBannerVideoRecords.findID(campaignID, creativeID)
if !found && adType == "video" { // Check condition if a banner was mislabeled as video (not found in videos db)
log1.Debug("Not Video. Checking if banner.")
_, found = dbBannerRecords.findID(campaignID, creativeID)
if found {
log1.Debug("Found as banner.")
adType = "banner"
} else {
// log this with campId and creatID
log1.Debug("Not Found in banners. Set to Unknown.")
adType = "unknown"
}
} else {
log1.Debug("adType is video.")
}
}
var intervalTime time.Time
if aggBids[k].count > 0 {
intervalTime = aggBids[k].intervalTm
} else if aggWins[k].count > 0 {
intervalTime = aggWins[k].intervalTm
} else if aggPixels[k].count > 0 {
intervalTime = aggPixels[k].intervalTm
} else if aggClicks[k].count > 0 {
intervalTime = aggClicks[k].intervalTm
} else {
intervalTime = time.Now()
}
adsize := AdSize{
AdType: adType,
ExtCreativeID: extCreativeID,
WidthHeightList: widthHeightList,
}
// Index a elasticsearch record (using JSON serialization)
now := time.Now().UTC()
aggrec := AggCounter{
CampaignID: campaignID,
CreativeID: creativeID,
AdType: adType,
AdSize: adsize,
Exchange: exchange,
Interval: intervalStr,
Region: campaignRec.Regions.String,
Timestamp: intervalTime,
DbTimestamp: now,
Bids: aggBids[k].count,
Wins: aggWins[k].count,
Pixels: aggPixels[k].count,
Clicks: aggClicks[k].count,
Cost: aggCosts[k].sumCost,
}
if *enableAggArraySend || *debug {
aggrec.BidIDs = aggBids[k].ids
aggrec.WinIDs = aggWins[k].ids
aggrec.PixelIDs = aggPixels[k].ids
aggrec.ClickIDs = aggClicks[k].ids
}
indexDay := fmt.Sprintf("%s-%d.%02d.%02d", elasticSearchAggregationIndex, now.Year(), now.Month(), now.Day())
recID := strings.Join(s[0:5], "_") + "_" + adType + "_" + fmt.Sprintf("%d", now.UnixNano())
log1.Info(fmt.Sprintf("Agg record: %v.", aggrec))
if !disableElasticSearchSend {
put1, err := client.Index().
Index(indexDay).
Type("campaign_perf").
Id(recID).
BodyJson(aggrec).
Do(ctx)
if err != nil {
// Handle error
fmt.Println(err.Error())
continue
}
log1.Info(fmt.Sprintf("Indexed agg record %s to index %s, %v.", put1.Id, put1.Index, aggrec))
} else {
log1.Info(fmt.Sprintf("Indexed agg record %s to index %s, %v.", recID, indexDay, aggrec))
}
// variables should be concurrency safe since date stamped keys delete old keys
delete(aggBids, k)
delete(aggWins, k)
delete(aggPixels, k)
delete(aggClicks, k)
delete(aggCosts, k)
aggBids.Unlock(k)
aggWins.Unlock(k)
aggClicks.Unlock(k)
aggPixels.Unlock(k)
aggCosts.Unlock(k)
}
////////////////
for k := range *allkeysDom {
aggWinsDom.Lock(k)
aggClicksDom.Lock(k)
aggPixelsDom.Lock(k)
aggCostsDom.Lock(k)
log1.Info(fmt.Sprintf("Writing domain entry key %sn", k))
s := strings.Split(k, "_")
campaignID, _ := strconv.ParseInt(s[0], 10, 64)
intervalStr := s[1]
//intervalTs := s[2]
domain := strings.Join(s[3:], "_") // domain can contain "_". If so, will be re-joined
domain, _ = neturl.PathUnescape(domain)
campaignRec := CampaignFields{}
campaignRec = dbCampaignRecords.findID(campaignID)
// Set time slice interval
var intervalTime time.Time
if aggWinsDom[k].count > 0 {
intervalTime = aggWinsDom[k].intervalTm
} else if aggPixelsDom[k].count > 0 {
intervalTime = aggPixelsDom[k].intervalTm
} else if aggClicksDom[k].count > 0 {
intervalTime = aggClicksDom[k].intervalTm
} else {
intervalTime = time.Now()
}
// Index a elasticsearch record (using JSON serialization)
now := time.Now().UTC()
//extLineId, _ := strconv.ParseInt(campaignRec.ExtLineID, 10, 64)
aggrec := DomainCounter{
CampaignID: campaignID,
Domain: domain,
Interval: intervalStr,
Region: campaignRec.Regions.String,
Timestamp: intervalTime,
DbTimestamp: now,
Wins: aggWinsDom[k].count,
Pixels: aggPixelsDom[k].count,
Clicks: aggClicksDom[k].count,
Cost: aggCostsDom[k].sumCost,
}
indexDay := fmt.Sprintf("%s-%d.%02d.%02d", elasticSearchAggregationDomainIndex, now.Year(), now.Month(), now.Day())
if !disableElasticSearchSend {
put1, err := client.Index().
Index(indexDay).
Type("campaign_domain").
Id(k).
BodyJson(aggrec).
Do(ctx)
if err != nil {
// Handle error
fmt.Println(err.Error())
fmt.Printf("ES structure %v.", aggrec)
continue
}
log1.Info(fmt.Sprintf("Indexed domain agg record %s to index %s, %v.", put1.Id, put1.Index, aggrec))
} else {
log1.Info(fmt.Sprintf("Indexed domain agg record %s to index %s, %v.", k, indexDay, aggrec))
}
// variables should be concurrency safe since date stamped keys delete old keys
delete(aggWinsDom, k)
delete(aggPixelsDom, k)
delete(aggClicksDom, k)
delete(aggCostsDom, k)
aggWinsDom.Unlock(k)
aggClicksDom.Unlock(k)
aggPixelsDom.Unlock(k)
aggCostsDom.Unlock(k)
}
subCounter.Lock()
for k, v := range subCounter.count {
log1.Info(fmt.Sprintf("Number of subscribe messages for %s: %d", k, v))
subCounter.count[k] = 0
}
//subCounter = make(map[string]int)
subCounter.Unlock()
return
}