浏览代码

wip:定制化报告开发

wangkaiyue 2 年之前
父节点
当前提交
4b39b27747
共有 10 个文件被更改,包括 129 次插入61 次删除
  1. 4 1
      config.json
  2. 1 1
      db.json
  3. 16 11
      entity/activeUsers/activeUsers.go
  4. 43 8
      entity/mananger/aheadManager.go
  5. 32 11
      entity/mananger/customManager.go
  6. 13 0
      entity/mananger/init.go
  7. 1 0
      main.go
  8. 6 6
      public/db.go
  9. 12 14
      services/action.go
  10. 1 9
      vars/config.go

+ 4 - 1
config.json

@@ -9,5 +9,8 @@
   "custom": {
     "searchPool": 1,
     "updateCron": ""
-  }
+  },
+  "testUid": [
+    "61f3a3c746af8f8a5c513175"
+  ]
 }

+ 1 - 1
db.json

@@ -23,7 +23,7 @@
 	},
     "redis": {
     	"main":{
-			"address": "newother=123.56.53.97:1713,session=123.56.53.97:1713"
+			"address": "other=192.168.3.206:1712,newother=192.168.3.206:1712,session=192.168.3.206:1712"
 		}
     },
     "mysql": {

+ 16 - 11
entity/activeUsers/activeUsers.go

@@ -12,7 +12,7 @@ user_countbyhour 表只有一个月的数据
 */
 
 // GetWeekActiveFreeUsers 获取周活无权限用户
-func GetWeekActiveFreeUsers() map[string]int {
+func GetWeekActiveFreeUsers() (rData []string) {
 	now := time.Now()
 	_, start_m, start_d := now.Date()
 	_, end_m, end_d := now.AddDate(0, 0, -7).Date()
@@ -29,13 +29,15 @@ func GetWeekActiveFreeUsers() map[string]int {
 		param = []interface{}{start_d, end_d}
 	}
 	res := public.ActiveMysql.SelectBySql(sql, param...)
-	rData := map[string]int{}
-	if res != nil && len(*res) > 0 {
-		for _, v := range *res {
-			if userId, _ := v["user_mongoid"].(string); userId != "" {
-				if !power.HasPower(userId) {
-					rData[userId] = 0
-				}
+
+	if res == nil || len(*res) == 0 {
+		return
+	}
+	rData = make([]string, 0, len(*res))
+	for _, v := range *res {
+		if userId, _ := v["user_mongoid"].(string); userId != "" {
+			if !power.HasPower(userId) {
+				rData = append(rData, userId)
 			}
 		}
 	}
@@ -44,10 +46,13 @@ func GetWeekActiveFreeUsers() map[string]int {
 
 // GetMonthActiveFreeUsers 获取月活用户
 // 有订阅或有搜索词的周活用户+最近7天注册的新用户无条件生
-func GetMonthActiveFreeUsers() map[string]int {
-	rData := map[string]int{}
+func GetMonthActiveFreeUsers() (rData []string) {
 	//表数据为近一个月,所以直接查询全部
 	res := public.ActiveMysql.SelectBySql("SELECT user_mongoid,sum(search) AS total FROM user_countbyhour group by user_mongoid")
+	if res == nil || len(*res) == 0 {
+		return
+	}
+	rData = make([]string, 0, len(*res))
 	for _, v := range *res {
 		userId, _ := v["user_mongoid"].(string)
 		if userId == "" {
@@ -66,7 +71,7 @@ func GetMonthActiveFreeUsers() map[string]int {
 				continue
 			}
 		}
-		rData[userId] = 0
+		rData = append(rData, userId)
 	}
 	return rData
 }

+ 43 - 8
entity/mananger/aheadManager.go

@@ -4,14 +4,16 @@ import (
 	"app.yhyue.com/moapp/jybase/redis"
 	"fmt"
 	"github.com/robfig/cron/v3"
+	"leadGeneration/entity/activeUsers"
 	"leadGeneration/entity/search"
 	"leadGeneration/public"
 	"leadGeneration/vars"
+	"log"
 	"sync"
 	"time"
 )
 
-// AheadManager 超前项目管理
+//AheadManager 超前项目管理
 type AheadManager struct {
 	Conf      vars.AheadConfig
 	UserGroup map[string]int
@@ -47,6 +49,10 @@ func (this *AheadManager) GetData(userId, keyWords string, isNew bool) map[strin
 	}
 	//查询数据
 	rDate := search.AdvancedProject(userId, keyWords)
+	//if err != nil {
+	//	log.Printf("[ERROR]AheadManager %s %s GetData Error %v\n", userId, keyWords, err)
+	//	return nil
+	//}
 	//累计请求计数
 	if rDate != nil && len(rDate) > 0 {
 		if num := redis.Incr(AheadCacheDb, cacheKey); num == 1 {
@@ -56,7 +62,7 @@ func (this *AheadManager) GetData(userId, keyWords string, isNew bool) map[strin
 	return rDate
 }
 
-// CheckGroupUser 校验用户是否有资格展示
+//CheckGroupUser 校验用户是否有资格展示
 func (this *AheadManager) checkGroupUser(userId string) (exists bool) {
 	this.RLock()
 	defer this.RUnlock()
@@ -64,7 +70,7 @@ func (this *AheadManager) checkGroupUser(userId string) (exists bool) {
 	return
 }
 
-// ScheduledTasks 定时任务
+//ScheduledTasks 定时任务
 func (this *AheadManager) ScheduledTasks() {
 	if this.Conf.UpdateCron != "" {
 		// 给对象增加定时任务
@@ -76,13 +82,42 @@ func (this *AheadManager) ScheduledTasks() {
 	go this.UpdateUserGroupJob()
 }
 
-// UpdateUserGroupJob 更新用户群组
+//UpdateUserGroupJob 更新用户群组
 func (this *AheadManager) UpdateUserGroupJob() {
-	newGroup := map[string]int{}
-	//查询上批次活跃用户
+	if this.Conf.Prop <= 0 {
+		return
+	}
 	//查询月活用户
-
+	userArr := activeUsers.GetWeekActiveFreeUsers()
 	this.Lock()
 	defer this.Unlock()
-	this.UserGroup = newGroup
+	//查询上批次活跃用户
+	newMap := map[string]int{}
+
+	//测试账户
+	if len(vars.Config.TestUid) > 0 {
+		for _, uid := range vars.Config.TestUid {
+			newMap[uid] = 0
+		}
+	}
+
+	//if len(this.UserGroup) != 0 {
+	//	for uId, num := range this.UserGroup {
+	//		if num > this.Conf.SaveClickTimes {
+	//			newMap[uId] = 0
+	//		}
+	//	}
+	//}
+	//新圈用户
+	for _, uId := range userArr {
+		newMap[uId] = 0
+	}
+	this.UserGroup = newMap
+
+	log.Printf("AheadManager NewGroup %v\n", newMap)
+
+}
+
+func sortUserByBatchAndGetFinal() {
+
 }

+ 32 - 11
entity/mananger/customManager.go

@@ -18,7 +18,7 @@ import (
 const (
 	customCacheDb           = "newother"
 	customDataCacheKey      = "leadGeneration_customData_%s"
-	customDataCacheTimeLong = 60 * 60 * 24 * 31
+	customDataCacheTimeLong = 60 * 60 * 24 * 30
 )
 
 // CustomManager 定制化报告管理
@@ -45,7 +45,9 @@ func InitCustomManager(conf vars.CustomConfig) *CustomManager {
 		Conf:      conf,
 		UserGroup: make(map[string]int),
 	}
+	//圈用户
 	go manager.ScheduledTasks()
+	//执行查询
 	go manager.DoSearch()
 	return manager
 }
@@ -94,20 +96,29 @@ func (this *CustomManager) ScheduledTasks() {
 // UpdateUserGroupJob 更新用户群组
 func (this *CustomManager) UpdateUserGroupJob() {
 	//查询月活用户
-	userMap := activeUsers.GetMonthActiveFreeUsers()
+	userArr := activeUsers.GetMonthActiveFreeUsers()
+
+	newMap := map[string]int{}
+	//测试账户
+	if len(vars.Config.TestUid) > 0 {
+		for _, uid := range vars.Config.TestUid {
+			newMap[uid] = 0
+		}
+	}
+
+	for _, uId := range userArr {
+		newMap[uId] = 0
+	}
+
+	//更新新用户群组
 	this.Lock()
 	defer this.Unlock()
-	this.UserGroup = userMap
-
+	this.UserGroup = newMap
 	//更新批次标识
 	batchFlag := time.Now().Format(public.Date_Full_Layout)
 	this.BatchFlag = batchFlag
-
-	userArr := make([]string, len(this.UserGroup), 0)
-	for userId, _ := range this.UserGroup {
-		userArr = append(userArr, userId)
-	}
-	go this.activityUserQueue(batchFlag, []string{})
+	log.Printf("CustomManager NewGroup %v\n", newMap)
+	go this.activityUserQueue(batchFlag, userArr)
 }
 
 // activityUserQueue 活跃用户查询队列
@@ -131,6 +142,9 @@ func (this *CustomManager) activityUserQueue(batchFlag string, userIds []string)
 // DoSearch 定制化分析报告查询队列
 func (this *CustomManager) DoSearch() {
 	for {
+		//是否在可执行时间段内
+		//???
+
 		var obj *SearchEntity
 		select { //优先级 newRegisterUserQueue > activityUserQueue
 		case obj = <-newRegisterUserQueue:
@@ -142,7 +156,7 @@ func (this *CustomManager) DoSearch() {
 		}
 		//查询结果处理
 		data := search.PotentialCustomizeAnalysis(obj.UserId, obj.Value)
-		if data != nil || len(data) == 0 {
+		if data == nil || len(data) == 0 {
 			log.Printf("[ERROR]CustomManager %s DoSearch %s Error %v\n", obj.UserId, obj.Value)
 			continue
 		}
@@ -155,6 +169,13 @@ func (this *CustomManager) DoSearch() {
 
 // checkActivityUser 校验用户是否是活跃用户
 func (this *CustomManager) checkActivityUser(userId string) (exists bool) {
+	if len(vars.Config.TestUid) > 0 {
+		for _, uid := range vars.Config.TestUid {
+			if uid == userId {
+				return true
+			}
+		}
+	}
 	this.RLock()
 	defer this.RUnlock()
 	_, exists = this.UserGroup[userId]

+ 13 - 0
entity/mananger/init.go

@@ -0,0 +1,13 @@
+package mananger
+
+import "leadGeneration/vars"
+
+var (
+	JyAheadManager  = &AheadManager{}
+	JyCustomManager = &CustomManager{}
+)
+
+func init() {
+	JyAheadManager = InitAheadManager(vars.Config.Ahead)
+	JyCustomManager = InitCustomManager(vars.Config.Custom)
+}

+ 1 - 0
main.go

@@ -3,6 +3,7 @@ package main
 import (
 	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
 	_ "leadGeneration/public"
+	_ "leadGeneration/services"
 	"leadGeneration/vars"
 	"net/http"
 )

+ 6 - 6
public/db.go

@@ -115,12 +115,12 @@ func init() {
 		if DbConf.Mysql.Active != nil {
 			log.Println("初始化 active mysql")
 			ActiveMysql = &mysql.Mysql{
-				Address:      DbConf.Mysql.Main.Address,
-				UserName:     DbConf.Mysql.Main.UserName,
-				PassWord:     DbConf.Mysql.Main.PassWord,
-				DBName:       DbConf.Mysql.Main.DbName,
-				MaxOpenConns: DbConf.Mysql.Main.MaxOpenConns,
-				MaxIdleConns: DbConf.Mysql.Main.MaxIdleConns,
+				Address:      DbConf.Mysql.Active.Address,
+				UserName:     DbConf.Mysql.Active.UserName,
+				PassWord:     DbConf.Mysql.Active.PassWord,
+				DBName:       DbConf.Mysql.Active.DbName,
+				MaxOpenConns: DbConf.Mysql.Active.MaxOpenConns,
+				MaxIdleConns: DbConf.Mysql.Active.MaxIdleConns,
 			}
 			ActiveMysql.Init()
 		}

+ 12 - 14
services/action.go

@@ -5,7 +5,7 @@ import (
 	qutil "app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
 	"app.yhyue.com/moapp/jybase/mongodb"
-	"leadGeneration/vars"
+	"leadGeneration/entity/mananger"
 	"log"
 	"time"
 )
@@ -30,13 +30,19 @@ func (this *LeadGeneration) GetDate() {
 
 		if !(t == 0 && keyWords == "") {
 			//获取超前项目
-			if aheadData := vars.JyAheadManager.GetData(userId, keyWords, isNewUser); aheadData != nil && len(aheadData) > 0 {
+			if aheadData := mananger.JyAheadManager.GetData(userId, keyWords, isNewUser); aheadData != nil && len(aheadData) > 0 {
 				rData["ahead"] = aheadData
 			}
 
 			//获取定制化报告数据
-			if customData := getCustomData(userId, keyWords, isNewUser); customData != nil && len(customData) > 0 {
-				rData["custom"] = customData
+			if customData := mananger.JyCustomManager.GetData(userId, keyWords, isNewUser); customData != nil && len(customData) > 0 {
+				//随机取一个图表
+				for k, v := range customData {
+					if v != nil {
+						rData[k] = v
+						break
+					}
+				}
 			}
 		}
 
@@ -52,19 +58,11 @@ func (this *LeadGeneration) GetDate() {
 func (this *LeadGeneration) ClickRecord() {
 	userId := qutil.ObjToString(this.GetSession("userId"))
 	rData, errMsg := func() (interface{}, error) {
-		return map[string]interface{}{}, nil
+		return true, nil
+		//return map[string]interface{}{}, nil
 	}()
 	if errMsg != nil {
 		log.Printf("%s LeadGeneration ClickRecord 异常:%s\n", userId, errMsg.Error())
 	}
 	this.ServeJson(NewResult(rData, errMsg))
 }
-
-func getAheadData(userId, keyWords string, isNew bool) map[string]interface{} {
-
-	return nil
-}
-
-func getCustomData(userId, keyWords string, isNew bool) map[string]interface{} {
-	return nil
-}

+ 1 - 9
vars/config.go

@@ -2,7 +2,6 @@ package vars
 
 import (
 	qutil "app.yhyue.com/moapp/jybase/common"
-	"leadGeneration/entity/mananger"
 )
 
 var Config *config
@@ -11,6 +10,7 @@ type config struct {
 	WebPort string       `json:"webPort"` //服务端口
 	Ahead   AheadConfig  `json:"ahead"`   //超前项目配置
 	Custom  CustomConfig `json:"custom"`  //定制化报告配置
+	TestUid []string     `json:"testUid"` //测试用户id
 }
 
 type AheadConfig struct {
@@ -25,15 +25,7 @@ type CustomConfig struct {
 	UpdateCron string `json:"updateCron"` //更新周活用户
 }
 
-var (
-	JyAheadManager  = &mananger.AheadManager{}
-	JyCustomManager = &mananger.CustomManager{}
-)
-
 func init() {
 	//程序配置文件
 	qutil.ReadConfig(&Config)
-	JyAheadManager = mananger.InitAheadManager(Config.Ahead)
-	JyCustomManager = mananger.InitCustomManager(Config.Custom)
-
 }