redis_data.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 advertiser == "" {
  130. return
  131. }
  132. conn := ads_redis.RedisConn.Get()
  133. defer conn.Close()
  134. redisKey := fmt.Sprintf("adsv3_%s_%s_%d", advertiser, action, bannerid)
  135. curTime := int(time.Now().Unix())
  136. if curTime%100 == 0 {
  137. _, _ = conn.Do("delete", redisKey)
  138. }
  139. _, _ = conn.Do("INCR", redisKey)
  140. }
  141. // 获取设备+ip上报是否可以
  142. func GetDeviceIpReport(deviceId string, ip string) (bool, error) {
  143. conn := ads_redis.RedisConn.Get()
  144. defer conn.Close()
  145. redisKey := fmt.Sprintf("dim2_%s_'%s'", deviceId, ip)
  146. rsp, err := conn.Do("GET", redisKey)
  147. if err != nil {
  148. return false, err
  149. }
  150. lastReportTime := int64(0)
  151. if rsp != nil {
  152. lastReportTime, _ = redis.Int64(rsp, err)
  153. }
  154. if time.Now().Unix()-lastReportTime >= 3*86400 {
  155. return true, nil
  156. }
  157. return false, nil
  158. }
  159. // 设置设备+ip上报的时间点
  160. func SetDeviceIpReport(deviceid string, ip string) {
  161. conn := ads_redis.RedisConn.Get()
  162. defer conn.Close()
  163. redisKey := fmt.Sprintf("dim2_%s_'%s'", deviceid, ip)
  164. _, _ = conn.Do("SET", redisKey, time.Now().Unix(), 3*86400)
  165. }
  166. // 设置总请求到广告的次数
  167. func SetAdsRequestNum(advertiser string, bannerId int) {
  168. if advertiser == "" {
  169. return
  170. }
  171. conn := ads_redis.RedisConn.Get()
  172. defer conn.Close()
  173. redisKey := fmt.Sprintf("adsv2_%s_%d", advertiser, bannerId)
  174. if time.Now().Unix()%100 == 0 {
  175. _, _ = conn.Do("delete", redisKey)
  176. }
  177. _, _ = conn.Do("incr", redisKey)
  178. }
  179. func SetAdsRealRequestNum(advertiser string) {
  180. if advertiser == "" {
  181. return
  182. }
  183. conn := ads_redis.RedisConn.Get()
  184. defer conn.Close()
  185. redisKey := fmt.Sprintf("adsv2_request_%s", advertiser)
  186. _, _ = conn.Do("incr", redisKey)
  187. }
  188. // 设置skip redis
  189. func SetSkipInfo(md5Skip string, skipUrl string) {
  190. conn := ads_redis.RedisConn.Get()
  191. defer conn.Close()
  192. redisKey := "su_" + md5Skip
  193. _, _ = conn.Do("set", redisKey, skipUrl, 300)
  194. }
  195. // 通过advertiser获取最小的script_order
  196. func GetMinScriptOrderByAdv(advertiser string) (int64, error) {
  197. conn := ads_redis.RedisConn.Get()
  198. defer conn.Close()
  199. redisKey := "adv_script_" + advertiser
  200. rsp, err := conn.Do("GET", redisKey)
  201. if err != nil {
  202. return 0, err
  203. }
  204. if rsp == nil {
  205. return 0, err
  206. }
  207. scriptOrder, _ := redis.Int64(rsp, err)
  208. return scriptOrder, nil
  209. }
  210. // 设置来源的曝光次数
  211. func SetReqSourceView(adv string, reqSource string, source string) {
  212. conn := ads_redis.RedisConn.Get()
  213. defer conn.Close()
  214. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  215. key := fmt.Sprintf("view3_%s_%s_%d_%s", adv, reqSource, dateInt, source)
  216. _, _ = conn.Do("incr", key)
  217. }
  218. // 获取剩余总投放量
  219. func GetRemainDispatchCount(key string) (int, error) {
  220. conn := ads_redis.RedisConn.Get()
  221. defer conn.Close()
  222. rsp, err := conn.Do("GET", key)
  223. if err != nil {
  224. return 0, err
  225. }
  226. if rsp == nil {
  227. return 0, nil
  228. }
  229. count, _ := redis.Int(rsp, err)
  230. return count, nil
  231. }
  232. // 获取已经投放的量
  233. // dispatchType: "click", "show"
  234. func GetFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, error) {
  235. conn := ads_redis.RedisConn.Get()
  236. defer conn.Close()
  237. key := fmt.Sprintf("order_kpi_%d_%d%d%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), dispatchType)
  238. rsp, err := conn.Do("GET", key)
  239. if err != nil {
  240. return 0, err
  241. }
  242. if rsp == nil {
  243. return 0, nil
  244. }
  245. count, _ := redis.Int(rsp, err)
  246. return count, nil
  247. }
  248. // 增加已经投放的量
  249. // dispatchType: "click", "show"
  250. func IncrFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
  251. conn := ads_redis.RedisConn.Get()
  252. defer conn.Close()
  253. key := fmt.Sprintf("order_kpi_%d_%d%d%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), dispatchType)
  254. rsp, err := conn.Do("INCR", key, incrCnt)
  255. if err != nil {
  256. return 0, err
  257. }
  258. count, _ := redis.Int(rsp, err)
  259. return count, nil
  260. }
  261. // 获取当前分钟已经投放的量
  262. // dispatchType: "click", "show"
  263. func GetPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, curTime time.Time) (int, error) {
  264. conn := ads_redis.RedisConn.Get()
  265. defer conn.Close()
  266. curMinutes := curTime.Hour()*60 + curTime.Minute()
  267. key := fmt.Sprintf("order_kpi_%d_%d%d%d_%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), curMinutes, dispatchType)
  268. rsp, err := conn.Do("GET", key)
  269. log.Infof("%s %s", key, rsp)
  270. if err != nil {
  271. return 0, err
  272. }
  273. if rsp == nil {
  274. return 0, nil
  275. }
  276. count, _ := redis.Int(rsp, err)
  277. return count, nil
  278. }
  279. // 增加当前分钟已经投放的量
  280. // dispatchType: "click", "show"
  281. func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, 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("INCR", key, incrCnt)
  287. if err != nil {
  288. return 0, err
  289. }
  290. count, _ := redis.Int(rsp, err)
  291. return count, nil
  292. }
  293. // 设置总计划投放数量
  294. // dispatchType: "click", "show"
  295. func SetPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
  296. conn := ads_redis.RedisConn.Get()
  297. defer conn.Close()
  298. key := fmt.Sprintf("plan_all_kpi_%d_%d%d%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), dispatchType)
  299. _, _ = conn.Do("SET", key, cnt, 3600*24)
  300. }
  301. // 设置订单计划投放数量
  302. // dispatchType: "click", "show"
  303. func SetOrderPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
  304. conn := ads_redis.RedisConn.Get()
  305. defer conn.Close()
  306. curMinutes := curTime.Hour()*60 + curTime.Minute()
  307. key := fmt.Sprintf("plan_kpi_%d_%d%d%d_%d_%s_kpi", orderId, curTime.Year(), curTime.Month(), curTime.Day(), curMinutes, dispatchType)
  308. _, _ = conn.Do("SET", key, cnt, 3600*24)
  309. }
  310. // 获取总量每分钟要投放的量
  311. func GetPerMinuteNeedDispatchCnt(curTime time.Time) (int, error) {
  312. conn := ads_redis.RedisConn.Get()
  313. defer conn.Close()
  314. curMinutes := curTime.Minute() + (curTime.Hour() * 60)
  315. key := fmt.Sprintf("time_%d", curMinutes)
  316. rsp, err := conn.Do("GET", key)
  317. if err != nil {
  318. return 0, err
  319. }
  320. if rsp == nil {
  321. return 0, nil
  322. }
  323. count, _ := redis.Int(rsp, err)
  324. return count, nil
  325. }
  326. // 获取订单每分钟要投放的量
  327. func GetOrderPerMinuteNeedDispatchCnt(orderId int64, curTime time.Time) (int, error) {
  328. conn := ads_redis.RedisConn.Get()
  329. defer conn.Close()
  330. curMinutes := curTime.Minute() + (curTime.Hour() * 60)
  331. key := fmt.Sprintf("time_%d_%d", orderId, curMinutes)
  332. rsp, err := conn.Do("GET", key)
  333. if err != nil {
  334. return 0, err
  335. }
  336. if rsp == nil {
  337. return 0, nil
  338. }
  339. count, _ := redis.Int(rsp, err)
  340. return count, nil
  341. }
  342. // 获取ios的ua和imei
  343. func GetIosUaImei(ip string) (string, string, error) {
  344. conn := ads_redis.RedisConn.Get()
  345. defer conn.Close()
  346. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  347. iosUaImeiRedisKey := fmt.Sprintf("ads_ios_%d_%s", dateInt, ip)
  348. // "{'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'}"
  349. rsp, err := conn.Do("GET", iosUaImeiRedisKey)
  350. if err != nil {
  351. return "", "", err
  352. }
  353. if rsp == nil {
  354. redisKey := fmt.Sprintf("new_ios_ua_v2_%d", dateInt)
  355. rsp, err = conn.Do("SRANDMEMBER", redisKey)
  356. if err != nil {
  357. return "", "", err
  358. }
  359. if rsp == nil {
  360. return "", "", nil
  361. }
  362. _, _ = conn.Do("SREM", redisKey, rsp)
  363. tomorrowDateInt, _ := strconv.Atoi(time.Unix(time.Now().Unix()+86400, 0).Format("20060102"))
  364. redisKey = fmt.Sprintf("new_ios_ua_v2_%d", tomorrowDateInt)
  365. _, _ = conn.Do("SADD", redisKey, rsp)
  366. }
  367. _, _ = conn.Do("SET", iosUaImeiRedisKey, rsp, 86520)
  368. var uaImeiInfo struct {
  369. Imei string
  370. Ua string
  371. }
  372. rspBytes, _ := redis.Bytes(rsp, err)
  373. err = json.Unmarshal(rspBytes, &uaImeiInfo)
  374. if err != nil {
  375. return "", "", err
  376. }
  377. return uaImeiInfo.Imei, uaImeiInfo.Ua, nil
  378. }