redis_data.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package redis_data
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gomodule/redigo/redis"
  6. log "github.com/sirupsen/logrus"
  7. "miads/adslib"
  8. "miads/adslib/ads_redis"
  9. "strconv"
  10. "time"
  11. )
  12. type ChannelFlag struct {
  13. Weigth int `json:"weigth"`
  14. ChannelFlag int `json:"channel_flag"`
  15. }
  16. type FreqControlConfItem struct {
  17. BeginHour int `json:"begin_hour"`
  18. EndHour int `json:"end_hour"`
  19. FreqTime int `json:"freq_time"`
  20. }
  21. type FreqControlConf struct {
  22. confs []FreqControlConfItem
  23. }
  24. func (self *FreqControlConf) GetControlTime(hour int) (int, bool) {
  25. if self.confs == nil {
  26. return 0, false
  27. }
  28. for _, v := range self.confs {
  29. if v.BeginHour <= hour && hour <= v.EndHour {
  30. return v.FreqTime, true
  31. }
  32. }
  33. return 0, false
  34. }
  35. // 获取渠道的标记
  36. func GetChannelFlag(reqSource string, business string) (ChannelFlag, error) {
  37. conn := ads_redis.RedisConn.Get()
  38. defer conn.Close()
  39. redisKey := fmt.Sprintf("acfv2_%s_%s", reqSource, business)
  40. rsp, err := conn.Do("GET", redisKey)
  41. if err != nil {
  42. return ChannelFlag{}, err
  43. }
  44. if rsp == nil {
  45. return ChannelFlag{}, nil
  46. }
  47. rspBytes, _ := redis.Bytes(rsp, err)
  48. channelFlag := ChannelFlag{}
  49. err = json.Unmarshal(rspBytes, &channelFlag)
  50. if err != nil {
  51. return ChannelFlag{}, err
  52. }
  53. return channelFlag, nil
  54. }
  55. // 获取渠道频率信息
  56. func GetFreqControlConf(reqSource string) (FreqControlConf, error) {
  57. conn := ads_redis.RedisConn.Get()
  58. defer conn.Close()
  59. redisKey := "rsfreq_" + reqSource
  60. rsp, err := conn.Do("GET", redisKey)
  61. if err != nil {
  62. return FreqControlConf{}, err
  63. }
  64. if rsp == nil {
  65. return FreqControlConf{}, nil
  66. }
  67. rspBytes, _ := redis.Bytes(rsp, err)
  68. f := FreqControlConf{}
  69. err = json.Unmarshal(rspBytes, &f.confs)
  70. if err != nil {
  71. return FreqControlConf{}, err
  72. }
  73. return f, nil
  74. }
  75. // 获取该广告的总请求次数
  76. func GetAdsRequestNum(advertiser string, adsBannerId int) (int, error) {
  77. if advertiser == "" {
  78. return 0, nil
  79. }
  80. conn := ads_redis.RedisConn.Get()
  81. defer conn.Close()
  82. redisKey := fmt.Sprintf("adsv2_%s_%d", advertiser, adsBannerId)
  83. rsp, err := conn.Do("GET", redisKey)
  84. if err != nil {
  85. return 0, err
  86. }
  87. if rsp == nil {
  88. return 0, nil
  89. }
  90. number, err := redis.Int(rsp, err)
  91. return number, nil
  92. }
  93. // 获取回调广告的动作请求次数
  94. func GetAdsFeedbackNum(advertiser string, action string, bannerid int) (int, error) {
  95. if advertiser == "" {
  96. return 0, nil
  97. }
  98. conn := ads_redis.RedisConn.Get()
  99. defer conn.Close()
  100. redisKey := fmt.Sprintf("adsv3_%s_%s_%d", advertiser, action, bannerid)
  101. if time.Now().Unix()%100 == 0 {
  102. _, _ = conn.Do("DELETE", redisKey)
  103. }
  104. rsp, err := conn.Do("GET", redisKey)
  105. if err != nil {
  106. return 0, err
  107. }
  108. if rsp == nil {
  109. return 0, nil
  110. }
  111. number, _ := redis.Int(rsp, err)
  112. return number, nil
  113. }
  114. // 获取流量百分比
  115. func GetFlowPercentDuration(advertiser string, action string) *adslib.FlogControlItem {
  116. config := adslib.GetConf()
  117. flowConf, ok := config.FlowPercent[advertiser]
  118. if !ok {
  119. return nil
  120. }
  121. actionConf, ok := flowConf[action]
  122. if !ok {
  123. return nil
  124. }
  125. return &actionConf
  126. }
  127. // 设置回调回来的广告动作的次数
  128. func SetDistributeActionNum(advertiser string, action string, bannerid int) {
  129. if adslib.GetConf().TestMode {
  130. return
  131. }
  132. if advertiser == "" {
  133. return
  134. }
  135. conn := ads_redis.RedisConn.Get()
  136. defer conn.Close()
  137. redisKey := fmt.Sprintf("adsv3_%s_%s_%d", advertiser, action, bannerid)
  138. curTime := int(time.Now().Unix())
  139. if curTime%100 == 0 {
  140. _, _ = conn.Do("delete", redisKey)
  141. }
  142. _, _ = conn.Do("INCR", redisKey)
  143. }
  144. // 获取设备+ip上报是否可以
  145. func GetDeviceIpReport(deviceId string, ip string) (bool, error) {
  146. conn := ads_redis.RedisConn.Get()
  147. defer conn.Close()
  148. redisKey := fmt.Sprintf("dim2_%s_'%s'", deviceId, ip)
  149. rsp, err := conn.Do("GET", redisKey)
  150. if err != nil {
  151. return false, err
  152. }
  153. lastReportTime := int64(0)
  154. if rsp != nil {
  155. lastReportTime, _ = redis.Int64(rsp, err)
  156. }
  157. if time.Now().Unix()-lastReportTime >= 3*86400 {
  158. return true, nil
  159. }
  160. return false, nil
  161. }
  162. // 设置设备+ip上报的时间点
  163. func SetDeviceIpReport(deviceid string, ip string) {
  164. if adslib.GetConf().TestMode {
  165. return
  166. }
  167. conn := ads_redis.RedisConn.Get()
  168. defer conn.Close()
  169. redisKey := fmt.Sprintf("dim2_%s_'%s'", deviceid, ip)
  170. _, _ = conn.Do("SET", redisKey, time.Now().Unix(), 3*86400)
  171. }
  172. // 设置总请求到广告的次数
  173. func SetAdsRequestNum(advertiser string, bannerId int) {
  174. if adslib.GetConf().TestMode {
  175. return
  176. }
  177. if advertiser == "" {
  178. return
  179. }
  180. conn := ads_redis.RedisConn.Get()
  181. defer conn.Close()
  182. redisKey := fmt.Sprintf("adsv2_%s_%d", advertiser, bannerId)
  183. if time.Now().Unix()%100 == 0 {
  184. _, _ = conn.Do("delete", redisKey)
  185. }
  186. _, _ = conn.Do("incr", redisKey)
  187. }
  188. func SetAdsRealRequestNum(advertiser string) {
  189. if adslib.GetConf().TestMode {
  190. return
  191. }
  192. if advertiser == "" {
  193. return
  194. }
  195. conn := ads_redis.RedisConn.Get()
  196. defer conn.Close()
  197. redisKey := fmt.Sprintf("adsv2_request_%s", advertiser)
  198. _, _ = conn.Do("incr", redisKey)
  199. }
  200. // 设置skip redis
  201. func SetSkipInfo(md5Skip string, skipUrl string) {
  202. conn := ads_redis.RedisConn.Get()
  203. defer conn.Close()
  204. redisKey := "su_" + md5Skip
  205. _, _ = conn.Do("set", redisKey, skipUrl, 300)
  206. }
  207. // 通过advertiser获取最小的script_order
  208. func GetMinScriptOrderByAdv(advertiser string) (int64, error) {
  209. conn := ads_redis.RedisConn.Get()
  210. defer conn.Close()
  211. redisKey := "adv_script_" + advertiser
  212. rsp, err := conn.Do("GET", redisKey)
  213. if err != nil {
  214. return 0, err
  215. }
  216. if rsp == nil {
  217. return 0, err
  218. }
  219. scriptOrder, _ := redis.Int64(rsp, err)
  220. return scriptOrder, nil
  221. }
  222. // 设置来源的曝光次数
  223. func SetReqSourceView(adv string, reqSource string, source string) {
  224. if adslib.GetConf().TestMode {
  225. return
  226. }
  227. conn := ads_redis.RedisConn.Get()
  228. defer conn.Close()
  229. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  230. key := fmt.Sprintf("view3_%s_%s_%d_%s", adv, reqSource, dateInt, source)
  231. _, _ = conn.Do("incr", key)
  232. }
  233. // 获取剩余总投放量
  234. func GetRemainDispatchCount(key string) (int, error) {
  235. conn := ads_redis.RedisConn.Get()
  236. defer conn.Close()
  237. rsp, err := conn.Do("GET", key)
  238. if err != nil {
  239. return 0, err
  240. }
  241. if rsp == nil {
  242. return 0, nil
  243. }
  244. count, _ := redis.Int(rsp, err)
  245. return count, nil
  246. }
  247. // 获取已经投放的量
  248. // dispatchType: "click", "show"
  249. func GetFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, error) {
  250. conn := ads_redis.RedisConn.Get()
  251. defer conn.Close()
  252. key := fmt.Sprintf("order_kpi_%d_%d%d%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), dispatchType)
  253. rsp, err := conn.Do("GET", key)
  254. if err != nil {
  255. return 0, err
  256. }
  257. if rsp == nil {
  258. return 0, nil
  259. }
  260. count, _ := redis.Int(rsp, err)
  261. return count, nil
  262. }
  263. // 增加已经投放的量
  264. // dispatchType: "click", "show"
  265. func IncrFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
  266. if adslib.GetConf().TestMode {
  267. return 0, nil
  268. }
  269. conn := ads_redis.RedisConn.Get()
  270. defer conn.Close()
  271. key := fmt.Sprintf("order_kpi_%d_%d%d%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), dispatchType)
  272. rsp, err := conn.Do("INCR", key, incrCnt)
  273. if err != nil {
  274. return 0, err
  275. }
  276. count, _ := redis.Int(rsp, err)
  277. return count, nil
  278. }
  279. // 获取当前分钟已经投放的量
  280. // dispatchType: "click", "show"
  281. func GetPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, error) {
  282. conn := ads_redis.RedisConn.Get()
  283. defer conn.Close()
  284. curMinutes := curTime.Hour()*60 + curTime.Minute()
  285. key := fmt.Sprintf("order_kpi_%d_%d%d%d_%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), curMinutes, dispatchType)
  286. rsp, err := conn.Do("GET", key)
  287. log.Infof("%s %s", key, rsp)
  288. if err != nil {
  289. return 0, err
  290. }
  291. if rsp == nil {
  292. return 0, nil
  293. }
  294. count, _ := redis.Int(rsp, err)
  295. return count, nil
  296. }
  297. // 增加当前分钟已经投放的量
  298. // dispatchType: "click", "show"
  299. func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
  300. if adslib.GetConf().TestMode {
  301. return 0, nil
  302. }
  303. conn := ads_redis.RedisConn.Get()
  304. defer conn.Close()
  305. curMinutes := curTime.Hour()*60 + curTime.Minute()
  306. key := fmt.Sprintf("order_kpi_%d_%d%d%d_%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), curMinutes, dispatchType)
  307. rsp, err := conn.Do("INCR", key, incrCnt)
  308. if err != nil {
  309. return 0, err
  310. }
  311. count, _ := redis.Int(rsp, err)
  312. return count, nil
  313. }
  314. // 设置总计划投放数量
  315. // dispatchType: "click", "show"
  316. func SetPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
  317. if adslib.GetConf().TestMode {
  318. return
  319. }
  320. conn := ads_redis.RedisConn.Get()
  321. defer conn.Close()
  322. key := fmt.Sprintf("plan_all_kpi_%d_%d%d%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), dispatchType)
  323. _, _ = conn.Do("SET", key, cnt, 3600*24)
  324. }
  325. // 设置订单计划投放数量
  326. // dispatchType: "click", "show"
  327. func SetOrderPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
  328. if adslib.GetConf().TestMode {
  329. return
  330. }
  331. conn := ads_redis.RedisConn.Get()
  332. defer conn.Close()
  333. curMinutes := curTime.Hour()*60 + curTime.Minute()
  334. key := fmt.Sprintf("plan_kpi_%d_%d%d%d_%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), curMinutes, dispatchType)
  335. _, _ = conn.Do("SET", key, cnt, 3600*24)
  336. }
  337. // 获取总量每分钟要投放的量
  338. func GetPerMinuteNeedDispatchCnt(curTime time.Time) (int, error) {
  339. conn := ads_redis.RedisConn.Get()
  340. defer conn.Close()
  341. curMinutes := curTime.Minute() + (curTime.Hour() * 60)
  342. key := fmt.Sprintf("time_%d", curMinutes)
  343. rsp, err := conn.Do("GET", key)
  344. if err != nil {
  345. return 0, err
  346. }
  347. if rsp == nil {
  348. return 0, nil
  349. }
  350. count, _ := redis.Int(rsp, err)
  351. return count, nil
  352. }
  353. // 获取订单每分钟要投放的量
  354. func GetOrderPerMinuteNeedDispatchCnt(orderId int64, curTime time.Time) (int, error) {
  355. conn := ads_redis.RedisConn.Get()
  356. defer conn.Close()
  357. curMinutes := curTime.Minute() + (curTime.Hour() * 60)
  358. key := fmt.Sprintf("time_%d_%d", orderId, curMinutes)
  359. rsp, err := conn.Do("GET", key)
  360. if err != nil {
  361. return 0, err
  362. }
  363. if rsp == nil {
  364. return 0, nil
  365. }
  366. count, _ := redis.Int(rsp, err)
  367. return count, nil
  368. }
  369. // 获取ios的ua和imei
  370. func GetIosUaImei(ip string) (string, string, error) {
  371. conn := ads_redis.RedisConn.Get()
  372. defer conn.Close()
  373. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  374. iosUaImeiRedisKey := fmt.Sprintf("ads_ios_%d_%s", dateInt, ip)
  375. // "{'imei': 'F389E3E1-5459-4B30-83A9-1AD504F6293F', 'ua': 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML like Gecko) Mobile/15E148'}"
  376. rsp, err := conn.Do("GET", iosUaImeiRedisKey)
  377. if err != nil {
  378. return "", "", err
  379. }
  380. if rsp == nil {
  381. redisKey := fmt.Sprintf("new_ios_ua_v2_%d", dateInt)
  382. rsp, err = conn.Do("SRANDMEMBER", redisKey)
  383. if err != nil {
  384. return "", "", err
  385. }
  386. if rsp == nil {
  387. return "", "", nil
  388. }
  389. _, _ = conn.Do("SREM", redisKey, rsp)
  390. tomorrowDateInt, _ := strconv.Atoi(time.Unix(time.Now().Unix()+86400, 0).Format("20060102"))
  391. redisKey = fmt.Sprintf("new_ios_ua_v2_%d", tomorrowDateInt)
  392. _, _ = conn.Do("SADD", redisKey, rsp)
  393. }
  394. _, _ = conn.Do("SET", iosUaImeiRedisKey, rsp, 86520)
  395. var uaImeiInfo struct {
  396. Imei string
  397. Ua string
  398. }
  399. rspBytes, _ := redis.Bytes(rsp, err)
  400. err = json.Unmarshal(rspBytes, &uaImeiInfo)
  401. if err != nil {
  402. return "", "", err
  403. }
  404. return uaImeiInfo.Imei, uaImeiInfo.Ua, nil
  405. }