redis_data.go 11 KB

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