forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package applicationstat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"stat/common/stdLog"
|
||||
)
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, Collapplicationstat)
|
||||
// 获取数据库表
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "applicationDbId", Value: 1},
|
||||
{Key: FN.ApplicationStat.SumData, Value: -1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.ApplicationStat.Id, Value: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(applicationstat:initApplicationStatIndex) Create ApplicationStat Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneApplicationStat 插入一个应用
|
||||
func InsertOneApplicationStat(ctx context.Context, applicationStat *ApplicationStat) *stdLog.SrvErr {
|
||||
if _, err := db.InsertOne(ctx, applicationStat); err != nil {
|
||||
log.Warn("applicationstat:InsertOneApplicationStat db.InsertOne failed", log.E(err))
|
||||
return stdLog.New(stdLog.InsertDataError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelOneApplicationStat 删除一个应用tag
|
||||
func DelOneApplicationStat(ctx context.Context, filter bson.M) (
|
||||
int64, *stdLog.SrvErr) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:DelOneApplicationStat db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, stdLog.New(stdLog.DelDataError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindOneApplicationStat 查找一个应用
|
||||
func FindOneApplicationStat(ctx context.Context, filter bson.M) (
|
||||
*ApplicationStat, *stdLog.SrvErr) {
|
||||
var applicationStat ApplicationStat
|
||||
if err := db.FindOneEx(
|
||||
ctx, &applicationStat, filter); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Info("applicationstat:FindOneApplicationStat db.FindOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, stdLog.New(stdLog.QueryDataError, "", "")
|
||||
}
|
||||
return nil, stdLog.New(stdLog.DataNotExist, "", "")
|
||||
}
|
||||
return &applicationStat, nil
|
||||
}
|
||||
|
||||
// CountApplicationStats 统计应用数量
|
||||
func CountApplicationStats(ctx context.Context, filter bson.M) (
|
||||
int64, *stdLog.SrvErr) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:CountApplicationStats db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, stdLog.New(stdLog.QueryDataError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneApplicationStat 更新一个应用
|
||||
func UpdateOneApplicationStat(ctx context.Context, filter bson.M, update bson.M) *stdLog.SrvErr {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:UpdateOneApplicationStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return stdLog.New(stdLog.UpdateDataError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
log.Warn("applicationstat:UpdateOneApplicationStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return stdLog.New(stdLog.DataNotExist, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateManyApplicationStat 更新一批应用
|
||||
func UpdateManyApplicationStat(ctx context.Context, filter bson.M, update bson.M) *stdLog.SrvErr {
|
||||
result, err := db.UpdateMMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:UpdateManyApplicationStat db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return stdLog.New(stdLog.UpdateDataError, "", "")
|
||||
}
|
||||
if result.MatchedCount <= 0 {
|
||||
log.Warn("applicationstat:UpdateManyApplicationStat db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
//return srvdef.New(srvdef.ErrApplicationNotExsit, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AggregateTopicInfos 聚合查找一批用户信息
|
||||
func AggregateApplicationStat(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) *stdLog.SrvErr {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("feedback:AggregateApplicationStat db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return stdLog.New(stdLog.QueryDataError, "", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyApplicationStat 查找一批应用
|
||||
func FindManyApplicationStat(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) (ApplicationStatList, *stdLog.SrvErr) {
|
||||
data := make(ApplicationStatList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("applicationstat:FindManyApplicationStat db.Find failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, stdLog.New(stdLog.QueryDataError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user