jiantaoli 4 years ago
parent
commit
c8d1c3d81d
5 changed files with 267 additions and 152 deletions
  1. 168 106
      ads_handler.go
  2. 25 40
      adslib/addata/custom.go
  3. 4 1
      adslib/addata/xiaomi.go
  4. 54 0
      adslib/graylog/graylog.go
  5. 16 5
      adslib/redis_data/redis_data.go

+ 168 - 106
ads_handler.go

@@ -25,9 +25,10 @@ import (
25
 var SECERET_KEY = "%videoopen%!@#$%"
25
 var SECERET_KEY = "%videoopen%!@#$%"
26
 
26
 
27
 type Response struct {
27
 type Response struct {
28
-	Result int `json:"result"`
29
-	Msg string`json:"msg"`
30
-	AdsResult string`json:"ads_result"`
28
+	Result    int    `json:"result"`
29
+	Msg       string `json:"msg"`
30
+	AdsResult string `json:"ads_result"`
31
+	addata.AdData
31
 }
32
 }
32
 
33
 
33
 func adsHandler(c *gin.Context) {
34
 func adsHandler(c *gin.Context) {
@@ -62,7 +63,7 @@ func adsHandler(c *gin.Context) {
62
 		ip = request.ReqSourceIp
63
 		ip = request.ReqSourceIp
63
 	}
64
 	}
64
 
65
 
65
-	if ip=="" {
66
+	if ip == "" {
66
 		ip = c.GetHeader("X-Forwarded-For")
67
 		ip = c.GetHeader("X-Forwarded-For")
67
 	}
68
 	}
68
 
69
 
@@ -85,24 +86,24 @@ func adsHandler(c *gin.Context) {
85
 	// 上報
86
 	// 上報
86
 	if request.NewAdsFlag == 1 {
87
 	if request.NewAdsFlag == 1 {
87
 		grayLogData := struct {
88
 		grayLogData := struct {
88
-			Ip string
89
-			Imei string
90
-			Model string
91
-			NetworkType int `json:"network_type"`
92
-			Province string
93
-			City string
94
-			ScreeSize string `json:"screen_size"`
95
-			Ua string
96
-			Brand string
97
-			Androidid string `json:"android_id"`
89
+			Ip           string
90
+			Imei         string
91
+			Model        string
92
+			NetworkType  int `json:"network_type"`
93
+			Province     string
94
+			City         string
95
+			ScreeSize    string `json:"screen_size"`
96
+			Ua           string
97
+			Brand        string
98
+			Androidid    string `json:"android_id"`
98
 			ShortMessage string `json:"short_message"`
99
 			ShortMessage string `json:"short_message"`
99
-			ReqSource string `json:"req_source"`
100
+			ReqSource    string `json:"req_source"`
100
 		}{
101
 		}{
101
 			Ip:           ip,
102
 			Ip:           ip,
102
 			Imei:         request.Imei,
103
 			Imei:         request.Imei,
103
 			Model:        request.Model,
104
 			Model:        request.Model,
104
 			NetworkType:  request.NetworkType,
105
 			NetworkType:  request.NetworkType,
105
-			Province:      ipInfo.Province,
106
+			Province:     ipInfo.Province,
106
 			City:         ipInfo.City,
107
 			City:         ipInfo.City,
107
 			ScreeSize:    request.ScreenSize,
108
 			ScreeSize:    request.ScreenSize,
108
 			Ua:           request.UaClient,
109
 			Ua:           request.UaClient,
@@ -122,16 +123,16 @@ func adsHandler(c *gin.Context) {
122
 	}
123
 	}
123
 
124
 
124
 	// 是否是安卓
125
 	// 是否是安卓
125
-	if request.Imei =="" {
126
+	if request.Imei == "" {
126
 		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_IOS", cityCode)
127
 		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_IOS", cityCode)
127
-		graylog.LogApi("ios_device", request.Idfa,"", request.ReqSource)
128
+		graylog.LogApi("ios_device", request.Idfa, "", request.ReqSource)
128
 	} else {
129
 	} else {
129
-		device.SetAdsTagLog(advertiser, request.ReqSource,"DS_ANDRIOID", cityCode)
130
+		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_ANDRIOID", cityCode)
130
 	}
131
 	}
131
 
132
 
132
 	freqControlInterval := 600
133
 	freqControlInterval := 600
133
 	// 深圳,东莞强行用600秒控制, 其他地区从配置读取
134
 	// 深圳,东莞强行用600秒控制, 其他地区从配置读取
134
-	if strings.Index(ipInfo.City, "深圳")== -1 && strings.Index(ipInfo.City, "东莞")==-1 {
135
+	if strings.Index(ipInfo.City, "深圳") == -1 && strings.Index(ipInfo.City, "东莞") == -1 {
135
 		// 频率控制
136
 		// 频率控制
136
 		freqControlConf, err := redis_data.GetFreqCrontolConf(request.ReqSource)
137
 		freqControlConf, err := redis_data.GetFreqCrontolConf(request.ReqSource)
137
 		if err != nil {
138
 		if err != nil {
@@ -153,13 +154,13 @@ func adsHandler(c *gin.Context) {
153
 	}
154
 	}
154
 
155
 
155
 	needControl := false
156
 	needControl := false
156
-	if request.ReqSource != "wzb_h5"  && lastReqTime != 0  && time.Now().Unix() - lastReqTime < int64(freqControlInterval) {
157
+	if request.ReqSource != "wzb_h5" && lastReqTime != 0 && time.Now().Unix()-lastReqTime < int64(freqControlInterval) {
157
 		// 需要凭空
158
 		// 需要凭空
158
 		needControl = true
159
 		needControl = true
159
-		device.SetAdsTagLog(advertiser, request.ReqSource,"DS_FREQ_0", cityCode)
160
+		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_FREQ_0", cityCode)
160
 	} else {
161
 	} else {
161
 		device.SetIpReqTime(ip, time.Now().Unix())
162
 		device.SetIpReqTime(ip, time.Now().Unix())
162
-		device.SetAdsTagLog(advertiser, request.ReqSource,"DS_FREQ_1", cityCode)
163
+		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_FREQ_1", cityCode)
163
 	}
164
 	}
164
 
165
 
165
 	//### 检查是否是ip黑名单
166
 	//### 检查是否是ip黑名单
@@ -169,12 +170,12 @@ func adsHandler(c *gin.Context) {
169
 		return
170
 		return
170
 	}
171
 	}
171
 	if isIpBlack {
172
 	if isIpBlack {
172
-		graylog.LogApi("black_ip_list", ip,"", request.ReqSource)
173
-		device.SetAdsTagLog(advertiser, request.ReqSource,"DS_BLACK_IP", cityCode)
173
+		graylog.LogApi("black_ip_list", ip, "", request.ReqSource)
174
+		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IP", cityCode)
174
 	}
175
 	}
175
 
176
 
176
 	// 获取渠道的黑白性
177
 	// 获取渠道的黑白性
177
-	reqChannelFlag, err := redis_data.GetChannelFlag(request.ReqSource,"ads_req")
178
+	reqChannelFlag, err := redis_data.GetChannelFlag(request.ReqSource, "ads_req")
178
 	if err != nil {
179
 	if err != nil {
179
 		c.String(404, "get req channel flag failed")
180
 		c.String(404, "get req channel flag failed")
180
 		return
181
 		return
@@ -182,12 +183,12 @@ func adsHandler(c *gin.Context) {
182
 	flowWeight := reqChannelFlag.Weigth
183
 	flowWeight := reqChannelFlag.Weigth
183
 	flowRandomNum := rand.Intn(100)
184
 	flowRandomNum := rand.Intn(100)
184
 
185
 
185
-	flowFlag := 0  // 有效的百分比
186
-	if flowRandomNum <= flowWeight{
186
+	flowFlag := 0 // 有效的百分比
187
+	if flowRandomNum <= flowWeight {
187
 		flowFlag = 1
188
 		flowFlag = 1
188
 	}
189
 	}
189
 	if reqChannelFlag.ChannelFlag == 1 {
190
 	if reqChannelFlag.ChannelFlag == 1 {
190
-		device.SetAdsTagLog(advertiser, request.ReqSource,fmt.Sprintf("DS_REQ_SOURCE_{%d}", reqChannelFlag.ChannelFlag), cityCode)
191
+		device.SetAdsTagLog(advertiser, request.ReqSource, fmt.Sprintf("DS_REQ_SOURCE_{%d}", reqChannelFlag.ChannelFlag), cityCode)
191
 	}
192
 	}
192
 
193
 
193
 	// 替换成小米的设备
194
 	// 替换成小米的设备
@@ -200,10 +201,10 @@ func adsHandler(c *gin.Context) {
200
 	// 检查是否是小米的设备
201
 	// 检查是否是小米的设备
201
 	specialDeviceNum := 0
202
 	specialDeviceNum := 0
202
 	if request.ReqSource == "wzb_h5" {
203
 	if request.ReqSource == "wzb_h5" {
203
-		specialDeviceNum = 6  // wzb_h5渠道只綁定一个imei, 具体愿意不知道
204
+		specialDeviceNum = 6 // wzb_h5渠道只綁定一个imei, 具体愿意不知道
204
 	}
205
 	}
205
 	if request.ReqSource == "moyuntv" || (!request.IsMiDevice() &&
206
 	if request.ReqSource == "moyuntv" || (!request.IsMiDevice() &&
206
-		(!isIpBlack &&  !needControl && reqChannelFlag.ChannelFlag ==1) && flowFlag == 1) {
207
+		(!isIpBlack && !needControl && reqChannelFlag.ChannelFlag == 1) && flowFlag == 1) {
207
 		randomNum := 3
208
 		randomNum := 3
208
 		deviceConf, realRedisKey, err := device.GetMiDeviceConf(ip, randomNum, specialDeviceNum)
209
 		deviceConf, realRedisKey, err := device.GetMiDeviceConf(ip, randomNum, specialDeviceNum)
209
 		if err != nil {
210
 		if err != nil {
@@ -257,7 +258,7 @@ func adsHandler(c *gin.Context) {
257
 					NewImei      string `json:"new_imei"`
258
 					NewImei      string `json:"new_imei"`
258
 					IP           string `json:"ip"`
259
 					IP           string `json:"ip"`
259
 					City         string `json:"city"`
260
 					City         string `json:"city"`
260
-					CityCode     int `json:"city_code"`
261
+					CityCode     int    `json:"city_code"`
261
 					Province     string
262
 					Province     string
262
 					HitFlag      int    `json:"hit_flag"`
263
 					HitFlag      int    `json:"hit_flag"`
263
 					LeftCnt      int    `json:"left_cnt"`
264
 					LeftCnt      int    `json:"left_cnt"`
@@ -285,7 +286,7 @@ func adsHandler(c *gin.Context) {
285
 		// 解码ua_client
286
 		// 解码ua_client
286
 		uaClient, _ = url.QueryUnescape(uaClient)
287
 		uaClient, _ = url.QueryUnescape(uaClient)
287
 	} else if request.IsMiDevice() {
288
 	} else if request.IsMiDevice() {
288
-		device.SetAdsTagLog(advertiser, request.ReqSource,"DS_MI_REAL", cityCode)  // 是小米设备
289
+		device.SetAdsTagLog(advertiser, request.ReqSource, "DS_MI_REAL", cityCode) // 是小米设备
289
 		userFlag = 3
290
 		userFlag = 3
290
 		sendPhoneType = 1
291
 		sendPhoneType = 1
291
 	}
292
 	}
@@ -301,7 +302,7 @@ func adsHandler(c *gin.Context) {
301
 		if replaceFlag == 1 {
302
 		if replaceFlag == 1 {
302
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI_REPLACE", cityCode)
303
 			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI_REPLACE", cityCode)
303
 		} else {
304
 		} else {
304
-			device.SetAdsTagLog(advertiser, request.ReqSource,"DS_BLACK_IMEI", cityCode)
305
+			device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI", cityCode)
305
 		}
306
 		}
306
 	}
307
 	}
307
 
308
 
@@ -339,13 +340,12 @@ func adsHandler(c *gin.Context) {
339
 	}
340
 	}
340
 
341
 
341
 	dspInfo.RealMd5Imei = md5Imei
342
 	dspInfo.RealMd5Imei = md5Imei
342
-	//response = {'result': 0, 'msg': 'ok','ads_result':''}
343
 	//ads_item = None
343
 	//ads_item = None
344
 	//xiaomi_response = []
344
 	//xiaomi_response = []
345
 	hour, _ := strconv.Atoi(time.Now().Format("01"))
345
 	hour, _ := strconv.Atoi(time.Now().Format("01"))
346
 
346
 
347
 	canRequest := true
347
 	canRequest := true
348
-	if 0 <= hour && hour <=1 {
348
+	if 0 <= hour && hour <= 1 {
349
 		randNum := rand.Intn(100)
349
 		randNum := rand.Intn(100)
350
 		if randNum > 5 {
350
 		if randNum > 5 {
351
 			canRequest = false
351
 			canRequest = false
@@ -358,16 +358,16 @@ func adsHandler(c *gin.Context) {
358
 	var adDataNum int
358
 	var adDataNum int
359
 
359
 
360
 	// 非频率控制,设备黑名单,ip黑名单,白名单渠道,并且替换了小米设备的
360
 	// 非频率控制,设备黑名单,ip黑名单,白名单渠道,并且替换了小米设备的
361
-	if canRequest && !isBlackImei && reqChannelFlag.ChannelFlag == 1 && flowFlag ==1 && !isIpBlack && advertiser == "xiaomi" && request.ReqSource  != "" && !isBlackImei && !needControl && (replaceFlag==1 || request.IsMiDevice()) {
361
+	if canRequest && !isBlackImei && reqChannelFlag.ChannelFlag == 1 && flowFlag == 1 && !isIpBlack && advertiser == "xiaomi" && request.ReqSource != "" && !isBlackImei && !needControl && (replaceFlag == 1 || request.IsMiDevice()) {
362
 		adData, xiaomiResponse, xiaomiRspAction, adDataNum, err = addata.GetAdsInfos(&dspInfo, advertiser)
362
 		adData, xiaomiResponse, xiaomiRspAction, adDataNum, err = addata.GetAdsInfos(&dspInfo, advertiser)
363
 	}
363
 	}
364
 
364
 
365
 	canMixFlag := 0
365
 	canMixFlag := 0
366
 	xiaomiResponseFlag := 0
366
 	xiaomiResponseFlag := 0
367
 	response := Response{}
367
 	response := Response{}
368
-	if adData != nil &&  len(adData.TargetAddition) > 1 {
368
+	if adData != nil && len(adData.TargetAddition) > 1 {
369
 		realTarget := ""
369
 		realTarget := ""
370
-		if adData.Target != ""{
370
+		if adData.Target != "" {
371
 			md5Skip := utils.Md5(addata.Target)
371
 			md5Skip := utils.Md5(addata.Target)
372
 			conf := adslib.GetConf()
372
 			conf := adslib.GetConf()
373
 			realTarget = conf.Host + fmt.Sprintf("?action=LOADING&req_source=%s&advertiser=video&skip=%s&brand=%s&request_id=%s&skip_other=%s",
373
 			realTarget = conf.Host + fmt.Sprintf("?action=LOADING&req_source=%s&advertiser=video&skip=%s&brand=%s&request_id=%s&skip_other=%s",
@@ -380,16 +380,16 @@ func adsHandler(c *gin.Context) {
380
 		adData.JsOrderId, _ = redis_data.GetMinScriptOrderByAdv(advertiser)
380
 		adData.JsOrderId, _ = redis_data.GetMinScriptOrderByAdv(advertiser)
381
 		adData.UserAgent = uaClient
381
 		adData.UserAgent = uaClient
382
 
382
 
383
-		if dspInfo.ReqSource =="kuxin" && adData.Target != "" {
383
+		if dspInfo.ReqSource == "kuxin" && adData.Target != "" {
384
 			adData.DpReport = fmt.Sprintf("%s?action=DP_CLICK&advertiser=video&req_source=%s", common.get_request_host(None), dspInfo.ReqSource)
384
 			adData.DpReport = fmt.Sprintf("%s?action=DP_CLICK&advertiser=video&req_source=%s", common.get_request_host(None), dspInfo.ReqSource)
385
 			adData.Dp = adslib.GetConf().DpTest
385
 			adData.Dp = adslib.GetConf().DpTest
386
 		}
386
 		}
387
 		if dspInfo.SendPhoneType == 0 {
387
 		if dspInfo.SendPhoneType == 0 {
388
-			device.SetAdsTagLog(advertiser,dspInfo.ReqSource,"ADS_GET", cityCode)  // 广告获取成功
389
-		} else if dspInfo.SendPhoneType==1 {
390
-			device.SetAdsTagLog(advertiser,dspInfo.ReqSource,"ADS_GET_1", cityCode)  // 广告获取成功
388
+			device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_GET", cityCode) // 广告获取成功
389
+		} else if dspInfo.SendPhoneType == 1 {
390
+			device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_GET_1", cityCode) // 广告获取成功
391
 		}
391
 		}
392
-		device.SetAdsTagLog(advertiser, dspInfo.ReqSource, fmt.Sprintf("ADS_USER_%d", userFlag), cityCode)  //  广告类型,缓存,新用户,小米手机
392
+		device.SetAdsTagLog(advertiser, dspInfo.ReqSource, fmt.Sprintf("ADS_USER_%d", userFlag), cityCode) //  广告类型,缓存,新用户,小米手机
393
 		// 判断这个渠道是否要去融合和融合的流量占比
393
 		// 判断这个渠道是否要去融合和融合的流量占比
394
 		mixChannelFlag, err := redis_data.GetChannelFlag(dspInfo.ReqSource, "ads_mix")
394
 		mixChannelFlag, err := redis_data.GetChannelFlag(dspInfo.ReqSource, "ads_mix")
395
 		if err != nil {
395
 		if err != nil {
@@ -402,29 +402,29 @@ func adsHandler(c *gin.Context) {
402
 		if flowRandomNum > mixChannelFlag.Weigth {
402
 		if flowRandomNum > mixChannelFlag.Weigth {
403
 			isOverFlow = true
403
 			isOverFlow = true
404
 		}
404
 		}
405
-		if mixChannelFlag.ChannelFlag !=1 || isOverFlow {
405
+		if mixChannelFlag.ChannelFlag != 1 || isOverFlow {
406
 			//extra_infos = []
406
 			//extra_infos = []
407
 		}
407
 		}
408
 
408
 
409
 		// 组装返回去的action
409
 		// 组装返回去的action
410
-		serverActionResponse := map[string]int{"server_view":0,"server_click":0,"server_close":0,"server_video_finish":0,"server_video_timer":0}
410
+		serverActionResponse := map[string]int{"server_view": 0, "server_click": 0, "server_close": 0, "server_video_finish": 0, "server_video_timer": 0}
411
 		for _, targetInfo := range adData.TargetAddition {
411
 		for _, targetInfo := range adData.TargetAddition {
412
 			if targetInfo.Type == "VIEW" {
412
 			if targetInfo.Type == "VIEW" {
413
-				serverActionResponse["server_view"]=1
414
-				redis_data.SetReqSourceView(advertiser, dspInfo.ReqSource,"xiafa")
413
+				serverActionResponse["server_view"] = 1
414
+				redis_data.SetReqSourceView(advertiser, dspInfo.ReqSource, "xiafa")
415
 			} else if targetInfo.Type == "CLICK" {
415
 			} else if targetInfo.Type == "CLICK" {
416
-				serverActionResponse["server_click"]=1
417
-			} else if targetInfo.Type=="CLOSE" {
418
-				serverActionResponse["server_close"]=1
419
-			} else if targetInfo.Type=="VIDEO_FINISH" {
420
-				serverActionResponse["server_video_finish"]=1
421
-			} else if targetInfo.Type =="VIDEO_TIMER" {
422
-				serverActionResponse["server_video_timer"]=1
416
+				serverActionResponse["server_click"] = 1
417
+			} else if targetInfo.Type == "CLOSE" {
418
+				serverActionResponse["server_close"] = 1
419
+			} else if targetInfo.Type == "VIDEO_FINISH" {
420
+				serverActionResponse["server_video_finish"] = 1
421
+			} else if targetInfo.Type == "VIDEO_TIMER" {
422
+				serverActionResponse["server_video_timer"] = 1
423
 			}
423
 			}
424
 		}
424
 		}
425
 
425
 
426
 		// 增加跟随订单
426
 		// 增加跟随订单
427
-		if len(adData.TargetAddition) == 2  && serverActionResponse["server_video_finish"] == 1 && (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku") {
427
+		if len(adData.TargetAddition) == 2 && serverActionResponse["server_video_finish"] == 1 && (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku") {
428
 			adData, err = addata.CombineOrderBy(adData, advertiser, &dspInfo)
428
 			adData, err = addata.CombineOrderBy(adData, advertiser, &dspInfo)
429
 			if err != nil {
429
 			if err != nil {
430
 				return err
430
 				return err
@@ -433,14 +433,14 @@ func adsHandler(c *gin.Context) {
433
 
433
 
434
 		//# 检查最后是否有click
434
 		//# 检查最后是否有click
435
 		//last_target_addition_infos = ads_item_new.get('target_addition',[])
435
 		//last_target_addition_infos = ads_item_new.get('target_addition',[])
436
-		for _, targetAddition  := range adData.TargetAddition {
437
-			if targetAddition.Type =="CLICK" || targetAddition.Type == "VIDEO_TIMER" {
436
+		for _, targetAddition := range adData.TargetAddition {
437
+			if targetAddition.Type == "CLICK" || targetAddition.Type == "VIDEO_TIMER" {
438
 				canMixFlag = 0
438
 				canMixFlag = 0
439
 				break
439
 				break
440
 			}
440
 			}
441
 		}
441
 		}
442
 
442
 
443
-		if request.NewAdsFlag==0 {
443
+		if request.NewAdsFlag == 0 {
444
 			//response.update(ads_item_new)
444
 			//response.update(ads_item_new)
445
 		} else {
445
 		} else {
446
 			jsonBytes, err := json.Marshal(*adData)
446
 			jsonBytes, err := json.Marshal(*adData)
@@ -452,22 +452,22 @@ func adsHandler(c *gin.Context) {
452
 		}
452
 		}
453
 
453
 
454
 		hour, _ := strconv.Atoi(time.Now().Format("01"))
454
 		hour, _ := strconv.Atoi(time.Now().Format("01"))
455
-		if 0 <= hour && hour <=23 && replaceFlag ==1 {
455
+		if 0 <= hour && hour <= 23 && replaceFlag == 1 {
456
 			randomNum := utils.Hash(imei) % 10000
456
 			randomNum := utils.Hash(imei) % 10000
457
 			if randomNum < 3000 {
457
 			if randomNum < 3000 {
458
 				//add_beanstalk_response.BeanstalkHandle().add_ads_response_info(md5_imei,ads_ip,web.utf8(province),web.utf8(city),ads_item_new,dsp_city_code,req_source,real_imei,xiaomi_response,xiaomi_action,server_action_response,dsp_info.request_id,xiaomi_ads_data_num)
458
 				//add_beanstalk_response.BeanstalkHandle().add_ads_response_info(md5_imei,ads_ip,web.utf8(province),web.utf8(city),ads_item_new,dsp_city_code,req_source,real_imei,xiaomi_response,xiaomi_action,server_action_response,dsp_info.request_id,xiaomi_ads_data_num)
459
 			}
459
 			}
460
 		}
460
 		}
461
 	}
461
 	}
462
-	if canMixFlag==1 {
462
+	if canMixFlag == 1 {
463
 		response.Result = 2
463
 		response.Result = 2
464
 		response.Msg = "no ads"
464
 		response.Msg = "no ads"
465
-		if xiaomiResponseFlag==1 {
465
+		if xiaomiResponseFlag == 1 {
466
 			response.Result = 0
466
 			response.Result = 0
467
 			response.Msg = "ok"
467
 			response.Msg = "ok"
468
 		}
468
 		}
469
 		// 增加打底广告
469
 		// 增加打底广告
470
-		if (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku")  && !needCotrol ==1 && !isBlackImei {
470
+		if (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku") && !needCotrol == 1 && !isBlackImei {
471
 			shortMessage := "kong"
471
 			shortMessage := "kong"
472
 			// 先跑定投
472
 			// 先跑定投
473
 			customAdData, err := addata.GetCustomAdsInfos(&dspInfo, advertiser, 0, 0, xiaomiResponseFlag)
473
 			customAdData, err := addata.GetCustomAdsInfos(&dspInfo, advertiser, 0, 0, xiaomiResponseFlag)
@@ -481,12 +481,12 @@ func adsHandler(c *gin.Context) {
481
 					return err
481
 					return err
482
 				}
482
 				}
483
 			}
483
 			}
484
-			device.SetAdsTagLog(advertiser, dspInfo.ReqSource,"ADS_ZIYOU_GET", cityCode)  // 广告获取成功
484
+			device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_ZIYOU_GET", cityCode) // 广告获取成功
485
 			if customAdData != nil {
485
 			if customAdData != nil {
486
 				shortMessage = "ads_tt_thirds"
486
 				shortMessage = "ads_tt_thirds"
487
 			}
487
 			}
488
 
488
 
489
-			if customAdData != nil && (shortMessage ==  "ads_vast_response" || shortMessage == "ads_tt_thirds") {
489
+			if customAdData != nil && (shortMessage == "ads_vast_response" || shortMessage == "ads_tt_thirds") {
490
 				//response_data_report = {}
490
 				//response_data_report = {}
491
 				//response_data_report['ip'] = ads_ip
491
 				//response_data_report['ip'] = ads_ip
492
 				//response_data_report['short_message'] =  short_message
492
 				//response_data_report['short_message'] =  short_message
@@ -496,58 +496,120 @@ func adsHandler(c *gin.Context) {
496
 				//response_data_report['advertiser']=advertiser
496
 				//response_data_report['advertiser']=advertiser
497
 				//log.send_to_graylog_tornado(gelf_data=response_data_report)
497
 				//log.send_to_graylog_tornado(gelf_data=response_data_report)
498
 
498
 
499
-				adDataBytes,  err :=
499
+				adDataBytes, err := json.Marshal(*adData)
500
+				if err != nil {
501
+					fmt.Sprintf("marshal addata failed: %s\n", err)
502
+				}
500
 				grayLogData := struct {
503
 				grayLogData := struct {
501
-					Ip string
504
+					Ip           string
502
 					ShortMessage string `json:"short_message"`
505
 					ShortMessage string `json:"short_message"`
503
-					ReqSource string `json:"req_source"`
506
+					ReqSource    string `json:"req_source"`
504
 					ResponseVast string `json:"response_vast"`
507
 					ResponseVast string `json:"response_vast"`
505
-					OrderName string `json:"order_name"`
506
-					Advertiser string `json:"advertiser"`
508
+					OrderName    string `json:"order_name"`
509
+					Advertiser   string `json:"advertiser"`
507
 				}{
510
 				}{
508
 					Ip:           ip,
511
 					Ip:           ip,
509
 					ShortMessage: "ads_api_log",
512
 					ShortMessage: "ads_api_log",
510
 					ReqSource:    "zhiku",
513
 					ReqSource:    "zhiku",
511
-					ResponseVast: ""
512
-					OrderName: customAdData.OrderName,
513
-					Advertiser:  advertiser,
514
+					ResponseVast: string(adDataBytes),
515
+					OrderName:    customAdData.OrderName,
516
+					Advertiser:   advertiser,
517
+				}
518
+
519
+				graylog.Log(grayLogData)
520
+			}
521
+			if shortMessage == "ads_tt_thirds" {
522
+				// 组装最后的信息
523
+				lastAdData := addata.AdData{}
524
+				rspTargetAddition := make([]addata.AdAction, 0, 50)
525
+				//  当小米有填充的时候
526
+				if len(response.TargetAddition) != 0 && len(customAdData.TargetAddition) != 0 {
527
+					rspTargetAddition = response.TargetAddition
528
+					lastAdData.UserAgent = response.UserAgent
529
+				}
530
+				//  当只有view的时候
531
+				if len(customAdData.TargetAddition) == 1 {
532
+					for _, url := range customAdData.TargetAddition[0].Urls {
533
+						rspTargetAddition[0].Urls = append(rspTargetAddition[0].Urls, url)
534
+					}
535
+				} else {
536
+					// 当自由订单有view和click的时候
537
+					for _, url := range customAdData.TargetAddition[0].Urls {
538
+						rspTargetAddition[0].Urls = append(rspTargetAddition[0].Urls, url)
539
+					}
540
+
541
+					for _, url := range customAdData.TargetAddition[1].Urls {
542
+						rspTargetAddition[1].Urls = append(rspTargetAddition[1].Urls, url)
543
+					}
544
+					rspTargetAddition[1].Type = "CLICK"
545
+				}
546
+				lastAdData.Target = customAdData.Target
547
+				lastAdData.JsOrderId = customAdData.JsOrderId
548
+				lastAdData.TargetAddition = rspTargetAddition
549
+				lastAdData.DpReport = response.DpReport
550
+				lastAdData.Dp = response.Dp
551
+
552
+				adDataBytes, err := json.Marshal(lastAdData)
553
+				if err != nil {
554
+					fmt.Sprintf("marshal addata failed: %s\n", err)
555
+				}
556
+				grayLogData := struct {
557
+					ShortMessage string `json:"short_message"`
558
+					ResponseVast string `json:"response_vast"`
559
+				}{
560
+					ShortMessage: "third_mix_xiaomi",
561
+					ResponseVast: string(adDataBytes),
514
 				}
562
 				}
515
 
563
 
516
 				graylog.Log(grayLogData)
564
 				graylog.Log(grayLogData)
565
+				customAdData = &lastAdData
566
+			}
567
+			if customAdData != nil {
568
+				device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_ZIYOU_HAVE", cityCode) // 广告获取成功
569
+				if dspInfo.ReqSource == "zhiku" {
570
+					response.Result = 0
571
+					response.Msg = "ok"
572
+
573
+					jsonBytes, err := json.Marshal(customAdData)
574
+					if err != nil {
575
+						c.String(404, err.Error())
576
+						return
577
+					}
578
+					encryptData, _ := encrypt.Encrypt(jsonBytes, []byte(SECERET_KEY))
579
+					response.AdsResult = encryptData
580
+					//self.report_graylog(json.dumps(extra_item))
581
+					rspBytes, err := json.Marshal(response)
582
+					if err != nil {
583
+						c.String(404, err.Error())
584
+						return
585
+					}
586
+					c.String(200, string(rspBytes))
587
+					return
588
+				} else {
589
+					customAdData.Result = 0
590
+					customAdData.Msg = "ok"
591
+					//self.report_graylog(json_response)
592
+					rspBytes, err := json.Marshal(customAdData)
593
+					if err != nil {
594
+						c.String(404, err.Error())
595
+						return
596
+					}
597
+					c.String(200, string(rspBytes))
598
+					return
599
+				}
517
 			}
600
 			}
518
-			//if short_message=="ads_tt_thirds":
519
-			//from thirds import thirds_mix_xiaomi
520
-			//extra_item = thirds_mix_xiaomi.mix_last_infos(extra_item,response)
521
-			//if (extra_item) != None:
522
-			//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_HAVE',dsp_city_code)  # 广告获取成功
523
-			//if req_source in ['zhiku']:
524
-			//response['result']=0
525
-			//response['msg']='ok'
526
-			//response['ads_result']= self.prpcrypt_tools.to_encrypt_content(json.dumps(extra_item))
527
-			//self.report_graylog(json.dumps(extra_item))
528
-			//self.write(json.dumps(response))
529
-			//self.finish()
530
-			//return
531
-			//else:
532
-			//extra_item['result']=0
533
-			//extra_item['msg']="ok"
534
-			//json_response = json.dumps(extra_item)
535
-			//self.report_graylog(json_response)
536
-			//self.write(json_response)
537
-			//self.finish()
538
-			//return
539
 		}
601
 		}
540
 	}
602
 	}
541
-	//redis_tools.set_ads_real_request_num(advertiser)
542
-	//json_response = json.dumps(response)
543
-	//try:
544
-	//if new_ads_flag==0:
545
-	//self.report_graylog(json_response)
546
-	//redis_device.set_ads_tag_log(advertiser,req_source,'DS_REQ',dsp_city_code)  # 所有请求
547
-	//except:
548
-	//pass
549
-	//self.write(json_response)
550
-	//self.finish()
551
-
552
-	c.String(200, "hehe")
603
+	redis_data.SetAdsRealRequestNum(advertiser)
604
+	rspBytes, err := json.Marshal(response)
605
+	if err != nil {
606
+		c.String(404, err.Error())
607
+		return
608
+	}
609
+
610
+	if request.NewAdsFlag == 0 {
611
+		//self.report_graylog(json_response)
612
+	}
613
+	device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "DS_REQ", cityCode) // 所有请求
614
+	c.String(200, string(rspBytes))
553
 }
615
 }

+ 25 - 40
adslib/addata/custom.go

@@ -11,19 +11,6 @@ import (
11
 	"time"
11
 	"time"
12
 )
12
 )
13
 
13
 
14
-type CustomAdData struct {
15
-	Duration       int64         `json:"duration"`
16
-	ImageURL       string        `json:"image_url"`
17
-	JsOrderID      int64        `json:"js_order_id"`
18
-	Msg            int64         `json:"msg"`
19
-	OrderName      string        `json:"order_name"`
20
-	Result         int64         `json:"result"`
21
-	Target         string        `json:"target"`
22
-	TargetAddition []AdAction `json:"target_addition"`
23
-	UserAgent      string        `json:"user_agent"`
24
-	VideoURL       string        `json:"video_url"`
25
-}
26
-
27
 func CombineOrderBy(adData *AdData, advertiser string, dsp *utils.DspParam) (*AdData, error) {
14
 func CombineOrderBy(adData *AdData, advertiser string, dsp *utils.DspParam) (*AdData, error) {
28
 	customAdData, err := GetCustomAdsInfos(dsp, advertiser, 1, 0, 0)
15
 	customAdData, err := GetCustomAdsInfos(dsp, advertiser, 1, 0, 0)
29
 	if err != nil {
16
 	if err != nil {
@@ -59,9 +46,8 @@ func CombineOrderBy(adData *AdData, advertiser string, dsp *utils.DspParam) (*Ad
59
 	return adData, nil
46
 	return adData, nil
60
 }
47
 }
61
 
48
 
62
-
63
 // 获取一个广告
49
 // 获取一个广告
64
-func getOneAds(dsp *utils.DspParam, orderType int, fixFlag int) (*redis_data.AdOrderInfo, error){
50
+func getOneAds(dsp *utils.DspParam, orderType int, fixFlag int) (*redis_data.AdOrderInfo, error) {
65
 	// 取出广告
51
 	// 取出广告
66
 	orders, err := redis_data.GetOrderInfos(dsp, fixFlag)
52
 	orders, err := redis_data.GetOrderInfos(dsp, fixFlag)
67
 	if err != nil {
53
 	if err != nil {
@@ -110,7 +96,7 @@ func getOneAds(dsp *utils.DspParam, orderType int, fixFlag int) (*redis_data.AdO
110
 }
96
 }
111
 
97
 
112
 // 获取投放数量
98
 // 获取投放数量
113
-func getNeedDispatchCount(adData *redis_data.AdOrderInfo) (int,error) {
99
+func getNeedDispatchCount(adData *redis_data.AdOrderInfo) (int, error) {
114
 	beginTime := time.Unix(adData.BeginTime, 0)
100
 	beginTime := time.Unix(adData.BeginTime, 0)
115
 
101
 
116
 	// 获取当前分钟值
102
 	// 获取当前分钟值
@@ -157,7 +143,7 @@ func getNeedDispatchCount(adData *redis_data.AdOrderInfo) (int,error) {
157
 	return needDispatchCount, nil
143
 	return needDispatchCount, nil
158
 }
144
 }
159
 
145
 
160
-func GetCustomAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag int, xiaomiHasFlag int) (*CustomAdData, error){
146
+func GetCustomAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag int, xiaomiHasFlag int) (*AdData, error) {
161
 	order, err := getOneAds(dsp, orderType, fixFlag)
147
 	order, err := getOneAds(dsp, orderType, fixFlag)
162
 	if err != nil {
148
 	if err != nil {
163
 		return nil, err
149
 		return nil, err
@@ -217,7 +203,7 @@ func GetCustomAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fi
217
 	// 获取当前分钟已经完成的下发
203
 	// 获取当前分钟已经完成的下发
218
 	curMinuteFinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
204
 	curMinuteFinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
219
 	if curMinuteFinishedDispatchCnt < curMinuteNeedDispatchCnt {
205
 	if curMinuteFinishedDispatchCnt < curMinuteNeedDispatchCnt {
220
-		data := CustomAdData{
206
+		data := AdData{
221
 			Duration:  5,
207
 			Duration:  5,
222
 			JsOrderID: order.JsOrderID,
208
 			JsOrderID: order.JsOrderID,
223
 			OrderName: order.Title,
209
 			OrderName: order.Title,
@@ -315,14 +301,14 @@ func GetCustomAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fi
315
 
301
 
316
 		var addi *AdAction
302
 		var addi *AdAction
317
 		if orderType == 0 {
303
 		if orderType == 0 {
318
-			addi = genMonitorAction("VIEW",  order.Title, dsp.ReqSource, showUrl)
304
+			addi = genMonitorAction("VIEW", order.Title, dsp.ReqSource, showUrl)
319
 		} else {
305
 		} else {
320
-			addi = genMonitorAction("VIEW",  order.Title, "follow", showUrl)
306
+			addi = genMonitorAction("VIEW", order.Title, "follow", showUrl)
321
 		}
307
 		}
322
 
308
 
323
 		data.TargetAddition = append(data.TargetAddition, *addi)
309
 		data.TargetAddition = append(data.TargetAddition, *addi)
324
 
310
 
325
-		r := rand.Intn( 1000)
311
+		r := rand.Intn(1000)
326
 		clickRate := int(float32(order.ClickKpi) / float32(order.ShowKpi) * 1000)
312
 		clickRate := int(float32(order.ClickKpi) / float32(order.ShowKpi) * 1000)
327
 		if r < clickRate {
313
 		if r < clickRate {
328
 			//下发点击
314
 			//下发点击
@@ -342,25 +328,24 @@ func GetCustomAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fi
342
 	return nil, nil
328
 	return nil, nil
343
 }
329
 }
344
 
330
 
345
-
346
 func genMonitorAction(action string, title string, reqSource string, url string) *AdAction {
331
 func genMonitorAction(action string, title string, reqSource string, url string) *AdAction {
347
-		duration := 5
348
-		if action =="VIDEO_TIMER" {
349
-			duration = 7
350
-		}
332
+	duration := 5
333
+	if action == "VIDEO_TIMER" {
334
+		duration = 7
335
+	}
351
 
336
 
352
-		urlArr := make([]string,0, 50)
353
-		if url != "" {
354
-			urlArr = strings.Split(url, "||")
355
-		}
356
-		adAction := AdAction{
357
-			Type:     action,
358
-			Duration: duration,
359
-			Urls:     urlArr,
360
-		}
361
-		urlHost := adslib.GetConf().HostIos
362
-		actionUrl := fmt.Sprintf("%s?action=%s&advertiser=%s&req_source=%s",
363
-			urlHost, action, reqSource, title)
364
-		adAction.Urls = append(adAction.Urls, actionUrl)
365
-		return &adAction
337
+	urlArr := make([]string, 0, 50)
338
+	if url != "" {
339
+		urlArr = strings.Split(url, "||")
340
+	}
341
+	adAction := AdAction{
342
+		Type:     action,
343
+		Duration: duration,
344
+		Urls:     urlArr,
345
+	}
346
+	urlHost := adslib.GetConf().HostIos
347
+	actionUrl := fmt.Sprintf("%s?action=%s&advertiser=%s&req_source=%s",
348
+		urlHost, action, reqSource, title)
349
+	adAction.Urls = append(adAction.Urls, actionUrl)
350
+	return &adAction
366
 }
351
 }

+ 4 - 1
adslib/addata/xiaomi.go

@@ -67,10 +67,13 @@ type AdData struct {
67
 	ImageUrl       string `json:"image_url"`
67
 	ImageUrl       string `json:"image_url"`
68
 	Duration       int64
68
 	Duration       int64
69
 	VideoUrl       string `json:"video_url"`
69
 	VideoUrl       string `json:"video_url"`
70
-	JsOrderId      int64 `json:"js_order_id"`
70
+	JsOrderId      int64  `json:"js_order_id"`
71
 	UserAgent      string `json:"user_agent"`
71
 	UserAgent      string `json:"user_agent"`
72
 	DpReport       string `json:"dp_report"`
72
 	DpReport       string `json:"dp_report"`
73
 	Dp             string `json:"dp"`
73
 	Dp             string `json:"dp"`
74
+	OrderName      string `json:"order_name"`
75
+	Result         int    `json:"result"`
76
+	Msg            string `json:"msg"`
74
 }
77
 }
75
 
78
 
76
 func getAdsReqUrl(dsp *utils.DspParam) string {
79
 func getAdsReqUrl(dsp *utils.DspParam) string {

+ 54 - 0
adslib/graylog/graylog.go

@@ -1,6 +1,7 @@
1
 package graylog
1
 package graylog
2
 import (
2
 import (
3
 	"encoding/json"
3
 	"encoding/json"
4
+	"github.com/gin-gonic/gin"
4
 	"github.com/robertkowalski/graylog-golang"
5
 	"github.com/robertkowalski/graylog-golang"
5
 )
6
 )
6
 var grayLog  *gelf.Gelf
7
 var grayLog  *gelf.Gelf
@@ -34,3 +35,56 @@ func LogApi(shortMsg string, desc string, extraDesc string, reqSource string) {
34
 	}
35
 	}
35
 	Log(grayLogData)
36
 	Log(grayLogData)
36
 }
37
 }
38
+
39
+// 发送返回日志
40
+func ReportGrayLog(c *gin.Context, uid int, response string, jsonFlag int) {
41
+	path := c.Request.URL.Path
42
+	reqTs := c.Request.Time()
43
+	ip = req.headers.get('X-Forwarded-For',"")
44
+	req_source_ip = req_data.get('ip',[])
45
+	if len(req_source_ip):
46
+	check_req_source_flag = redis_ip.check_req_source(req_data.get('req_source',[])[0])
47
+	if check_req_source_flag:
48
+	ip = req_source_ip[0]
49
+	if ip=="":
50
+	ip = self.request.headers.get("X-Real-IP","")
51
+	if ip=="":
52
+	ip = req.remote_ip
53
+	if ip.find(",") > 0:
54
+	ip = ip.split(",")[0]
55
+	device_id = req_data.get('device_id,', '')
56
+	request = urllib.unquote(urllib.urlencode(req_data, 'utf-8'))
57
+	province,city,city_code = ip_location_helper.get_region_by_ip(ip)
58
+	gelf_data = {}
59
+	gelf_data['short_message'] = 'ads_api_log'
60
+	gelf_data['_user_id'] = user_id
61
+	gelf_data['_device_id'] = device_id
62
+	gelf_data['_ip'] = ip
63
+	gelf_data['_uri'] = path
64
+	gelf_data['_province']=province
65
+	gelf_data['_city']=city
66
+	gelf_data['_city_code']=city_code
67
+	for key,value in req_data.items():
68
+	if str(key) in ['ip']:
69
+	continue
70
+	gelf_data['_{0}'.format(key)] = common.my_str(value[0])
71
+	req_len = 102400
72
+	gelf_data['_request'] = request[0:req_len]
73
+	#json_response = json.dumps(response)
74
+	### 结果不上报了
75
+	json_response = ""
76
+	if len(json_response) < req_len and len(json_response) > 60:
77
+try:
78
+	json_response = json.dumps(
79
+		response, indent=4, sort_keys=True,
80
+		ensure_ascii=False).encode("utf-8")
81
+except:
82
+	send_try_except()
83
+try:
84
+	json_response = json_response.decode("utf-8")
85
+except:
86
+	pass
87
+	gelf_data['_response'] = json_response[0:req_len]
88
+	gelf_data['_response_time'] = round(req_ts * 1000.0, 2)
89
+	send_to_graylog_tornado(gelf_data, req=req, req_data=cur_req_data)
90
+}

+ 16 - 5
adslib/redis_data/redis_data.go

@@ -215,6 +215,18 @@ func SetAdsRequestNum(advertiser string, bannerid int) {
215
 	conn.Do("incr", redisKey)
215
 	conn.Do("incr", redisKey)
216
 }
216
 }
217
 
217
 
218
+func SetAdsRealRequestNum(advertiser string) {
219
+	if advertiser == "" {
220
+		return
221
+	}
222
+
223
+	conn := ads_redis.RedisConn.Get()
224
+	defer conn.Close()
225
+
226
+	redisKey := fmt.Sprintf("adsv2_request_%s", advertiser)
227
+	conn.Do("incr", redisKey)
228
+}
229
+
218
 // 设置skip redis
230
 // 设置skip redis
219
 func SetSkipInfo(md5Skip string, skipUrl string) {
231
 func SetSkipInfo(md5Skip string, skipUrl string) {
220
 	conn := ads_redis.RedisConn.Get()
232
 	conn := ads_redis.RedisConn.Get()
@@ -225,20 +237,20 @@ func SetSkipInfo(md5Skip string, skipUrl string) {
225
 }
237
 }
226
 
238
 
227
 // 通过advertiser获取最小的script_order
239
 // 通过advertiser获取最小的script_order
228
-func GetMinScriptOrderByAdv(advertiser string) (string, error) {
240
+func GetMinScriptOrderByAdv(advertiser string) (int64, error) {
229
 	conn := ads_redis.RedisConn.Get()
241
 	conn := ads_redis.RedisConn.Get()
230
 	defer conn.Close()
242
 	defer conn.Close()
231
 
243
 
232
 	redisKey := "adv_script_" + advertiser
244
 	redisKey := "adv_script_" + advertiser
233
 	rsp, err := conn.Do("GET", redisKey)
245
 	rsp, err := conn.Do("GET", redisKey)
234
 	if err != nil {
246
 	if err != nil {
235
-		return "", err
247
+		return 0, err
236
 	}
248
 	}
237
 
249
 
238
 	if rsp == nil {
250
 	if rsp == nil {
239
-		return "", err
251
+		return 0, err
240
 	}
252
 	}
241
-	scriptOrder, _ := redis.String(rsp, err)
253
+	scriptOrder, _ := redis.Int64(rsp, err)
242
 	return scriptOrder, nil
254
 	return scriptOrder, nil
243
 }
255
 }
244
 
256
 
@@ -451,4 +463,3 @@ func GetIosUaImei(ip string) (string, string, error) {
451
 	json.Unmarshal(rspBytes, &uaImeiInfo)
463
 	json.Unmarshal(rspBytes, &uaImeiInfo)
452
 	return uaImeiInfo.Imei, uaImeiInfo.Ua, nil
464
 	return uaImeiInfo.Imei, uaImeiInfo.Ua, nil
453
 }
465
 }
454
-