custom.go 10 KB

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