redis_data.go 11 KB

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