aheadManager.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package mananger
  2. import (
  3. "app.yhyue.com/moapp/jybase/redis"
  4. "fmt"
  5. "github.com/robfig/cron/v3"
  6. "leadGeneration/entity/activeUsers"
  7. "leadGeneration/entity/search"
  8. "leadGeneration/public"
  9. "leadGeneration/vars"
  10. "log"
  11. "sync"
  12. "time"
  13. )
  14. //AheadManager 超前项目管理
  15. type AheadManager struct {
  16. Conf vars.AheadConfig
  17. UserGroup map[string]int
  18. sync.RWMutex
  19. }
  20. const (
  21. //缓存
  22. AheadCacheDb = "newother"
  23. AheadRequestFrequencyCacheKey = "leadGeneration_AheadRequest_%d_%s"
  24. AheadRequestTimesLong = 60 * 60 * 24 //缓存一天
  25. )
  26. // InitAheadManager 初始化
  27. func InitAheadManager(conf vars.AheadConfig) *AheadManager {
  28. manager := &AheadManager{
  29. Conf: conf,
  30. UserGroup: make(map[string]int),
  31. }
  32. go manager.ScheduledTasks()
  33. return manager
  34. }
  35. // GetData 获取查询数据
  36. func (this *AheadManager) GetData(userId, keyWords string, isNew bool) map[string]interface{} {
  37. if !(isNew || this.checkGroupUser(userId)) {
  38. return nil
  39. }
  40. //校验每日请求次数
  41. cacheKey := fmt.Sprintf(AheadRequestFrequencyCacheKey, time.Now().Format(public.TimeFormat), userId)
  42. if this.Conf.DailyTimes > redis.GetInt(AheadCacheDb, fmt.Sprintf(AheadRequestFrequencyCacheKey, time.Now().Format(public.TimeFormat))) {
  43. return nil
  44. }
  45. //查询数据
  46. rDate := search.AdvancedProject(userId, keyWords)
  47. //if err != nil {
  48. // log.Printf("[ERROR]AheadManager %s %s GetData Error %v\n", userId, keyWords, err)
  49. // return nil
  50. //}
  51. //累计请求计数
  52. if rDate != nil && len(rDate) > 0 {
  53. if num := redis.Incr(AheadCacheDb, cacheKey); num == 1 {
  54. _ = redis.SetExpire(AheadCacheDb, cacheKey, AheadRequestTimesLong)
  55. }
  56. }
  57. return rDate
  58. }
  59. //CheckGroupUser 校验用户是否有资格展示
  60. func (this *AheadManager) checkGroupUser(userId string) (exists bool) {
  61. this.RLock()
  62. defer this.RUnlock()
  63. _, exists = this.UserGroup[userId]
  64. return
  65. }
  66. //ScheduledTasks 定时任务
  67. func (this *AheadManager) ScheduledTasks() {
  68. if this.Conf.UpdateCron != "" {
  69. // 给对象增加定时任务
  70. if _, err := cron.New().AddFunc(this.Conf.UpdateCron, this.UpdateUserGroupJob); err != nil {
  71. panic(err)
  72. }
  73. }
  74. //首次运行圈选用户
  75. go this.UpdateUserGroupJob()
  76. }
  77. //UpdateUserGroupJob 更新用户群组
  78. func (this *AheadManager) UpdateUserGroupJob() {
  79. if this.Conf.Prop <= 0 {
  80. return
  81. }
  82. //查询月活用户
  83. userArr := activeUsers.GetWeekActiveFreeUsers()
  84. this.Lock()
  85. defer this.Unlock()
  86. //查询上批次活跃用户
  87. newMap := map[string]int{}
  88. //测试账户
  89. if len(vars.Config.TestUid) > 0 {
  90. for _, uid := range vars.Config.TestUid {
  91. newMap[uid] = 0
  92. }
  93. }
  94. //if len(this.UserGroup) != 0 {
  95. // for uId, num := range this.UserGroup {
  96. // if num > this.Conf.SaveClickTimes {
  97. // newMap[uId] = 0
  98. // }
  99. // }
  100. //}
  101. //新圈用户
  102. for _, uId := range userArr {
  103. newMap[uId] = 0
  104. }
  105. this.UserGroup = newMap
  106. log.Printf("AheadManager NewGroup %v\n", newMap)
  107. }
  108. func sortUserByBatchAndGetFinal() {
  109. }