redis_data.go 11 KB

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