优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -48,12 +48,3 @@ type ListLogsResp struct {
|
|||||||
Logs []OperationLogInfo `json:"logs" dc:"日志列表"`
|
Logs []OperationLogInfo `json:"logs" dc:"日志列表"`
|
||||||
Total int64 `json:"total" dc:"总数"`
|
Total int64 `json:"total" dc:"总数"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== 记录操作日志DTO ==========
|
|
||||||
|
|
||||||
// RecordCreateLogReq 记录创建操作日志请求
|
|
||||||
type RecordCreateLogReq struct {
|
|
||||||
ServiceName string `json:"service_name" v:"required" dc:"服务名"`
|
|
||||||
Collection string `json:"collection" v:"required" dc:"数据所在集合名称"`
|
|
||||||
Data []interface{} `json:"data" dc:"当前数据"`
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,11 +8,10 @@ import (
|
|||||||
type OperationLog struct {
|
type OperationLog struct {
|
||||||
beans.MongoBaseDO `bson:",inline"`
|
beans.MongoBaseDO `bson:",inline"`
|
||||||
|
|
||||||
ServiceName string `bson:"service_name" json:"service_name"` // 服务名:具体的微服务名称
|
ServiceName string `bson:"service_name" json:"service_name"` // 服务名:具体的微服务名称
|
||||||
Collection string `bson:"collection" json:"collection"` // 集合名:数据所在的集合名称
|
Collection string `bson:"collection" json:"collection"` // 集合名:数据所在的集合名称
|
||||||
CollectionID string `bson:"collection_id" json:"collection_id"` // 数据ID:具体操作的数据ID,如订单号、钱包ID等
|
CollectionID string `bson:"collection_id" json:"collection_id"` // 数据ID:具体操作的数据ID,如订单号、钱包ID等
|
||||||
Operation string `bson:"operation" json:"operation"` // 操作类型:create, update, delete
|
Operation string `bson:"operation" json:"operation"` // 操作类型:create, update, delete
|
||||||
UserName string `bson:"user_name" json:"user_name"` // 操作人名称
|
IPAddress string `bson:"ip_address" json:"ip_address"` // 操作IP地址
|
||||||
IPAddress string `bson:"ip_address" json:"ip_address"` // 操作IP地址
|
Data interface{} `bson:"data,omitempty" json:"data"` // 当前数据:操作时的数据状态
|
||||||
Data map[string]interface{} `bson:"data,omitempty" json:"data"` // 当前数据:操作时的数据状态
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"gitee.com/red-future---jilin-g/common/log/dao"
|
"gitee.com/red-future---jilin-g/common/log/dao"
|
||||||
"gitee.com/red-future---jilin-g/common/log/model/dto"
|
"gitee.com/red-future---jilin-g/common/log/model/dto"
|
||||||
logEntity "gitee.com/red-future---jilin-g/common/log/model/entity"
|
logEntity "gitee.com/red-future---jilin-g/common/log/model/entity"
|
||||||
"gitee.com/red-future---jilin-g/common/utils"
|
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
)
|
)
|
||||||
@@ -75,26 +74,14 @@ func (s *operationLog) List(ctx context.Context, req *dto.ListLogsReq) ([]dto.Op
|
|||||||
|
|
||||||
// record 记录操作日志的通用方法
|
// record 记录操作日志的通用方法
|
||||||
func (s *operationLog) record(ctx context.Context, serviceName, collection, collectionID, operation string, data map[string]interface{}) error {
|
func (s *operationLog) record(ctx context.Context, serviceName, collection, collectionID, operation string, data map[string]interface{}) error {
|
||||||
// 获取用户信息
|
|
||||||
user, err := utils.GetUserInfo(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取请求信息
|
// 获取请求信息
|
||||||
ipAddress := getHTTPRequestInfo(ctx)
|
ipAddress := getHTTPRequestInfo(ctx)
|
||||||
|
|
||||||
var userName string
|
|
||||||
if user.UserName != nil {
|
|
||||||
userName = gconv.String(user.UserName)
|
|
||||||
}
|
|
||||||
|
|
||||||
log := &logEntity.OperationLog{
|
log := &logEntity.OperationLog{
|
||||||
ServiceName: serviceName,
|
ServiceName: serviceName,
|
||||||
Collection: collection,
|
Collection: collection,
|
||||||
CollectionID: collectionID,
|
CollectionID: collectionID,
|
||||||
Operation: operation,
|
Operation: operation,
|
||||||
UserName: userName,
|
|
||||||
IPAddress: ipAddress,
|
IPAddress: ipAddress,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitee.com/red-future---jilin-g/common/beans"
|
"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/container/gvar"
|
||||||
"github.com/gogf/gf/v2/os/grpool"
|
"github.com/gogf/gf/v2/os/grpool"
|
||||||
|
|
||||||
@@ -178,7 +178,7 @@ var logPool *grpool.Pool
|
|||||||
|
|
||||||
// init 初始化MongoDB连接
|
// init 初始化MongoDB连接
|
||||||
func init() {
|
func init() {
|
||||||
logPool = grpool.New(10)
|
logPool = grpool.New(1)
|
||||||
// 按需初始化:没有配置 mongo.address 则跳过
|
// 按需初始化:没有配置 mongo.address 则跳过
|
||||||
mongoAddr = g.Cfg().MustGet(context.Background(), "mongo.address").String()
|
mongoAddr = g.Cfg().MustGet(context.Background(), "mongo.address").String()
|
||||||
if mongoAddr == "" {
|
if mongoAddr == "" {
|
||||||
@@ -390,6 +390,33 @@ func (m *MongoDB) CleanRedis(ctx context.Context, filter bson.M, tenantId interf
|
|||||||
return
|
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 删除记录
|
// Delete 删除记录
|
||||||
func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.DeleteManyOptions]) (count int64, err error) {
|
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 {
|
if len(filter) == 0 {
|
||||||
@@ -408,19 +435,7 @@ func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string,
|
|||||||
count = r.DeletedCount
|
count = r.DeletedCount
|
||||||
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||||
//写日志
|
//写日志
|
||||||
var rows []interface{}
|
m.log(ctx, filter, collection, nil, user.UserName, user.TenantId, "delete")
|
||||||
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)
|
|
||||||
}
|
|
||||||
return
|
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)
|
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||||
//写日志
|
//写日志
|
||||||
serverName := g.Cfg().MustGet(ctx, "server.name").String()
|
m.log(ctx, filter, collection, update, user.UserName, user.TenantId, "update")
|
||||||
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)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -619,29 +622,7 @@ func (m *MongoDB) Insert(ctx context.Context, documents []interface{}, collectio
|
|||||||
ids = r.InsertedIDs
|
ids = r.InsertedIDs
|
||||||
err = m.CleanRedis(ctx, bson.M{}, user.TenantId, collection)
|
err = m.CleanRedis(ctx, bson.M{}, user.TenantId, collection)
|
||||||
//写日志
|
//写日志
|
||||||
serverName := g.Cfg().MustGet(ctx, "server.name").String()
|
m.log(ctx, nil, collection, ids, user.UserName, user.TenantId, "insert")
|
||||||
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)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user