jiantaoli преди 4 години
родител
ревизия
a0bc343123
променени са 7 файла, в които са добавени 246 реда и са изтрити 178 реда
  1. 8 0
      .idea/dictionaries/jiantaoli.xml
  2. 114 92
      ads_handler.go
  3. 81 80
      adslib/addata/custom.go
  4. 1 1
      adslib/addata/xiaomi.go
  5. 25 3
      adslib/redis_data/redis_data.go
  6. 16 1
      adslib/utils/string.go
  7. 1 1
      request.go

+ 8 - 0
.idea/dictionaries/jiantaoli.xml

@@ -0,0 +1,8 @@
1
+<component name="ProjectDictionaryState">
2
+  <dictionary name="jiantaoli">
3
+    <words>
4
+      <w>kuxin</w>
5
+      <w>zhiku</w>
6
+    </words>
7
+  </dictionary>
8
+</component>

+ 114 - 92
ads_handler.go

@@ -1,14 +1,17 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"encoding/json"
4 5
 	"fmt"
5 6
 	"github.com/gin-gonic/gin"
7
+	"hash"
6 8
 	"math/rand"
7 9
 	"miads/adslib"
8 10
 	"miads/adslib/addata"
9 11
 	"miads/adslib/ads_checker"
10 12
 	"miads/adslib/city"
11 13
 	"miads/adslib/device"
14
+	"miads/adslib/encrypt"
12 15
 	"miads/adslib/graylog"
13 16
 	"miads/adslib/ip2region"
14 17
 	"miads/adslib/redis_data"
@@ -19,7 +22,13 @@ import (
19 22
 	"time"
20 23
 )
21 24
 
22
-var DECRYPT_KEY = "%videoopen%!@#$%"
25
+var SECERET_KEY = "%videoopen%!@#$%"
26
+
27
+type Response struct {
28
+	Result int `json:"result"`
29
+	Msg string`json:"msg"`
30
+	AdsResult string`json:"ads_result"`
31
+}
23 32
 
