优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/beans"
|
||||
"gitee.com/red-future---jilin-g/common/log/model/dto"
|
||||
"gitee.com/red-future---jilin-g/common/log/model/entity"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/os/grpool"
|
||||
|
||||
@@ -178,7 +178,7 @@ var logPool *grpool.Pool
|
||||
|
||||
// init 初始化MongoDB连接
|
||||
func init() {
|
||||
logPool = grpool.New(10)
|
||||
logPool = grpool.New(1)
|
||||
// 按需初始化:没有配置 mongo.address 则跳过
|
||||
mongoAddr = g.Cfg().MustGet(context.Background(), "mongo.address").String()
|
||||
if mongoAddr == "" {
|
||||
@@ -390,6 +390,33 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf
|
||||
return
|
||||
}
|
||||
|
||||
var serverName = g.Cfg().MustGet(context.TODO(), "server.name").String()
|
||||
var logRedisKey = fmt.Sprintf("log:%s", serverName)
|
||||
|
||||
func (m *MongoDB) log(ctx context.Context, filter bson.M, collection string, data interface{}, userName, tenantId interface{}, operationType string) {
|
||||
_ = logPool.AddWithRecover(ctx, func(ctx context.Context) {
|
||||
log := &entity.OperationLog{
|
||||
ServiceName: serverName,
|
||||
Collection: collection,
|
||||
CollectionID: filter["_id"].(string),
|
||||
Operation: operationType,
|
||||
IPAddress: g.RequestFromCtx(ctx).GetClientIp(),
|
||||
Data: data,
|
||||
}
|
||||
log.Creator = userName
|
||||
now := >ime.Now().Time
|
||||
log.CreatedAt = now
|
||||
log.UpdatedAt = now
|
||||
log.TenantId = tenantId
|
||||
if _, err := redis.AddToStream(ctx, logRedisKey, log); err != nil {
|
||||
glog.Error(ctx, "mongoLog-AddToStream err: %v", err)
|
||||
}
|
||||
}, func(ctx context.Context, exception error) {
|
||||
glog.Error(ctx, "mongoLog-AddWithRecover err: %v", exception)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除记录
|
||||
func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.DeleteManyOptions]) (count int64, err error) {
|
||||
if len(filter) == 0 {
|
||||
@@ -408,19 +435,7 @@ func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string,
|
||||
count = r.DeletedCount
|
||||
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||
//写日志
|
||||
var rows []interface{}
|
||||
if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
serverName := g.Cfg().MustGet(ctx, "server.name").String()
|
||||
logRedisKey := fmt.Sprintf("log:%s", serverName)
|
||||
if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{
|
||||
ServiceName: serverName,
|
||||
Collection: collection,
|
||||
Data: rows,
|
||||
}); err != nil {
|
||||
glog.Error(ctx, "mongoLog-AddToStream err: %v", err)
|
||||
}
|
||||
m.log(ctx, filter, collection, nil, user.UserName, user.TenantId, "delete")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -450,19 +465,7 @@ func (m *MongoDB) Update(ctx context.Context, filter bson.M, update bson.M, coll
|
||||
}
|
||||
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||
//写日志
|
||||
serverName := g.Cfg().MustGet(ctx, "server.name").String()
|
||||
logRedisKey := fmt.Sprintf("log:%s", serverName)
|
||||
var rows []interface{}
|
||||
if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{
|
||||
ServiceName: serverName,
|
||||
Collection: collection,
|
||||
Data: rows,
|
||||
}); err != nil {
|
||||
glog.Error(ctx, "mongoLog-AddToStream err: %v", err)
|
||||
}
|
||||
m.log(ctx, filter, collection, update, user.UserName, user.TenantId, "update")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -619,29 +622,7 @@ func (m *MongoDB) Insert(ctx context.Context, documents []interface{}, collectio
|
||||
ids = r.InsertedIDs
|
||||
err = m.CleanRedis(ctx, bson.M{}, user.TenantId, collection)
|
||||
//写日志
|
||||
serverName := g.Cfg().MustGet(ctx, "server.name").String()
|
||||
logRedisKey := fmt.Sprintf("log:%s", serverName)
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
rows := make([]interface{}, 0, len(ids))
|
||||
if len(ids) == 1 {
|
||||
doc := gconv.Map(documents[0])
|
||||
doc["id"] = ids[0]
|
||||
rows = append(rows, doc)
|
||||
} else {
|
||||
filter := bson.M{"_id": bson.M{"$in": ids}}
|
||||
if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{
|
||||
ServiceName: serverName,
|
||||
Collection: collection,
|
||||
Data: rows,
|
||||
}); err != nil {
|
||||
glog.Error(ctx, "mongoLog-AddToStream err: %v", err)
|
||||
}
|
||||
m.log(ctx, nil, collection, ids, user.UserName, user.TenantId, "insert")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user