-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwrite_agg.go
88 lines (82 loc) · 2.28 KB
/
write_agg.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
//
// Write aggregation results to the log
// Replace this with a write to your custom data store
//
package main
import (
"encoding/json"
"fmt"
"time"
)
//
// AggCounter - Counts aggregation record format. set as JSON.
//
type AggCounter struct {
CampaignID int64 `json:"campaignId"`
CreativeID int64 `json:"creativeId"`
Interval string `json:"interval"`
Region string `json:"region"`
Timestamp time.Time `json:"timestamp"`
DbTimestamp time.Time `json:"dbTimestamp"`
Bids int64 `json:"bids"`
Wins int64 `json:"wins"`
Pixels int64 `json:"pixels"`
Clicks int64 `json:"clicks"`
}
//
// Print the aggregation record for the last interval
//
func writeAggregatedRecords(allkeys *map[RecordKey]struct{}) {
log1 := logger.GetLogger("writeAggregatedRecords")
for k := range *allkeys {
log1.Debug(fmt.Sprintf("Writing entry key %v:", k))
// Lock this recordkey's counters
aggBids.Lock(k)
aggWins.Lock(k)
aggClicks.Lock(k)
aggPixels.Lock(k)
campaignID := k.CampaignID
creativeID := k.CreativeID
intervalStr := k.IntervalStr
campaignRec := findCampaign(campaignID, creativeID)
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()
}
// Create a aggregation record in JSON
now := time.Now().UTC()
aggrec := AggCounter{
CampaignID: campaignID,
CreativeID: creativeID,
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,
}
jsonStr, _ := json.Marshal(aggrec)
log1.Info(fmt.Sprintf("Agg record %s", jsonStr))
// Delete the counter object for this recordkey since we've written it
delete(aggBids, k)
delete(aggWins, k)
delete(aggPixels, k)
delete(aggClicks, k)
// Unlock counters
aggBids.Unlock(k)
aggWins.Unlock(k)
aggClicks.Unlock(k)
aggPixels.Unlock(k)
}
return
}