redis_data.go 11 KB

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