forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package downloadstat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
// 初始化model
|
||||
func Init() {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, CollDownloadStat)
|
||||
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "appId", Value: 1},
|
||||
{Key: "type", Value: 1},
|
||||
{Key: "sumDate", Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "type", Value: 1},
|
||||
{Key: "sumDate", Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "type", Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "hour", Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "createdAt", Value: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollDownloadStat, err))
|
||||
}
|
||||
}
|
||||
|
||||
// CountStats 统计数量
|
||||
func CountStats(ctx context.Context, filter bson.M) (int64, error) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("downloadStat CountStats db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// InsertOneStat 插入一个
|
||||
func InsertOneStat(ctx context.Context, media *Stat) error {
|
||||
if _, err := db.InsertOne(ctx, media); err != nil {
|
||||
log.Warn("downloadStat:InsertOneStat db.InsertOne failed", log.E(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindOneStat 查找一个
|
||||
func FindOneStat(ctx context.Context, filter bson.M) (*Stat, error) {
|
||||
var stat Stat
|
||||
if err := db.FindOneEx(ctx, &stat, filter); err != nil {
|
||||
log.Warn("downloadStat:FindOneStatEx db.FindOneEx failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, err
|
||||
}
|
||||
return &stat, nil
|
||||
}
|
||||
|
||||
// DelOneStat 删除一个
|
||||
func DelOneStat(ctx context.Context, filter bson.M) (int64, error) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("downloadStat:DelOneStat db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneStat 更新一个
|
||||
func UpdateOneStat(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
_, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("downloadStat:UpdateOneStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyStat 查找一批
|
||||
func FindManyStat(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (StatList, error) {
|
||||
data := make(StatList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("downloadStat:FindManyStat db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// AggregateStats 聚合查找一批机器人小时统计信息
|
||||
func AggregateStats(ctx context.Context, bind interface{}, pipeline []bson.M) error {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("downloadStat:FindManyStat db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user