24 33
 func adsHandler(c *gin.Context) {
25 34
 	c.Header("Content-Type", "application/json")
@@ -353,8 +362,9 @@ func adsHandler(c *gin.Context) {
353 362
 		adData, xiaomiResponse, xiaomiRspAction, adDataNum, err = addata.GetAdsInfos(&dspInfo, advertiser)
354 363
 	}
355 364
 
356
-	//can_mix_flag = 1
357
-	//xiaomi_response_flag = 0
365
+	canMixFlag := 0
366
+	xiaomiResponseFlag := 0
367
+	response := Response{}
358 368
 	if adData != nil &&  len(adData.TargetAddition) > 1 {
359 369
 		realTarget := ""
360 370
 		if adData.Target != ""{
@@ -412,101 +422,113 @@ func adsHandler(c *gin.Context) {
412 422
 				serverActionResponse["server_video_timer"]=1
413 423
 			}
414 424
 		}
415
-		//response_report_flag = 1
416 425
 
417 426
 		// 增加跟随订单
418
-		if len(adData.TargetAddition) == 2  && server_action_response["server_video_finish"] == 1 && (dspInfo.ReqSource == "kuxin" or dspInfo.ReqSource == "zhiku") {
419
-			//from thirds import tt_thirds
420
-			//ads_item_new =yield tt_thirds.combine_order_by(ads_item_new,dsp_info)
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)
429
+			if err != nil {
430
+				return err
431
+			}
421 432
 		}
433
+
422 434
 		//# 检查最后是否有click
423 435
 		//last_target_addition_infos = ads_item_new.get('target_addition',[])
424
-		//for last_target_addition_info in last_target_addition_infos:
425
-		//if last_target_addition_info['type']=="CLICK":
426
-		//can_mix_flag = 0
427
-		//break
428
-		//if last_target_addition_info['type']=="VIDEO_TIMER":
429
-		//can_mix_flag = 0
430
-		//break
431
-		//if new_ads_flag==0:
432
-		//response.update(ads_item_new)
433
-		//else:
434
-		//encrypt_ads_item = self.prpcrypt_tools.to_encrypt_content(json.dumps(ads_item_new))
435
-		//response['ads_result']=encrypt_ads_item
436
-		//hour = int(time.strftime("%H",time.localtime(time.time())))
437
-		//if (hour>=0 and hour<=23) and replace_flag==1:
438
-		//random_num = abs(hash(ads_imei))%(10 ** 4)
439
-		//if random_num<3000:
440
-		//response_report_flag = 1
441
-		//else:
442
-		//response_report_flag = 0
443
-		//if response_report_flag==1:
444
-		//try:
445
-		//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)
446
-		//except:
447
-		//log.send_try_except()
436
+		for _, targetAddition  := range adData.TargetAddition {
437
+			if targetAddition.Type =="CLICK" || targetAddition.Type == "VIDEO_TIMER" {
438
+				canMixFlag = 0
439
+				break
440
+			}
441
+		}
442
+
443
+		if request.NewAdsFlag==0 {
444
+			//response.update(ads_item_new)
445
+		} else {
446
+			jsonBytes, err := json.Marshal(*adData)
447
+			if err != nil {
448
+				return err
449
+			}
450
+			encryptData, _ := encrypt.Encrypt(jsonBytes, []byte(SECERET_KEY))
451
+			response.AdsResult = encryptData
452
+		}
453
+
454
+		hour, _ := strconv.Atoi(time.Now().Format("01"))
455
+		if 0 <= hour && hour <=23 && replaceFlag ==1 {
456
+			randomNum := utils.Hash(imei) % 10000
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)
459
+			}
460
+		}
461
+	}
462
+	if canMixFlag==1 {
463
+		response.Result = 2
464
+		response.Msg = "no ads"
465
+		if xiaomiResponseFlag==1 {
466
+			response.Result = 0
467
+			response.Msg = "ok"
468
+		}
469
+		// 增加打底广告
470
+		if (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku")  && !needCotrol ==1 && !isBlackImei {
471
+			shortMessage := "kong"
472
+			// 先跑定投
473
+			customAdData, err := addata.GetCustomAdsInfos(&dspInfo, advertiser, 0, 0, xiaomiResponseFlag)
474
+			if err != nil {
475
+				return err
476
+			}
477
+			// 定投没有就跑其他的
478
+			if customAdData == nil {
479
+				customAdData, err = addata.GetCustomAdsInfos(&dspInfo, advertiser, 0, 1, xiaomiResponseFlag)
480
+				if err != nil {
481
+					return err
482
+				}
483
+			}
484
+			device.SetAdsTagLog(advertiser, dspInfo.ReqSource,"ADS_ZIYOU_GET", cityCode)  // 广告获取成功
485
+			if customAdData != nil {
486
+				shortMessage = "ads_tt_thirds"
487
+			}
488
+			if customAdData != nil && customAdData.OrderName  == "mh_dsp" && xiaomiResponseFlag==0 {
489
+				//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_MH',dsp_city_code)  # 广告获取成功
490
+				//#如果是外部dsp,走这个逻辑
491
+				//from thirds import xiaomi_extra
492
+				//extra_item,ads_item = yield xiaomi_extra.get_ads_infos(dsp_info)
493
+				//short_message = "ads_vast_response"
494
+			}
495
+			//if extra_item and extra_item['order_name']=="xiaodu_dsp" and xiaomi_response_flag==0:
496
+			//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_XIAODU',dsp_city_code)  # 广告获取成功
497
+			//from thirds import xiaodu
498
+			//extra_item,_ = yield xiaodu.get_ads_infos(dsp_info)
499
+			//short_message = "ads_xiaodu_response"
500
+			//if extra_item is not None and short_message in ['ads_xiaodu_response','ads_vast_response','ads_tt_thirds']:
501
+			//response_data_report = {}
502
+			//response_data_report['ip'] = ads_ip
503
+			//response_data_report['short_message'] =  short_message
504
+			//response_data_report['req_source'] = req_source
505
+			//response_data_report['response_vast'] = json.dumps(extra_item)
506
+			//response_data_report['order_name']=order_name
507
+			//response_data_report['advertiser']=advertiser
508
+			//log.send_to_graylog_tornado(gelf_data=response_data_report)
509
+			//if short_message=="ads_tt_thirds":
510
+			//from thirds import thirds_mix_xiaomi
511
+			//extra_item = thirds_mix_xiaomi.mix_last_infos(extra_item,response)
512
+			//if (extra_item) != None:
513
+			//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_HAVE',dsp_city_code)  # 广告获取成功
514
+			//if req_source in ['zhiku']:
515
+			//response['result']=0
516
+			//response['msg']='ok'
517
+			//response['ads_result']= self.prpcrypt_tools.to_encrypt_content(json.dumps(extra_item))
518
+			//self.report_graylog(json.dumps(extra_item))
519
+			//self.write(json.dumps(response))
520
+			//self.finish()
521
+			//return
522
+			//else:
523
+			//extra_item['result']=0
524
+			//extra_item['msg']="ok"
525
+			//json_response = json.dumps(extra_item)
526
+			//self.report_graylog(json_response)
527
+			//self.write(json_response)
528
+			//self.finish()
529
+			//return
530
+		}
448 531
 	}
