-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncollect.go
303 lines (272 loc) · 10.1 KB
/
asyncollect.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package datarangers_sdk
/**
* Copyright 2020 Beijing Volcano Engine Technology Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
// SendEventAb
// eventParam : 事件属性
// custom : 用户自定义事件公共属性
// Deprecated instead of SendEventInfo
func SendEventAb(apptype AppType, appid int64, uuid string, abSdkVersion string, eventname string, eventParam map[string]interface{}, custom map[string]interface{}, did ...int64) error {
var abSdkVersionList []string
if abSdkVersion != "" {
abSdkVersionList = []string{abSdkVersion}
}
dmg := getServerSdkEventWithAbMessage(appid, uuid, abSdkVersionList, []string{eventname}, []map[string]interface{}{eventParam}, custom, apptype, did...)
mq.push(dmg)
return nil
}
// SendEvent
// eventParam : 事件属性
// custom : 用户自定义事件公共属性
// Deprecated: instead of SendEventInfo
func SendEvent(appType AppType, appid int64, uuid string, eventname string, eventParam map[string]interface{}, custom map[string]interface{}, did ...int64) error {
return SendEventAb(appType, appid, uuid, "", eventname, eventParam, custom, did...)
}
// SendEvents
// eventParam : 事件属性
// custom : 用户自定义事件公共属性
// Deprecated: instead of SendEventInfos
func SendEvents(appType AppType, appid int64, uuid string, eventnameList []string, eventParamList []map[string]interface{}, custom map[string]interface{}, did ...int64) error {
if len(eventnameList) != len(eventParamList) {
return fmt.Errorf("事件数目与 属性数目对不上")
}
dmg := getServerSdkEventMessage(appid, uuid, eventnameList, eventParamList, custom, appType, did...)
mq.push(dmg)
return nil
}
// SendEventInfo 上报事件信息
// event: 事件信息
// custom: 事件公共属性
func SendEventInfo(appType AppType, appId int64, uuid string, event *EventV3, custom map[string]interface{}) error {
return SendEventInfos(appType, appId, uuid, []*EventV3{event}, custom)
}
// SendEventInfoWithItem 上报携带item的事件
// event: 事件信息
// custom: 事件公共属性
// itemList: item 信息
func SendEventInfoWithItem(appType AppType, appId int64, uuid string, event *EventV3, custom map[string]interface{}, itemList []*Item) error {
generateItem(event.Params, itemList)
return SendEventInfos(appType, appId, uuid, []*EventV3{event}, custom)
}
// SendEventInfos 上报多个事件
// events: 事件信息
// custom: 事件公共属性
func SendEventInfos(appType AppType, appId int64, uuid string, events []*EventV3, custom map[string]interface{}) error {
hd := &Header{
Aid: &appId,
Custom: custom,
UserUniqueId: &uuid,
}
return SendEventsWithHeader(appType, appId, hd, events)
}
// SendEventWithHeader 使用header上报事件
// header: 事件的header
// event: 事件信息
func SendEventWithHeader(appType AppType, appId int64, hd *Header, event *EventV3) error {
return SendEventsWithHeader(appType, appId, hd, []*EventV3{event})
}
func SendEventsWithHeader(appType AppType, appId int64, hd *Header, events []*EventV3) error {
dmg := getEventsWithHeader(appId, appType, hd, events)
mq.push(dmg)
return nil
}
// SendProfile
// profileAction :用户公共属性操作类型
// profileParam : 用户公共属性
func SendProfile(apptype AppType, appid int64, uuid string, profileAction ProfileActionType, profileParam map[string]interface{}, did ...int64) error {
dmg := getServerSdkEventMessage(appid, uuid, []string{string(profileAction)}, []map[string]interface{}{profileParam}, map[string]interface{}{}, apptype, did...)
mq.push(dmg)
return nil
}
// SendItem
// Deprecated: instead of SendEventInfoWithItem
func SendItem(appid int64, uuid string, eventname string, eventParam map[string]interface{}, custom map[string]interface{}, itemList []*Item) error {
generateItem(eventParam, itemList)
dmg := getServerSdkEventMessage(appid, uuid, []string{eventname}, []map[string]interface{}{eventParam}, custom, APP)
mq.push(dmg)
return nil
}
func generateItem(eventParam map[string]interface{}, itemList []*Item) {
var __items = []interface{}{}
itemmap := map[string][]interface{}{}
for _, item := range itemList {
itemIdmap := map[string]interface{}{}
itemIdmap["id"] = item.ItemId
itemmap[*item.ItemName] = append(itemmap[*item.ItemName], itemIdmap)
}
__items = append(__items, itemmap)
eventParam["__items"] = __items
}
// ItemSet 设置item 属性
// itemName: item 名称
// itemParamList: item 属性信息
func ItemSet(appid int64, itemName string, itemParamList []map[string]interface{}) error {
if ok := checkItemParamList(itemName, itemParamList); !ok {
return fmt.Errorf("itemParam Must contains Id &&& id must be string")
}
//TODO 批量set 失效。
batch := []string{}
for i := 0; i < len(itemParamList); i++ {
batch = append(batch, string(ITEM_SET))
}
dmg := getServerSdkEventMessage(appid, "__rangers", batch, itemParamList, map[string]interface{}{}, APP)
dmg.MessageType = MESSAGE_ITEM
mq.push(dmg)
return nil
}
func checkItemParamList(itemName string, itemParamList []map[string]interface{}) bool {
for _, itemMap := range itemParamList {
if id, ok := itemMap["id"]; ok {
if intId, ok := id.(int); ok {
id = strconv.Itoa(intId)
}
if _, ok := id.(string); !ok {
return false
}
itemMap["item_id"] = id
itemMap["item_name"] = itemName
delete(itemMap, "id")
} else {
return false
}
}
return true
}
func ItemUnset(appid int64, itemName string, id string, removeKeyList []string) error {
itemParam := map[string]interface{}{}
itemParam["item_name"] = itemName
itemParam["item_id"] = id
for _, key := range removeKeyList {
itemParam[key] = 1
}
dmg := getServerSdkEventMessage(appid, "__rangers", []string{string(ITEM_UNSET)}, []map[string]interface{}{itemParam}, map[string]interface{}{}, APP)
dmg.MessageType = MESSAGE_ITEM
mq.push(dmg)
return nil
}
func ProfileSet(apptype AppType, appid int64, uuid string, profileParam map[string]interface{}, did ...int64) error {
dmg := getServerSdkEventMessage(appid, uuid, []string{string(SET)}, []map[string]interface{}{profileParam}, map[string]interface{}{}, apptype, did...)
dmg.MessageType = MESSAGE_USER
mq.push(dmg)
return nil
}
func ProfileSetOnce(apptype AppType, appid int64, uuid string, profileParam map[string]interface{}, did ...int64) error {
dmg := getServerSdkEventMessage(appid, uuid, []string{string(SET_ONCE)}, []map[string]interface{}{profileParam}, map[string]interface{}{}, apptype, did...)
dmg.MessageType = MESSAGE_USER
mq.push(dmg)
return nil
}
func ProfileIncrement(apptype AppType, appid int64, uuid string, profileParam map[string]interface{}, did ...int64) error {
dmg := getServerSdkEventMessage(appid, uuid, []string{string(INCREAMENT)}, []map[string]interface{}{profileParam}, map[string]interface{}{}, apptype, did...)
dmg.MessageType = MESSAGE_USER
mq.push(dmg)
return nil
}
func ProfileUnset(apptype AppType, appid int64, uuid string, profileNameList []string, did ...int64) error {
profileParam := map[string]interface{}{}
for _, name := range profileNameList {
profileParam[name] = 1
}
dmg := getServerSdkEventMessage(appid, uuid, []string{string(UNSET)}, []map[string]interface{}{profileParam}, map[string]interface{}{}, apptype, did...)
dmg.MessageType = MESSAGE_USER
mq.push(dmg)
return nil
}
func ProfileAppend(apptype AppType, appid int64, uuid string, profileParam map[string]interface{}, did ...int64) error {
dmg := getServerSdkEventMessage(appid, uuid, []string{string(APPEND)}, []map[string]interface{}{profileParam}, map[string]interface{}{}, apptype, did...)
dmg.MessageType = MESSAGE_USER
mq.push(dmg)
return nil
}
func SendEventWithDevice(apptype AppType, appid int64, uuid string, eventname string, eventParam map[string]interface{}, custom map[string]interface{}, device deviceType, deviceKey string) error {
dmg := getServerSdkEventMessage(appid, uuid, []string{eventname}, []map[string]interface{}{eventParam}, custom, apptype)
if device == "ANDROID" {
dmg.Header.Openudid = &deviceKey
} else {
dmg.Header.VendorId = &deviceKey
}
mq.push(dmg)
return nil
}
type execPool struct {
max int
tickets chan *ticket
}
type ticket struct {
id int
}
//单例模式
func newExecpool(x int) *execPool {
instance = &execPool{}
instance.max = x
instance.tickets = make(chan *ticket, instance.max)
for i := 0; i < instance.max; i++ {
instance.tickets <- &ticket{id: i}
}
return instance
}
func (p *execPool) exec() {
for {
t := <-p.tickets
go func() {
p.Send()
p.tickets <- t
}()
}
}
func (p *execPool) handleErr(dmgs []interface{}, sendErr error) {
if confIns.ErrHandler != nil {
err := confIns.ErrHandler(dmgs, sendErr)
if err != nil {
fatal(fmt.Sprintf("call ErrHandler failed, error: %v", err))
}
}
if confIns.ErrHandler == nil || confIns.AlwaysWriteToErrFile {
for _, dmg := range dmgs {
ans, _ := json.Marshal(dmg)
errFileWriter.Println(string(ans))
}
}
}
func (p *execPool) Send() {
if confIns.BatchConfig.Enable {
dmgs, err := p.doSendBatch()
if err != nil {
p.handleErr(dmgs, err)
}
return
}
dmg := mq.pop()
err := appCollector.send(dmg)
if err != nil {
p.handleErr([]interface{}{dmg}, err)
}
}
func (p *execPool) doSend() (interface{}, error) {
if confIns.BatchConfig.Enable {
return p.doSendBatch()
}
dmg := mq.pop()
var err error
err = appCollector.send(dmg)
return dmg, err
}
func (p *execPool) doSendBatch() ([]interface{}, error) {
waitTimeMs := time.Duration(confIns.BatchConfig.WaitTimeMs) * time.Millisecond
dmgs := mq.popBatch(confIns.BatchConfig.Size, waitTimeMs)
if len(dmgs) < 1 {
return dmgs, nil
}
debug(fmt.Sprintf("dmgs size: %d", len(dmgs)))
err := appCollector.sendBatch(dmgs)
return dmgs, err
}