custom.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package addata
  2. import (
  3. "fmt"
  4. log "github.com/sirupsen/logrus"
  5. "math/rand"
  6. "miads/adslib"
  7. "miads/adslib/redis_data"
  8. "miads/adslib/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func CombineOrderBy(adData *AdData, dsp *utils.DspParam) (*AdData, error) {
  14. customAdData, err := GetCustomAdsInfos(dsp, 1, 0, 0)
  15. if err != nil {
  16. return adData, err
  17. }
  18. if customAdData == nil {
  19. return adData, nil
  20. }
  21. if len(customAdData.TargetAddition) == 0 {
  22. return adData, nil
  23. }
  24. maxExchangeLen := len(customAdData.TargetAddition)
  25. // 最多替换数量, 不知道为啥是2
  26. if maxExchangeLen > 2 {
  27. maxExchangeLen = 2
  28. }
  29. hasClickTarget := false
  30. for i := 0; i < maxExchangeLen; i++ {
  31. adData.TargetAddition[i].Urls = customAdData.TargetAddition[i].Urls
  32. if customAdData.TargetAddition[i].Type == "CLICK" {
  33. adData.TargetAddition[i].Type = "CLICK"
  34. hasClickTarget = true
  35. }
  36. }
  37. if hasClickTarget {
  38. adData.Target = customAdData.Target
  39. adData.JsOrderId = customAdData.JsOrderId
  40. }
  41. return adData, nil
  42. }
  43. // 获取一个广告
  44. func GetOneAds(dsp *utils.DspParam, orderType int, fixFlag int) (*redis_data.AdOrderInfo, error) {
  45. // 取出广告
  46. orders, err := redis_data.GetOrderInfos(dsp, fixFlag)
  47. if err != nil {
  48. return nil, err
  49. }
  50. if len(orders) == 0 {
  51. return nil, nil
  52. }
  53. gotOrders := make([]redis_data.AdOrderInfo, 0, 1000)
  54. allKpi := int64(0)
  55. for _, order := range orders {
  56. if order.OrderType == int64(orderType) {
  57. gotOrders = append(gotOrders, order)
  58. allKpi += order.ShowKpi
  59. }
  60. }
  61. orderRange := make([]int, 0, 1000)
  62. curRateIdx := 0
  63. // orderRange中记录的是当前同索引位置的gotOrders里的order的比例上限, 比如如果有两个order [ShowKpi:40, ShowKpi: 60]
  64. // 那么orderRange就是 [rate:400, rate: 1000], 所以要按比例取order, 只需要获得在比例上限里随机取一个数, 然后判断落在那个orderRange里,
  65. // 对应去gotOrders取同索引里的order即可, 因为算法里有对小于1的比例做补偿, 所以rate上限可能超出1000, rate乘1000而不是100主要是为了
  66. // 如果showKpi差异过大, 对大量小于1的订单做补偿, 会影响整体流量分布, 这里放到1000倍, 降低补偿1的影响, 算法复杂度是O(n), 只有三个非嵌套循环
  67. for _, order := range gotOrders {
  68. rate := int(float32(order.ShowKpi) / float32(allKpi) * 1000)
  69. // 防止比例过小, 取int后变为0
  70. if rate < 1 {
  71. rate = 1
  72. }
  73. curRateIdx = curRateIdx + rate
  74. orderRange = append(orderRange, curRateIdx)
  75. }
  76. randNum := rand.Intn(curRateIdx)
  77. for i, rateRange := range orderRange {
  78. if randNum <= rateRange {
  79. return &gotOrders[i], nil
  80. }
  81. }
  82. return nil, nil
  83. }
  84. // 获取投放数量
  85. func getNeedDispatchCount(adData *redis_data.AdOrderInfo) (int, error) {
  86. beginTime := time.Unix(adData.BeginTime, 0)
  87. // 获取当前分钟值
  88. beginMinute := beginTime.Minute() + (beginTime.Hour() * 60)
  89. // 获取起点分钟, 不知道原因, 不加会有bug
  90. beginMinute += 2
  91. // 获取分钟数到24点还能跑多少值
  92. key := "time_all_count_" + strconv.Itoa(beginMinute)
  93. // 0 默认曲线, 1 定制曲线
  94. if adData.LineType == 1 {
  95. key = fmt.Sprintf("time_all_count_%d_%d", adData.OrderID, beginMinute)
  96. }
  97. // 起始剩余值
  98. beginRemainDispatchCount, err := redis_data.GetRemainDispatchCount(key)
  99. if err != nil {
  100. return 0, err
  101. }
  102. // 计算最后分钟
  103. endTime := time.Unix(adData.EndTime, 0)
  104. endMinute := endTime.Minute() + (endTime.Hour() * 60)
  105. if endMinute > 1439 {
  106. // 不懂这里逻辑
  107. endMinute = endMinute - 1440 + 2
  108. }
  109. endKey := "time_all_count_" + strconv.Itoa(endMinute)
  110. if adData.LineType == 1 {
  111. // 定制曲线
  112. endKey = fmt.Sprintf("time_all_count_%d_%d", adData.OrderID, endMinute)
  113. }
  114. endRemainDispatchCnt, err := redis_data.GetRemainDispatchCount(endKey)
  115. if err != nil {
  116. return 0, err
  117. }
  118. log.Infof("begin need dispatch count key: %s, %d, end: %s, %d", key, beginRemainDispatchCount, endKey, endRemainDispatchCnt)
  119. // 结束的剩余值 - 起始剩余值 = 获取区间能跑的值
  120. needDispatchCount := beginRemainDispatchCount - endRemainDispatchCnt
  121. return needDispatchCount, nil
  122. }
  123. func GetCustomAdsInfos(dsp *utils.DspParam, orderType int, fixFlag int, xiaomiHasFlag int) (*AdData, error) {
  124. order, err := GetOneAds(dsp, orderType, fixFlag)
  125. if err != nil {
  126. log.WithField("request_id", dsp.RequestId).Errorf("get one ads failed: %s", err)
  127. return nil, err
  128. }
  129. if order == nil {
  130. return nil, nil
  131. }
  132. log.WithField("request_id", dsp.RequestId).Infof("begin got custom ad info, xiaomi has flag: %d", xiaomiHasFlag)
  133. if xiaomiHasFlag == 1 {
  134. if strings.Index(order.Title, "_ios") > 0 {
  135. return nil, nil
  136. }
  137. }
  138. // 获取剩余时间内的值
  139. needDispatchCnt, err := getNeedDispatchCount(order)
  140. if err != nil {
  141. return nil, err
  142. }
  143. if needDispatchCnt < 1 {
  144. return nil, nil
  145. }
  146. curTime := time.Now()
  147. // #已经投放的key
  148. finishShowCnt, err := redis_data.GetFinishedDispatchCount(order.OrderID, "show", curTime)
  149. if err != nil {
  150. return nil, err
  151. }
  152. redis_data.SetPlanDispatchCount(order.OrderID, "show", needDispatchCnt, curTime)
  153. log.WithField("request_id", dsp.RequestId).Infof("all count: %d, over kpi: %d, show kpi: %d", needDispatchCnt, finishShowCnt, order.ShowKpi)
  154. // 计算曲线比例
  155. rate := float32(order.ShowKpi) / float32(needDispatchCnt)
  156. lineValue := 0
  157. if order.LineType == 1 {
  158. // 订单定制曲线
  159. lineValue, err = redis_data.GetOrderPerMinuteNeedDispatchCnt(order.OrderID, curTime)
  160. } else {
  161. lineValue, err = redis_data.GetPerMinuteNeedDispatchCnt(curTime)
  162. }
  163. if err != nil {
  164. return nil, err
  165. }
  166. log.WithField("request_id", dsp.RequestId).Infof("line value: %d, rate: %f", lineValue, rate)
  167. // 当前分钟需要放出去的数量
  168. curMinuteNeedDispatchCnt := int(float32(lineValue) * rate)
  169. if curMinuteNeedDispatchCnt < 1 {
  170. curMinuteNeedDispatchCnt = 1
  171. }
  172. redis_data.SetOrderPlanDispatchCount(order.OrderID, "show", curMinuteNeedDispatchCnt, curTime)
  173. log.WithField("request_id", dsp.RequestId).Infof("tt thirds: %+v", order)
  174. // 获取当前分钟已经完成的下发
  175. curMinuteFinishedDispatchCnt, err := redis_data.GetPreMinuteFinishedDispatchCount(order.OrderID, "show", curTime)
  176. log.WithField("request_id", dsp.RequestId).Infof("o value: %d, w value: %d", curMinuteFinishedDispatchCnt, curMinuteNeedDispatchCnt)
  177. if curMinuteFinishedDispatchCnt < curMinuteNeedDispatchCnt {
  178. data := AdData{
  179. Duration: 5,
  180. JsOrderId: order.JsOrderID,
  181. OrderName: order.Title,
  182. UserAgent: dsp.UaOrigin,
  183. }
  184. //放量
  185. _, err := redis_data.IncrFinishedDispatchCount(order.OrderID, "show", 1, curTime)
  186. if err != nil {
  187. return nil, err
  188. }
  189. _, err = redis_data.IncrPreMinuteFinishedDispatchCount(order.OrderID, "show", 1, curTime)
  190. if err != nil {
  191. return nil, err
  192. }
  193. showUrl := order.ShowURL
  194. clickUrl := order.ClickURL
  195. targetUrl := order.TargetURL
  196. if strings.Index(order.Title, "_ios") != -1 {
  197. iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
  198. if err != nil {
  199. log.WithField("request_id", dsp.RequestId).Errorf("get ios ua imei failed: %s", err)
  200. return nil, err
  201. }
  202. if iosUa != "" {
  203. data.UserAgent = iosUa
  204. }
  205. if order.ImeiReplaceFlag == 1 && iosImei != "" {
  206. showUrl = strings.ReplaceAll(showUrl, "__IDFA__", iosImei)
  207. clickUrl = strings.ReplaceAll(clickUrl, "__IDFA__", iosImei)
  208. targetUrl = strings.ReplaceAll(targetUrl, "__IDFA__", iosImei)
  209. }
  210. if strings.Index(order.Title, "__OS__") != -1 {
  211. showUrl = strings.ReplaceAll(showUrl, "__OS__", "1")
  212. clickUrl = strings.ReplaceAll(clickUrl, "__OS__", "1")
  213. targetUrl = strings.ReplaceAll(targetUrl, "__OS__", "1")
  214. }
  215. } else if strings.Index(order.Title, "_android") != -1 {
  216. // 判断是否需要替换imei
  217. if order.ImeiReplaceFlag == 1 {
  218. showUrl = strings.ReplaceAll(showUrl, "__IMEI__", dsp.RealMd5Imei)
  219. clickUrl = strings.ReplaceAll(clickUrl, "__IMEI__", dsp.RealMd5Imei)
  220. targetUrl = strings.ReplaceAll(targetUrl, "__IMEI__", dsp.RealMd5Imei)
  221. }
  222. if strings.Index(order.Title, "__OS__") != -1 {
  223. showUrl = strings.ReplaceAll(showUrl, "__OS__", "0")
  224. clickUrl = strings.ReplaceAll(clickUrl, "__OS__", "0")
  225. targetUrl = strings.ReplaceAll(targetUrl, "__OS__", "0")
  226. }
  227. } else {
  228. r := rand.Intn(100)
  229. if r < 40 && xiaomiHasFlag == 0 {
  230. iosImei, iosUa, err := redis_data.GetIosUaImei(dsp.Ip)
  231. if err != nil {
  232. log.WithField("request_id", dsp.RequestId).Errorf("get ios ua imei failed: %s", err)
  233. return nil, err
  234. }
  235. if iosUa != "" {
  236. data.UserAgent = iosUa
  237. }
  238. if order.ImeiReplaceFlag == 1 && iosImei != "" {
  239. showUrl = strings.ReplaceAll(showUrl, "__IDFA__", iosImei)
  240. clickUrl = strings.ReplaceAll(clickUrl, "__IDFA__", iosImei)
  241. targetUrl = strings.ReplaceAll(targetUrl, "__IDFA__", iosImei)
  242. }
  243. if strings.Index(order.Title, "__OS__") != -1 {
  244. showUrl = strings.ReplaceAll(showUrl, "__OS__", "1")
  245. clickUrl = strings.ReplaceAll(clickUrl, "__OS__", "1")
  246. targetUrl = strings.ReplaceAll(targetUrl, "__OS__", "1")
  247. }
  248. } else {
  249. // 判断是否需要替换imei
  250. if order.ImeiReplaceFlag == 1 {
  251. showUrl = strings.ReplaceAll(showUrl, "__IMEI__", dsp.RealMd5Imei)
  252. clickUrl = strings.ReplaceAll(clickUrl, "__IMEI__", dsp.RealMd5Imei)
  253. targetUrl = strings.ReplaceAll(targetUrl, "__IMEI__", dsp.RealMd5Imei)
  254. }
  255. if strings.Index(order.Title, "__OS__") != -1 {
  256. showUrl = strings.ReplaceAll(showUrl, "__OS__", "0")
  257. clickUrl = strings.ReplaceAll(clickUrl, "__OS__", "0")
  258. targetUrl = strings.ReplaceAll(targetUrl, "__OS__", "0")
  259. }
  260. }
  261. }
  262. if strings.Index(order.Title, "__IP__") != -1 {
  263. showUrl = strings.ReplaceAll(showUrl, "__IP__", dsp.Ip)
  264. clickUrl = strings.ReplaceAll(clickUrl, "__IP__", dsp.Ip)
  265. targetUrl = strings.ReplaceAll(targetUrl, "__IP__", dsp.Ip)
  266. }
  267. var addi *AdAction
  268. if orderType == 0 {
  269. addi = genMonitorAction("VIEW", order.Title, dsp.ReqSource, showUrl)
  270. } else {
  271. addi = genMonitorAction("VIEW", order.Title, "follow", showUrl)
  272. }
  273. data.TargetAddition = append(data.TargetAddition, *addi)
  274. r := rand.Intn(1000)
  275. clickRate := int(float32(order.ClickKpi) / float32(order.ShowKpi) * 1000)
  276. log.WithField("request_id", dsp.RequestId).Infof("rand: %d, rate: %d", r, clickRate)
  277. if r < clickRate {
  278. //下发点击
  279. addi = genMonitorAction("CLICK", order.Title, dsp.ReqSource, clickUrl)
  280. data.TargetAddition = append(data.TargetAddition, *addi)
  281. md5Skip := utils.Md5(targetUrl)
  282. _, err = redis_data.IncrFinishedDispatchCount(order.OrderID, "click", 1, curTime)
  283. if err != nil {
  284. log.WithField("request_id", dsp.RequestId).Errorf("incr finished dispatch count failed: %s", err)
  285. }
  286. realTarget := adslib.GetConf().Host + fmt.Sprintf("?action=LOADING&req_source=%s&advertiser=%s&skip=%s&skip_other=%s",
  287. dsp.ReqSource, order.Title, "", md5Skip)
  288. // 塞入缓存中
  289. redis_data.SetSkipInfo(md5Skip, targetUrl)
  290. data.Target = realTarget
  291. }
  292. return &data, nil
  293. }
  294. return nil, nil
  295. }
  296. func genMonitorAction(action string, title string, reqSource string, url string) *AdAction {
  297. duration := 5
  298. if action == "VIDEO_TIMER" {
  299. duration = 7
  300. }
  301. urlArr := make([]string, 0, 50)
  302. if url != "" {
  303. urlArr = strings.Split(url, "||")
  304. }
  305. adAction := AdAction{
  306. Type: action,
  307. Duration: duration,
  308. Urls: urlArr,
  309. }
  310. urlHost := adslib.GetConf().HostIos
  311. actionUrl := fmt.Sprintf("%s?action=%s&advertiser=%s&req_source=%s",
  312. urlHost, action, title, reqSource)
  313. adAction.Urls = append(adAction.Urls, actionUrl)
  314. return &adAction
  315. }