449
-	//if can_mix_flag==1:
450
-	//response['result'] = 2
451
-	//response['msg'] = 'no ads'
452
-	//if xiaomi_response_flag==1:
453
-	//response['result'] = 0
454
-	//response['msg'] = 'ok'
455
-	//#增加打底广告
456
-	//if req_source in ['kuxin','zhiku'] and freq_contro_flag==1 and device_black_flag==0:
457
-	//short_message = "kong"
458
-	//## 先跑定投
459
-	//from thirds import tt_thirds
460
-	//ads_item,extra_item = yield tt_thirds.get_ads_infos(dsp_info,fix_flag=0,xiaomi_has_flag=xiaomi_response_flag)
461
-	//## 定投没有就跑其他的
462
-	//if not extra_item:
463
-	//from thirds import tt_thirds
464
-	//ads_item,extra_item = yield tt_thirds.get_ads_infos(dsp_info,fix_flag=1,xiaomi_has_flag=xiaomi_response_flag)
465
-	//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_GET',dsp_city_code)  # 广告获取成功
466
-	//if extra_item:
467
-	//short_message = "ads_tt_thirds"
468
-	//order_name = extra_item['order_name']
469
-	//if extra_item and extra_item['order_name'] == 'mh_dsp' and xiaomi_response_flag==0:
470
-	//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_MH',dsp_city_code)  # 广告获取成功
471
-	//#如果是外部dsp,走这个逻辑
472
-	//from thirds import xiaomi_extra
473
-	//extra_item,ads_item = yield xiaomi_extra.get_ads_infos(dsp_info)
474
-	//short_message = "ads_vast_response"
475
-	//if extra_item and extra_item['order_name']=="xiaodu_dsp" and xiaomi_response_flag==0:
476
-	//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_XIAODU',dsp_city_code)  # 广告获取成功
477
-	//from thirds import xiaodu
478
-	//extra_item,_ = yield xiaodu.get_ads_infos(dsp_info)
479
-	//short_message = "ads_xiaodu_response"
480
-	//if extra_item is not None and short_message in ['ads_xiaodu_response','ads_vast_response','ads_tt_thirds']:
481
-	//response_data_report = {}
482
-	//response_data_report['ip'] = ads_ip
483
-	//response_data_report['short_message'] =  short_message
484
-	//response_data_report['req_source'] = req_source
485
-	//response_data_report['response_vast'] = json.dumps(extra_item)
486
-	//response_data_report['order_name']=order_name
487
-	//response_data_report['advertiser']=advertiser
488
-	//log.send_to_graylog_tornado(gelf_data=response_data_report)
489
-	//if short_message=="ads_tt_thirds":
490
-	//from thirds import thirds_mix_xiaomi
491
-	//extra_item = thirds_mix_xiaomi.mix_last_infos(extra_item,response)
492
-	//if (extra_item) != None:
493
-	//redis_device.set_ads_tag_log(advertiser,req_source,'ADS_ZIYOU_HAVE',dsp_city_code)  # 广告获取成功
494
-	//if req_source in ['zhiku']:
495
-	//response['result']=0
496
-	//response['msg']='ok'
497
-	//response['ads_result']= self.prpcrypt_tools.to_encrypt_content(json.dumps(extra_item))
498
-	//self.report_graylog(json.dumps(extra_item))
499
-	//self.write(json.dumps(response))
500
-	//self.finish()
501
-	//return
502
-	//else:
503
-	//extra_item['result']=0
504
-	//extra_item['msg']="ok"
505
-	//json_response = json.dumps(extra_item)
506
-	//self.report_graylog(json_response)
507
-	//self.write(json_response)
508
-	//self.finish()
509
-	//return
510 532
 	//redis_tools.set_ads_real_request_num(advertiser)
