@@ -0,0 +1,121 @@
|
||||
package dailyretentionmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const table = models.DailyRetention
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "date", Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "createdAt", Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "adGroup", Value: 1},
|
||||
{Key: "date", Value: -1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Init 初始化索引
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
// QueryAllList 分页查询文档
|
||||
func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*DailyRetention, err error) {
|
||||
if err = coll(nil).Find(&out, filter, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllList", table, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("opts", opts),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// QueryOne
|
||||
func QueryOne(filter bson.M) (*DailyRetention, error) {
|
||||
v := DailyRetention{}
|
||||
if err := coll(nil).FindOne(&v, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryOne", table, "FindOne", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return &v, err
|
||||
}
|
||||
|
||||
if v.Id.IsZero() {
|
||||
return nil, errors.New("record not found")
|
||||
}
|
||||
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
// UpsertOne
|
||||
func UpsertOne(filter bson.M, updateData bson.M) (err error) {
|
||||
if _, err = coll(nil).UpsertOne(filter, updateData); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpsertOne", table, "UpdateOne", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("updateData", updateData),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func BulkWrite(models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (err error) {
|
||||
if len(models) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err = coll(nil).Bulk(models, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "BulkWrite", table, "BulkWrite", err),
|
||||
log.Any("modelsCount", len(models)),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Distinct(filter bson.M, fieldName string, opts ...*options.DistinctOptions) (out []*DailyRetention, err error) {
|
||||
raw, err := coll(nil).Distinct(fieldName, filter, opts...)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Distinct", table, "UpdateOne", err),
|
||||
log.Any("fieldName", fieldName),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return
|
||||
}
|
||||
out = make([]*DailyRetention, len(raw))
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package dailyretentionmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models/commod"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const (
|
||||
MaxRetentionDays = 30 // 最大留存天数
|
||||
)
|
||||
|
||||
// DailyRetention 每日用户留存统计
|
||||
// @table:daily_retention
|
||||
// @model:DailyRetentionModel
|
||||
type DailyRetention struct {
|
||||
Id primitive.ObjectID `json:"id" bson:"_id"` // 自增ID
|
||||
Date time.Time `json:"date" bson:"date" index:"date_idx, date_adgroup_idx:unique"` // 日期,当日0点
|
||||
AdGroup commod.AdGroup `json:"adGroup" bson:"adGroup" index:"adgroup_idx, date_adgroup_idx:unique"` // 广告组
|
||||
NewUsers int64 `json:"newUsers" bson:"newUsers"` // 当天新增用户
|
||||
UserRetained map[int]int64 `json:"userRetained" bson:"userRetained"` // key = 第几天, value = 日活留存人数
|
||||
UserRates map[int]float64 `json:"userRates" bson:"userRates"` // key = 第几天, value = 日活留存率
|
||||
AdClickAcc map[int]int64 `json:"adClickAcc" bson:"adClickAcc"` // key = 第几天, value = 广告点击累计
|
||||
TotalClickAcc map[int]int64 `json:"totalClickAcc" bson:"totalClickAcc"` // key = 第几天, value = 总点击累计
|
||||
PayCountAcc map[int]int64 `json:"payCountAcc" bson:"payCountAcc"` // key = 第几天, value = 充值次数累计
|
||||
PayTotalAcc map[int]int64 `json:"payTotalAcc" bson:"payTotalAcc"` // key = 第几天, value = 充值总金额累计
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 更新时间
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt" index:"-created_at_idx"` // 创建时间
|
||||
}
|
||||
Reference in New Issue
Block a user