浏览代码

feat:优化

wangchuanjin 1 年之前
父节点
当前提交
ac1a9ae537
共有 4 个文件被更改,包括 66 次插入10 次删除
  1. 7 0
      activeStartTip/config.json
  2. 12 2
      activeStartTip/config/config.go
  3. 1 1
      activeStartTip/go.mod
  4. 46 7
      activeStartTip/main.go

+ 7 - 0
activeStartTip/config.json

@@ -4,6 +4,13 @@
       "address": "192.168.3.206:27080",
       "size": 2,
       "dbName": "qfw"
+    },
+    "log": {
+      "address": "192.168.3.206:27090",
+      "size": 5,
+      "dbName": "qfw",
+      "userName": "admin",
+      "password": "123456"
     }
   },
   "etcd": {

+ 12 - 2
activeStartTip/config/config.go

@@ -7,8 +7,9 @@ import (
 )
 
 var (
-	Config *config
-	Mgo    *mongodb.MongodbSim
+	Config  *config
+	Mgo     *mongodb.MongodbSim
+	Mgo_Log *mongodb.MongodbSim
 )
 
 type MsgConf struct {
@@ -27,6 +28,7 @@ type etcdConf struct {
 type config struct {
 	Mongodb struct {
 		Main *mongo
+		Log  *mongo
 	}
 	Etcd        etcdConf
 	Webdomain   string
@@ -52,4 +54,12 @@ func init() {
 		DbName:      Config.Mongodb.Main.DbName,
 	}
 	Mgo.InitPool()
+	Mgo_Log = &mongodb.MongodbSim{
+		MongodbAddr: Config.Mongodb.Log.Address,
+		Size:        Config.Mongodb.Log.Size,
+		DbName:      Config.Mongodb.Log.DbName,
+		UserName:    Config.Mongodb.Log.UserName,
+		Password:    Config.Mongodb.Log.Password,
+	}
+	Mgo_Log.InitPool()
 }

+ 1 - 1
activeStartTip/go.mod

@@ -6,6 +6,7 @@ require (
 	app.yhyue.com/moapp/MessageCenter v0.0.0-20231026080417-49f63bf75554
 	app.yhyue.com/moapp/jybase v0.0.0-20231011022039-fa0f75e5282f
 	github.com/zeromicro/go-zero v1.5.5
+	go.mongodb.org/mongo-driver v1.12.1
 )
 
 require (
@@ -58,7 +59,6 @@ require (
 	go.etcd.io/etcd/api/v3 v3.5.9 // indirect
 	go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
 	go.etcd.io/etcd/client/v3 v3.5.9 // indirect
-	go.mongodb.org/mongo-driver v1.12.1 // indirect
 	go.opentelemetry.io/otel v1.14.0 // indirect
 	go.opentelemetry.io/otel/exporters/jaeger v1.14.0 // indirect
 	go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect

+ 46 - 7
activeStartTip/main.go

@@ -5,13 +5,17 @@ import (
 	. "activeStartTip/rpc"
 	"log"
 	"sync"
+	"sync/atomic"
 	"time"
 
 	"app.yhyue.com/moapp/MessageCenter/rpc/type/message"
+	"app.yhyue.com/moapp/jybase/common"
 	. "app.yhyue.com/moapp/jybase/mongodb"
+	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
 func main() {
+	allUser := loadAllUser()
 	pool := make(chan bool, Config.SendMsgPool)
 	wait := &sync.WaitGroup{}
 	sess := Mgo.GetMgoConn()
@@ -38,9 +42,10 @@ func main() {
 	}
 	log.Println("start...", query)
 	it := sess.DB("qfw").C("user").Find(query).Select(map[string]interface{}{
-		"_id": 1,
-	}).Iter()
-	index := 0
+		"_id":          1,
+		"base_user_id": 1,
+	}).Sort("_id").Iter()
+	var index int64
 	for m := make(map[string]interface{}); it.Next(&m); {
 		pool <- true
 		wait.Add(1)
@@ -49,6 +54,14 @@ func main() {
 				<-pool
 				wait.Done()
 			}()
+			if !allUser[common.Int64All(u["base_user_id"])] {
+				return
+			}
+			rs := atomic.AddInt64(&index, 1)
+			rs++
+			if rs%5000 == 0 {
+				log.Println("index", rs)
+			}
 			_id := BsonIdToSId(u["_id"])
 			SendMsg(&message.MultipleSaveMsgReq{
 				UserIds:    _id,
@@ -61,11 +74,37 @@ func main() {
 				IosPushUrl: "/jy_mobile/points/earn",
 			})
 		}(m)
-		index++
-		if index%500 == 0 {
-			log.Println("index", index)
-		}
 	}
 	wait.Wait()
 	log.Println("over...", index)
 }
+
+func loadAllUser() map[int64]bool {
+	_id := primitive.NewObjectIDFromTimestamp(time.Now().AddDate(-1, 0, 0))
+	log.Println("开始加载最近一年活跃用户。。。", _id)
+	allUser := map[int64]bool{}
+	sess := Mgo.GetMgoConn()
+	defer Mgo.DestoryMongoConn(sess)
+	it := sess.DB("qfw").C("jy_gateway_logs").Find(map[string]interface{}{
+		"_id": map[string]interface{}{
+			"$gt": _id,
+		},
+	}).Select(map[string]interface{}{
+		"_id":        0,
+		"userid_new": 1,
+	}).Iter()
+	var index int64
+	for m := make(map[string]interface{}); it.Next(&m); {
+		index++
+		if index%50000 == 0 {
+			log.Println("加载最近一年活跃用户", index)
+		}
+		userid_new := common.Int64All(m["userid_new"])
+		if userid_new <= 0 {
+			continue
+		}
+		allUser[userid_new] = true
+	}
+	log.Println("加载最近一年活跃用户结束。。。", _id, index)
+	return allUser
+}