511 533
 	//json_response = json.dumps(response)
512 534
 	//try:

+ 81 - 80
adslib/addata/custom.go

@@ -6,7 +6,6 @@ import (
6 6
 	"miads/adslib"
7 7
 	"miads/adslib/redis_data"
8 8
 	"miads/adslib/utils"
9
-	"net/url"
10 9
 	"strconv"
11 10
 	"strings"
12 11
 	"time"
@@ -25,28 +24,39 @@ type CustomAdData struct {
25 24
 	VideoURL       string        `json:"video_url"`
26 25
 }
27 26
 
28
-func CombineOrderBy(adData *AdData, dsp *utils.DspParam) {
29
-	ads_item, extra_item = yield get_ads_infos(dsp, order_type = 1)
30
-	if extra_item == None:
31
-	raise
32
-	gen.Return(infos)
33
-	last_target_addition = infos.get('target_addition', [])
34
-	extra_item_addition = extra_item.get('target_addition', [])
35
-	if len(extra_item_addition) == 0:
36
-	raise
37
-	gen.Return(infos)
38
-	if len(extra_item_addition) == 1:
39
-	last_target_addition[0]['urls'].extend(extra_item_addition[0]['urls'])
40
-	raise
41
-	gen.Return(infos)
42
-	else:
43
-	last_target_addition[0]['urls'].extend(extra_item_addition[0]['urls'])
44
-	last_target_addition[1]['urls'].extend(extra_item_addition[1]['urls'])
45
-	infos['target'] = extra_item['target']
46
-	infos['js_order_id'] = extra_item['js_order_id']
47
-	last_target_addition[1]['type'] = 'CLICK'
48
-	raise
49
-	gen.Return(infos)
27
+func CombineOrderBy(adData *AdData, advertiser string, dsp *utils.DspParam) (*AdData, error) {
28
+	customAdData, err := GetCustomAdsInfos(dsp, advertiser, 1, 0, 0)
29
+	if err != nil {
30
+		return adData, err
31
+	}
32
+	if customAdData == nil {
33
+		return adData, nil
34
+	}
35
+
36
+	if len(customAdData.TargetAddition) == 0 {
37
+		return adData, nil
38
+	}
39
+
40
+	maxExchangeLen := len(customAdData.TargetAddition)
41
+	// 最多替换数量, 不知道为啥是2
42
+	if maxExchangeLen > 2 {
43
+		maxExchangeLen = 2
44
+	}
45
+
46
+	hasClickTarget := false
47
+	for i := 0; i < maxExchangeLen; i++ {
48
+		adData.TargetAddition[i].Urls = customAdData.TargetAddition[i].Urls
49
+		if customAdData.TargetAddition[i].Type == "CLICK" {
50
+			adData.TargetAddition[i].Type = "CLICK"
51
+			hasClickTarget = true
52
+		}
53
+	}
54
+
55
+	if hasClickTarget {
56
+		adData.Target = customAdData.Target
57
+		adData.JsOrderId = customAdData.JsOrderID
58
+	}
59
+	return adData, nil
50 60
 }
51 61
 
52 62
 
@@ -147,28 +157,25 @@ func getNeedDispatchCount(adData *redis_data.AdOrderInfo) (int,error) {
147 157
 	return needDispatchCount, nil
148 158
 }
149 159
 
150
-func GetAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag int, xiaomiHasFlag int) {
151
-	ua = dsp.ua_client
152
-	req_source = dsp.req_source
153
-
154
-	order, err := getOneAds(dsp, order_type, fix_flag)
160
+func GetCustomAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag int, xiaomiHasFlag int) (*CustomAdData, error){
161
+	order, err := getOneAds(dsp, orderType, fixFlag)
155 162
 	if err != nil {
156
-		return err
163
+		return nil, err
157 164
 	}
158 165
 	if order == nil {
159
-		return nil
166
+		return nil, nil
160 167
 	}
161 168
 
162 169
 	if xiaomiHasFlag == 1 {
163 170
 		if strings.Index(order.Title, "_ios") > 0 {
164
-			return nil
171
+			return nil, nil
165 172
 		}
166 173
 	}
167 174
 
168 175
 	// 获取剩余时间内的值
169 176
 	needDispatchCnt, err := getNeedDispatchCount(order)
170 177
 	if err != nil {
171
-		return err
178
+		return nil, err
172 179
 	}
173 180
 
174 181
 	if needDispatchCnt < 1 {
@@ -182,55 +189,49 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag
182 189
 	if err != nil {
183 190
 		return nil, err
184 191
 	}
185
-	finishClickCnt, err := redis_data.GetFinishedDispatchCount(order.OrderID, "click", curTime)
186
-	if err != nil {
187
-		return nil, err
188
-	}
189 192
 
190
-	redis_data.SetPlanDispatchCount(order.OrderID, "show", needDispatchCount, time.Now().Unix())
193
+	fmt.Sprintf("finish show cnt: %d\n", finishShowCnt)
194
+	redis_data.SetPlanDispatchCount(order.OrderID, "show", needDispatchCnt, curTime)
191 195
 
192 196
 	// 计算曲线比例
193 197
 	rate := float32(order.ShowKpi) / float32(needDispatchCnt)
194 198
 
195
-	curMinutes := curTime.Hour() * 60 + curTime.Minute()
196
-	key := "time_" + strconv.Itoa(curMinutes)
199
+	lineValue := 0
197 200
 	if order.LineType == 1 {
198
-		// 定制曲线
199
-		key = fmt.Sprintf("time_%d_%d", order.OrderID, curMinutes)
201
+		// 订单定制曲线
202
+		lineValue, err = redis_data.GetOrderPerMinuteNeedDispatchCnt(order.OrderID, curTime)
203
+	} else {
204
+		lineValue, err = redis_data.GetPerMinuteNeedDispatchCnt(curTime)
200 205
 	}
201
-
202
-	lineValue, err := redis_data.GetPerMinuteNeedDispatchCnt(order.OrderID, curTime)
203 206
 	if err != nil {
204 207
 		return nil, err
205 208
 	}
206 209
 	// 当前分钟需要放出去的数量
207 210
 	curMinuteNeedDispatchCnt := int(float32(lineValue) * rate)
208
-	if dispatchCnt < 1 {
209
-		dispatchCnt = 1
211
+	if curMinuteNeedDispatchCnt < 1 {
212
+		curMinuteNeedDispatchCnt = 1
210 213
 	}
211 214
 
212
-	redis_data.SetOrderPlanDispatchCount(order.OrderID, "show", dispatchCnt, curTime)
215
+	redis_data.SetOrderPlanDispatchCount(order.OrderID, "show", curMinuteNeedDispatchCnt, curTime)
213 216
 
214 217
 	// 获取当前分钟已经完成的下发
215
-	curMinutefinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
216
-
217
-	if curMinutefinishedDispatchCnt < curMinuteNeedDispatchCnt {
218
-
218
+	curMinuteFinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
219
+	if curMinuteFinishedDispatchCnt < curMinuteNeedDispatchCnt {
219 220
 		data := CustomAdData{
220
-			Duration:       5,
221
-			JsOrderID:      order.JsOrderID,
222
-			OrderName:      order.Title,
223
-			UserAgent:      dsp.UaOrigin,
221
+			Duration:  5,
222
+			JsOrderID: order.JsOrderID,
223
+			OrderName: order.Title,
224
+			UserAgent: dsp.UaOrigin,
224 225
 		}
225 226
 
226 227
 		//放量
227 228
 		_, err := redis_data.IncrFinishedDispatchCount(order.OrderID, "show", 1, curTime)
228 229
 		if err != nil {
229
-			return err
230
+			return nil, err
230 231
 		}
231
-		_, err := redis_data.IncrPreMinuteFinishedDispatchCount(order.OrderID, "show", 1, curTime)
232
+		_, err = redis_data.IncrPreMinuteFinishedDispatchCount(order.OrderID, "show", 1, curTime)
232 233
 		if err != nil {
233
-			return err
234
+			return nil, err
234 235
 		}
235 236
 
236 237
 		showUrl := order.ShowURL
@@ -240,7 +241,7 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag
240 241
 		if strings.Index(order.Title, "_ios") != -1 {
241 242
 			iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
242 243
 			if err != nil {
243
-				return err
244
+				return nil, err
244 245
 			}
245 246
 			if iosUa != "" {
246 247
 				data.UserAgent = iosUa
@@ -274,7 +275,7 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag
274 275
 			if r < 40 && xiaomiHasFlag == 0 {
275 276
 				iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
276 277
 				if err != nil {
277
-					return err
278
+					return nil, err
278 279
 				}
279 280
 				if iosUa != "" {
280 281
 					data.UserAgent = iosUa
@@ -290,7 +291,7 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag
290 291
 					clickUrl = strings.ReplaceAll(clickUrl, "__OS__", "1")
291 292
 					targetUrl = strings.ReplaceAll(targetUrl, "__OS__", "1")
292 293
 				}
293
-		} else {
294
+			} else {
294 295
 				// 判断是否需要替换imei
295 296
 				if order.ImeiReplaceFlag == 1 {
296 297
 					showUrl = strings.ReplaceAll(showUrl, "__IMEI__", dsp.RealMd5Imei)
@@ -303,6 +304,7 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag
303 304
 					clickUrl = strings.ReplaceAll(clickUrl, "__OS__", "0")
304 305
 					targetUrl = strings.ReplaceAll(targetUrl, "__OS__", "0")
305 306
 				}
307
+			}
306 308
 		}
307 309
 
308 310
 		if strings.Index(order.Title, "__IP__") != -1 {
@@ -313,32 +315,31 @@ func GetAdsInfos(dsp *utils.DspParam, advertiser string, orderType int, fixFlag
313 315
 
314 316
 		var addi *AdAction
315 317
 		if orderType == 0 {
316
-			addi = genMonitorAction("VIEW", 5, order.Title, dsp.ReqSource, showUrl)
318
+			addi = genMonitorAction("VIEW",  order.Title, dsp.ReqSource, showUrl)
317 319
 		} else {
318
-			addi = genMonitorAction("VIEW", 5, order.Title, "follow", showUrl)
320
+			addi = genMonitorAction("VIEW",  order.Title, "follow", showUrl)
319 321
 		}
320 322
 
321 323
 		data.TargetAddition = append(data.TargetAddition, *addi)
322 324
 
323
-		r = rand.Intn(0, 1000)
324
-		c_rate = float32(order.ClickKpi) / order * 1000
325
-		if r < c_rate:
326
-		#下发点击
327
-		addi = get_monitor_url('CLICK', 1, order_name, req_source, click_url)
328
-		#addi['urls'].append(click_url)
329
-		data['target_addition'].append(addi)
330
-		md5_skip = hashlib.md5(target).hexdigest()
331
-		redis_tools.incr_key_value(click_key)
332
-		real_target = ads_config.Host + "?action=LOADING&req_source={0}&advertiser={2}&skip={1}&skip_other={3}".format(
333
-			req_source, '', order_name, md5_skip)
334
-		### 塞入缓存中
335
-		redis_tools.set_skip_info(md5_skip, target)
336
-		data['target'] = real_target
337
-		raise
338
-		gen.Return((None, data))
325
+		r := rand.Intn( 1000)
326
+		clickRate := int(float32(order.ClickKpi) / float32(order.ShowKpi) * 1000)
327
+		if r < clickRate {
328
+			//下发点击
329
+			addi = genMonitorAction("CLICK", order.Title, dsp.ReqSource, clickUrl)
330
+			data.TargetAddition = append(data.TargetAddition, *addi)
331
+			md5Skip := utils.Md5(targetUrl)
332
+			redis_data.IncrFinishedDispatchCount(order.OrderID, "click", 1, curTime)
333
+			realTarget := adslib.GetConf().Host + fmt.Sprintf("?action=LOADING&req_source=%s&advertiser=%s&skip=%s&skip_other=%s",
334
+				dsp.ReqSource, "", order.Title, md5Skip)
335
+			// 塞入缓存中
336
+			redis_data.SetSkipInfo(md5Skip, targetUrl)
337
+			data.Target = realTarget
338
+			return &data, nil
339
+		}
339 340
 	}
340
-	#获取当前已经放出去的次数
341
-	raise gen.Return((None, None))
341
+
342
+	return nil, nil
342 343
 }
343 344
 
344 345
 

+ 1 - 1
adslib/addata/xiaomi.go

@@ -67,7 +67,7 @@ type AdData struct {
67 67
 	ImageUrl       string `json:"image_url"`
68 68
 	Duration       int64
69 69
 	VideoUrl       string `json:"video_url"`
70
-	JsOrderId      string `json:"js_order_id"`
70
+	JsOrderId      int64 `json:"js_order_id"`
71 71
 	UserAgent      string `json:"user_agent"`
72 72
 	DpReport       string `json:"dp_report"`
73 73
 	Dp             string `json:"dp"`

+ 25 - 3
adslib/redis_data/redis_data.go

@@ -215,7 +215,7 @@ func SetAdsRequestNum(advertiser string, bannerid int) {
215 215
 	conn.Do("incr", redisKey)
216 216
 }
217 217
 
218
-// 设置skipredis
218
+// 设置skip redis
219 219
 func SetSkipInfo(md5Skip string, skipUrl string) {
220 220
 	conn := ads_redis.RedisConn.Get()
221 221
 	defer conn.Close()
@@ -364,8 +364,29 @@ func SetOrderPlanDispatchCount(orderId int64, dispatchType string, cnt int, curT
364 364
 	conn.Do("SET", key, cnt, 3600*24)
365 365
 }
366 366
 
367
-// 获取每分钟要投放的量
368
-func GetPerMinuteNeedDispatchCnt(orderId int64, curTime time.Time) (int, error) {
367
+// 获取总量每分钟要投放的量
368
+func GetPerMinuteNeedDispatchCnt(curTime time.Time) (int, error) {
369
+	conn := ads_redis.RedisConn.Get()
370
+	defer conn.Close()
371
+
372
+	curMinutes := curTime.Minute() + (curTime.Hour() * 60)
373
+	key := fmt.Sprintf("time_%d", curMinutes)
374
+	rsp, err := conn.Do("GET", key)
375
+
376
+	if err != nil {
377
+		return 0, err
378
+	}
379
+
380
+	if rsp == nil {
381
+		return 0, nil
382
+	}
383
+
384
+	count, _ := redis.Int(rsp, err)
385
+	return count, nil
386
+}
387
+
388
+// 获取订单每分钟要投放的量
389
+func GetOrderPerMinuteNeedDispatchCnt(orderId int64, curTime time.Time) (int, error) {
369 390
 	conn := ads_redis.RedisConn.Get()
370 391
 	defer conn.Close()
371 392
 
@@ -430,3 +451,4 @@ func GetIosUaImei(ip string) (string, string, error) {
430 451
 	json.Unmarshal(rspBytes, &uaImeiInfo)
431 452
 	return uaImeiInfo.Imei, uaImeiInfo.Ua, nil
432 453
 }
454
+

+ 16 - 1
adslib/utils/string.go

@@ -1,6 +1,9 @@
1 1
 package utils
2 2
 
3
-import "regexp"
3
+import (
4
+	"hash/crc32"
5
+	"regexp"
6
+)
4 7
 
5 8
 func Rstrip(str string) string {
6 9
 	if str == "" {
@@ -10,3 +13,15 @@ func Rstrip(str string) string {
10 13
 	reg := regexp.MustCompile("\\0+$")
11 14
 	return reg.ReplaceAllString(str, "")
12 15
 }
16
+
17
+func Hash(string) int {
18
+	v := int(crc32.ChecksumIEEE([]byte(s)))
19
+	if v >= 0 {
20
+		return v
21
+	}
22
+	if -v >= 0 {
23
+		return -v
24
+	}
25
+	// v == MinInt
26
+	return 0
27
+}

+ 1 - 1
request.go

@@ -50,7 +50,7 @@ func (self *Request) Parse(c *gin.Context) {
50 50
 
51 51
 	// 进行解密
52 52
 	if self.NewAdsFlag == 1 && self.content != "" {
53
-		contentDecode, err := encrypt.Decrypt(self.content, []byte(DECRYPT_KEY))
53
+		contentDecode, err := encrypt.Decrypt(self.content, []byte(SECERET_KEY))
54 54
 		if err != nil {
55 55
 			fmt.Printf("error: %s", err)
56 56
 			c.String(404, "decode failed")