Explorar el Código

更新数据统计

zhengtao hace 9 meses
padre
commit
8b2bda0dc4

+ 5 - 0
extend/time/time_to.go

@@ -78,3 +78,8 @@ func (c MhayaTime) DailyTOTimeStamp() int64 {
 	t := time.Date(now.Year(), time.Month(now.Month()), now.Day(), 0, 0, 0, 0, now.Location())
 	return t.Unix()
 }
+
+func (c MhayaTime) TOTimeStamp() int64 {
+	t := time.Date(c.Year(), time.Month(c.Month()), c.Day(), 0, 0, 0, 0, c.Location())
+	return t.Unix()
+}

+ 12 - 2
game/game_cluster/internal/guid/guid.go

@@ -1,7 +1,9 @@
 package guid
 
 import (
+	"context"
 	csnowflake "github.com/mhaya/extend/snowflake"
+	"github.com/mhaya/game/game_cluster/internal/mdb"
 )
 
 // Next 生成唯一id
@@ -10,6 +12,14 @@ import (
 // redis
 func Next() string {
 	node, _ := csnowflake.NewNode(1)
-	id := node.Generate()
-	return id.Base58()
+	var sid string
+	for {
+		id := node.Generate()
+		sid = id.Base58()
+		if mdb.RDB.HExists(context.Background(), "PlayerID", sid).Val() {
+			continue
+		}
+		mdb.RDB.HSet(context.Background(), "PlayerID", sid, 1)
+		return sid
+	}
 }

+ 53 - 19
game/game_cluster/internal/mdb/models/PlayerStat.go

@@ -1,8 +1,13 @@
 package models
 
 import (
+	"fmt"
 	mhayaTime "github.com/mhaya/extend/time"
 	"github.com/mhaya/game/game_cluster/internal/data"
+	"github.com/mhaya/game/game_cluster/internal/guid"
+	clog "github.com/mhaya/logger"
+	"math/rand"
+	"time"
 )
 
 type PlayerLevelStat struct {
@@ -28,46 +33,59 @@ type PlayerCountryStat struct {
 }
 
 type Preserve struct {
-	ID    int64   `json:"id"`
-	Num   int     `json:"num"`
-	Ratio float64 `json:"ratio"`
+	ID    int64  `json:"id"`
+	Num   int    `json:"num"`
+	Ratio string `json:"ratio"`
 }
 
 var preserveConfig = []int64{2, 3, 7, 15, 30}
 
 // GetPlayerPreserve 获取用户留存
-func GetPlayerPreserve(startTime, EndTime int64) {
+func GetPlayerPreserve(startTime, EndTime int64) map[int64][]*Preserve {
 	if startTime > mhayaTime.Now().Unix() || EndTime > mhayaTime.Now().Unix() {
-		return
+		return nil
 	}
-	sTime := mhayaTime.CreateFromTimestamp(startTime).DailyTOTimeStamp()
+	sTime := mhayaTime.CreateFromTimestamp(startTime).TOTimeStamp()
 
 	if EndTime > mhayaTime.Now().Unix() {
 		EndTime = mhayaTime.Now().Unix()
 	}
-	eTime := mhayaTime.CreateFromTimestamp(EndTime).DailyTOTimeStamp()
-	mhayaTime.CreateFromTimestamp(sTime).DiffInDays(mhayaTime.CreateFromTimestamp(eTime))
+	eTime := mhayaTime.CreateFromTimestamp(EndTime).TOTimeStamp()
+	day := mhayaTime.CreateFromTimestamp(sTime).DiffInDays(mhayaTime.CreateFromTimestamp(eTime))
+	if day > 7 {
+		return nil
+	}
+	var ret = make(map[int64][]*Preserve, 0)
+	for i := 0; i < int(day); i++ {
+		curTime := mhayaTime.CreateFromTimestamp(sTime).Add(time.Duration(i) * 24 * time.Hour).Unix()
+		ret[curTime] = PlayerPreserve(curTime)
+	}
+	return ret
 }
 
-func PlayerPreserve(startTime, day int64) {
+func PlayerPreserve(startTime int64) []*Preserve {
 	var preserve []*Preserve
-	_, curNum := GetNewPlayerMap(startTime, DailyRecordNewRegistered)
-	if curNum < 1 {
-		return
-	}
+	cur, curNum := GetNewPlayerMap(startTime, DailyRecordNewRegistered)
 
-	preserve = append(preserve, &Preserve{ID: 1, Num: curNum, Ratio: 100})
+	preserve = append(preserve, &Preserve{ID: 1, Num: curNum, Ratio: "100%"})
 
 	for _, v := range preserveConfig {
-		if day < v {
-			preserve = append(preserve, &Preserve{ID: v, Num: 0, Ratio: 0})
+		nextTime := mhayaTime.CreateFromTimestamp(startTime).Add(time.Duration(v-1) * 24 * time.Hour).Unix()
+		next, nextNum := GetNewPlayerMap(nextTime, DailyRecordOldLogin)
+		if nextNum == 0 {
+			preserve = append(preserve, &Preserve{ID: v, Num: 0, Ratio: ""})
 		} else {
-			//	nextTime := mhayaTime.CreateFromTimestamp(startTime).AddDays(int(v) - 1)
-			//next, nextNum := GetNewPlayerMap(startTime, DailyRecordOldLogin)
-			preserve = append(preserve, &Preserve{ID: v, Num: 1, Ratio: 0})
+			var num int
+			for key, _ := range next {
+				if _, ok := cur[key]; ok {
+					num++
+				}
+			}
+			preserve = append(preserve, &Preserve{ID: v, Num: num, Ratio: fmt.Sprintf("%.2f", float64(num)/float64(curNum)*100)})
 		}
 
 	}
+	return preserve
 }
 
 func GetNewPlayerMap(startTime int64, op int) (map[string]struct{}, int) {
@@ -92,3 +110,19 @@ func GetNewPlayerMap(startTime int64, op int) (map[string]struct{}, int) {
 	}
 	return user, len(user)
 }
+
+func TestPreserve(startTime int64, num int) {
+	sTime := mhayaTime.CreateFromTimestamp(startTime).TOTimeStamp()
+	for i := 0; i < num; i++ {
+		userID := guid.Next()
+		clog.Debugf("num=%v, user =%v", i, userID)
+		SetAppointDailyRecordNewUserRegisterHash("ios", "tg", userID, "1", sTime, DailyRecordNewRegistered)
+		for _, v := range preserveConfig {
+			r := rand.Intn(100)
+			if r < 80 {
+				nextTime := mhayaTime.CreateFromTimestamp(sTime).Add(time.Duration(v-1) * 24 * time.Hour).Unix()
+				SetAppointDailyRecordNewUserRegisterHash("ios", "tg", userID, "1", nextTime, DailyRecordOldLogin)
+			}
+		}
+	}
+}

+ 9 - 0
game/game_cluster/internal/mdb/models/dailyRecord.go

@@ -7,6 +7,7 @@ import (
 	"github.com/mhaya/game/game_cluster/internal/constant"
 	"github.com/mhaya/game/game_cluster/internal/data"
 	"github.com/mhaya/game/game_cluster/internal/mdb"
+	clog "github.com/mhaya/logger"
 	"time"
 )
 
@@ -78,6 +79,14 @@ func SetDailyRecordNewUserRegisterHash(platform, channel, userName, ip string, o
 	mdb.RDB.HSet(context.Background(), key, userName, ip)
 }
 
+func SetAppointDailyRecordNewUserRegisterHash(platform, channel, userName, ip string, daily int64, op int) {
+	key := GetAppointDailyRecordKey(platform, channel, daily, op)
+	err := mdb.RDB.HSet(context.Background(), key, userName, ip).Err()
+	if err != nil {
+		clog.Errorf("set err:=%v", err)
+	}
+}
+
 func GetAllDailyRecordNewUserRegister(platform, channel string, op int) (map[string]string, error) {
 	key := GetDailyRecordKey(platform, channel, op)
 	return mdb.RDB.HGetAll(context.Background(), key).Result()

+ 2 - 0
game/game_cluster/nodes/web/controller/controller.go

@@ -58,6 +58,8 @@ func (p *Controller) Init() {
 
 	group.POST("/cashOut", p.cashOut)
 
+	group.POST("/test", p.test)
+
 	system := mhayaActor.NewSystem()
 	system.SetApp(p.App)
 	parentActor := &User{}

+ 33 - 0
game/game_cluster/nodes/web/controller/test.go

@@ -0,0 +1,33 @@
+package controller
+
+import (
+	mhayaGin "github.com/mhaya/components/gin"
+	mhayaTime "github.com/mhaya/extend/time"
+	"github.com/mhaya/game/game_cluster/internal/code"
+	"github.com/mhaya/game/game_cluster/internal/mdb/models"
+	"github.com/mhaya/game/game_cluster/internal/param"
+	mhayaLogger "github.com/mhaya/logger"
+	"time"
+)
+
+func (p *Controller) test(c *mhayaGin.Context) {
+
+	var pa param.ClaimReq
+	if err := c.BindJSON(&pa); err != nil {
+		mhayaLogger.Warnf("if share err {. params=%s", pa)
+		code.RenderResult(c, code.Error)
+		return
+	}
+	if pa.Tp == 0 {
+		code.RenderResult(c, code.Error)
+		return
+	}
+
+	if pa.Tp == 1 {
+		models.TestPreserve(mhayaTime.Now().Add(-60*24*time.Hour).Add(time.Duration(pa.Id)*24*time.Hour).Unix(), 10000)
+		code.RenderResult(c, code.OK)
+	} else {
+		rs := models.GetPlayerPreserve(int64(pa.Id), int64(pa.Id)+60*60*24*7)
+		code.RenderResult(c, code.OK, rs)
+	}
+}