wangkaiyue 2 жил өмнө
parent
commit
6b9edb7cfb

+ 2 - 2
db.json

@@ -37,9 +37,9 @@
 	    },
 		"active": {
 			"dbName": "useranaly",
-			"address": "192.168.3.11:3366",
+			"address": "192.168.3.217:4000",
 			"userName": "root",
-			"passWord": "Topnet123",
+			"passWord": "=PDT49#80Z!RVv52_z",
 			"maxOpenConns": 5,
 			"maxIdleConns": 5
 		}

+ 2 - 2
entity/activeUsers/activeUsers.go

@@ -28,7 +28,7 @@ func GetWeekActiveFreeUsers() (rData []string) {
 		sql = "SELECT DISTINCT(user_mongoid) FROM user_countbyhour WHERE day >= ? AND day < ?"
 		param = []interface{}{start_d, end_d}
 	}
-	res := public.ActiveMysql.SelectBySql(sql, param...)
+	res := public.UserAnalyseDb.SelectBySql(sql, param...)
 
 	if res == nil || len(*res) == 0 {
 		return
@@ -48,7 +48,7 @@ func GetWeekActiveFreeUsers() (rData []string) {
 // 有订阅或有搜索词的周活用户+最近7天注册的新用户无条件生
 func GetMonthActiveFreeUsers() (rData []string) {
 	//表数据为近一个月,所以直接查询全部
-	res := public.ActiveMysql.SelectBySql("SELECT user_mongoid,sum(search) AS total FROM user_countbyhour group by user_mongoid")
+	res := public.UserAnalyseDb.SelectBySql("SELECT user_mongoid,sum(search) AS total FROM user_countbyhour group by user_mongoid")
 	if res == nil || len(*res) == 0 {
 		return
 	}

+ 43 - 0
entity/activeUsers/groupSave.go

@@ -0,0 +1,43 @@
+package activeUsers
+
+import (
+	"app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/date"
+	"leadGeneration/public"
+	"log"
+	"time"
+)
+
+const batchNum = 100 //批次插入限制
+
+// SaveBatchGroup 保存批次分组信息
+func SaveBatchGroup(userIds []string, groupType int, groupName, from string) {
+	arrLen := len(userIds)
+	if arrLen == 0 {
+		return
+	}
+	now := time.Now().Format(date.Date_Full_Layout)
+	var finalCount int64 = 0
+	for i := 0; i < (arrLen/batchNum)+1; i++ {
+		start := batchNum * i
+		end := (batchNum + 1) * 100
+		if end >= arrLen {
+			end = arrLen
+		}
+		values := make([]interface{}, 0, batchNum)
+		for _, uId := range userIds[start:end] {
+			values = append(values, uId, groupType, groupName, from, now)
+		}
+		if count, _ := public.UserAnalyseDb.InsertBatch("user_leadGeneration_group",
+			[]string{"user_mongoid", "group_type", "group_name", "source", "create_time"}, values); count > 0 {
+			finalCount += count
+		} else {
+			log.Printf("[ERROR] SaveBatchGroup index:%d value:%v error\n", i, values)
+		}
+	}
+	if common.Int64All(arrLen) == finalCount {
+		log.Printf("[INFO] SaveBatchGroup save %v %s %s total %d finished:%d\n", common.If(groupType > 0, "定制化分析报告", "超前项目"), groupName, from, arrLen, finalCount)
+	} else {
+		log.Printf("[ERROR] SaveBatchGroup save %v %s %s total %d finished:%d\n", common.If(groupType > 0, "定制化分析报告", "超前项目"), groupName, from, arrLen, finalCount)
+	}
+}

+ 24 - 16
entity/mananger/aheadManager.go

@@ -17,6 +17,7 @@ import (
 type AheadManager struct {
 	Conf      vars.AheadConfig
 	UserGroup map[string]int
+	BatchFlag string
 	sync.RWMutex
 }
 
@@ -88,37 +89,44 @@ func (this *AheadManager) UpdateUserGroupJob() {
 		return
 	}
 
-	//查询上批次活跃用户
 	newMap := map[string]int{}
-	var userArr []string
-	//测试账户
+	var userArr, oldActive []string
+
 	if len(vars.Config.TestUid) > 0 {
+		//测试模式
 		for _, uid := range vars.Config.TestUid {
-			newMap[uid] = 0
+			userArr = append(userArr, uid)
 		}
 	} else {
 		//查询月活用户
-		userArr = activeUsers.GetWeekActiveFreeUsers()
+		userArr = this.sortUserByBatchAndGetFinal(activeUsers.GetWeekActiveFreeUsers())
 	}
 	this.Lock()
 	defer this.Unlock()
-	//if len(this.UserGroup) != 0 {
-	//	for uId, num := range this.UserGroup {
-	//		if num > this.Conf.SaveClickTimes {
-	//			newMap[uId] = 0
-	//		}
-	//	}
-	//}
+	if len(this.UserGroup) != 0 {
+		for uId, num := range this.UserGroup {
+			if num > this.Conf.SaveClickTimes {
+				oldActive = append(oldActive, uId)
+			}
+		}
+	}
+	this.BatchFlag = time.Now().Format(public.Date_Full_Layout)
+	//保存批次记录
+	go activeUsers.SaveBatchGroup(oldActive, 0, this.BatchFlag, "oldActive")
+	go activeUsers.SaveBatchGroup(userArr, 0, this.BatchFlag, "newActive")
+	//旧活跃用户
+	for _, uId := range oldActive {
+		newMap[uId] = 0
+	}
 	//新圈用户
 	for _, uId := range userArr {
 		newMap[uId] = 0
 	}
 	this.UserGroup = newMap
-
 	log.Printf("AheadManager NewGroup %v\n", newMap)
-
 }
 
-func sortUserByBatchAndGetFinal() {
-
+// sortUserByBatchAndGetFinal 根据百分比取用户
+func (this *AheadManager) sortUserByBatchAndGetFinal(userId []string) (rData []string) {
+	return nil
 }

+ 2 - 3
entity/mananger/customManager.go

@@ -97,13 +97,12 @@ func (this *CustomManager) ScheduledTasks() {
 func (this *CustomManager) UpdateUserGroupJob() {
 	var userArr []string
 	newMap := map[string]int{}
-	//测试账户
 	if len(vars.Config.TestUid) > 0 {
+		//测试模式
 		for _, uid := range vars.Config.TestUid {
 			newMap[uid] = 0
 			userArr = append(userArr, uid)
 		}
-
 	} else {
 		//查询月活用户
 		userArr = activeUsers.GetMonthActiveFreeUsers()
@@ -121,6 +120,7 @@ func (this *CustomManager) UpdateUserGroupJob() {
 	this.BatchFlag = batchFlag
 	log.Printf("CustomManager NewGroup %v\n", newMap)
 	go this.activityUserQueue(batchFlag, userArr)
+	go activeUsers.SaveBatchGroup(userArr, 1, this.BatchFlag, "newActive")
 }
 
 // activityUserQueue 活跃用户查询队列
@@ -146,7 +146,6 @@ func (this *CustomManager) DoSearch() {
 	for {
 		//是否在可执行时间段内
 		//???
-
 		var obj *SearchEntity
 		select { //优先级 newRegisterUserQueue > activityUserQueue
 		case obj = <-newRegisterUserQueue:

+ 7 - 8
public/db.go

@@ -10,11 +10,11 @@ import (
 )
 
 var (
-	DbConf      *dbConf
-	MainMysql   *mysql.Mysql
-	ActiveMysql *mysql.Mysql
-	MQFW        m.MongodbSim
-	Mgo_Log     m.MongodbSim
+	DbConf        *dbConf
+	MainMysql     *mysql.Mysql //主程序订单表
+	UserAnalyseDb *mysql.Mysql //用户分析
+	MQFW          m.MongodbSim
+	Mgo_Log       m.MongodbSim
 )
 
 type dbConf struct {
@@ -114,7 +114,7 @@ func init() {
 
 		if DbConf.Mysql.Active != nil {
 			log.Println("初始化 active mysql")
-			ActiveMysql = &mysql.Mysql{
+			UserAnalyseDb = &mysql.Mysql{
 				Address:      DbConf.Mysql.Active.Address,
 				UserName:     DbConf.Mysql.Active.UserName,
 				PassWord:     DbConf.Mysql.Active.PassWord,
@@ -122,8 +122,7 @@ func init() {
 				MaxOpenConns: DbConf.Mysql.Active.MaxOpenConns,
 				MaxIdleConns: DbConf.Mysql.Active.MaxIdleConns,
 			}
-			ActiveMysql.Init()
+			UserAnalyseDb.Init()
 		}
-
 	}
 }