forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,445 @@
|
||||
package channelCodeBlocStatMod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
db = mongos.New(mongos.MgoManager, CollChannelCodeBlocStat)
|
||||
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "channelCode", Value: 1}, {Key: "appId", Value: 1}, {Key: "sumDate", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "channel", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "appId", Value: 1}},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollChannelCodeBlocStat, err))
|
||||
}
|
||||
}
|
||||
|
||||
// UpsertOneUserRegisterStat 更新或插入一条用户注册信息统计信息
|
||||
func UpsertOneUserRegisterStat(ctx context.Context, code string, appId int32, sumDate time.Time, direct, fission, deduction int64) (err error) {
|
||||
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
||||
|
||||
update := bson.M{}
|
||||
if direct > 0 {
|
||||
update["newDirectCount"] = direct
|
||||
}
|
||||
if fission > 0 {
|
||||
update["newFissionCount"] = fission
|
||||
}
|
||||
if deduction > 0 {
|
||||
update["newDeductionCount"] = deduction
|
||||
}
|
||||
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter, bson.M{"$set": update})
|
||||
} else {
|
||||
insertDoc := update
|
||||
insertDoc["sumDate"] = sumDate
|
||||
insertDoc["appId"] = appId
|
||||
insertDoc["channelCode"] = code
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// UpsertOneUserRechargeStat 更新或插入一条用户消费统计信息
|
||||
func UpsertOneUserRechargeStat(ctx context.Context, code string, appId int32, sumDate time.Time, rechargeAmount, deductionAmount, fissionAmount, newRechargeAmount int64) (err error) {
|
||||
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
||||
|
||||
update := bson.M{}
|
||||
if rechargeAmount > 0 {
|
||||
update["directAmount"] = rechargeAmount
|
||||
}
|
||||
if deductionAmount > 0 {
|
||||
update["deductionAmount"] = deductionAmount
|
||||
}
|
||||
if fissionAmount > 0 {
|
||||
update["fissionAmount"] = fissionAmount
|
||||
}
|
||||
|
||||
if newRechargeAmount > 0 {
|
||||
update["newRechargeAmount"] = newRechargeAmount
|
||||
}
|
||||
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter, bson.M{"$inc": update})
|
||||
} else {
|
||||
insertDoc := update
|
||||
insertDoc["sumDate"] = sumDate
|
||||
insertDoc["appId"] = appId
|
||||
insertDoc["channelCode"] = code
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// UpsertOneDistrictStat 更新渠道的留存
|
||||
func UpsertOneDistrictStat(ctx context.Context, code string, appId int32, sumDate time.Time, day1Rate, day3Rate, day7Rate *float64) (err error) {
|
||||
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
||||
|
||||
update := bson.M{}
|
||||
if day1Rate != nil {
|
||||
update["day1RetainRate"] = *day1Rate
|
||||
}
|
||||
if day3Rate != nil {
|
||||
update["day3RetainRate"] = *day3Rate
|
||||
}
|
||||
|
||||
if day7Rate != nil {
|
||||
update["day7RetainRate"] = *day7Rate
|
||||
}
|
||||
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter, bson.M{"$set": update})
|
||||
} else {
|
||||
insertDoc := update
|
||||
insertDoc["sumDate"] = sumDate
|
||||
insertDoc["appId"] = appId
|
||||
insertDoc["channelCode"] = code
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// UpsertOneDistrictVisitStat 更新渠道的留存
|
||||
func UpsertOneDistrictVisitStat(ctx context.Context, code string, appId int32, sumDate time.Time, directVisitCount, fissionVisitCount int64) (err error) {
|
||||
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
||||
|
||||
update := bson.M{"directVisitCount": directVisitCount, "fissionVisitCount": fissionVisitCount}
|
||||
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter, bson.M{"$set": update})
|
||||
} else {
|
||||
insertDoc := update
|
||||
insertDoc["sumDate"] = sumDate
|
||||
insertDoc["appId"] = appId
|
||||
insertDoc["channelCode"] = code
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// UpsertOneDistrictCpc 更新渠道的留存
|
||||
func UpsertOneDistrictCpc(ctx context.Context, code string, appId int32, sumDate time.Time, visitCount int64) (err error) {
|
||||
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
||||
|
||||
update := bson.M{"landPageVisitCount": visitCount}
|
||||
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter, bson.M{"$set": update})
|
||||
} else {
|
||||
insertDoc := update
|
||||
insertDoc["sumDate"] = sumDate
|
||||
insertDoc["appId"] = appId
|
||||
insertDoc["channelCode"] = code
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// UpsertOneDistrictRechargeStat 更新渠道的新增充值
|
||||
func UpsertOneDistrictRechargeStat(ctx context.Context, code string, appId int32, sumDate time.Time, directAmount, fissionAmount, deduction int64) (err error) {
|
||||
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
||||
|
||||
update := bson.M{"fissionAmount": fissionAmount, "directAmount": directAmount, "deductionAmount": deduction}
|
||||
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter, bson.M{"$set": update})
|
||||
} else {
|
||||
insertDoc := update
|
||||
insertDoc["sumDate"] = sumDate
|
||||
insertDoc["appId"] = appId
|
||||
insertDoc["channelCode"] = code
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 后台获取统计信息
|
||||
func GetChannelCodeBlocStatAgentList(ctx context.Context, channelCodes []string, appId int32, start, end *time.Time, skip, limit int64) (count int64, data ChannelCodeBlocStatList, err error) {
|
||||
if len(channelCodes) == 0 {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"channelCode": bson.M{"$in": channelCodes}, "appId": appId}
|
||||
if start != nil && end != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
||||
} else if start != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start}
|
||||
} else if end != nil {
|
||||
filter["sumDate"] = bson.M{"$lt": end}
|
||||
}
|
||||
|
||||
sort := bson.D{{Key: "sumDate", Value: -1}, {Key: "newDirectCount", Value: -1}}
|
||||
|
||||
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 后台获取统计信息
|
||||
func GetChannelCodeBlocStatAgentListTotal(ctx context.Context, channelCodes []string, appId int32, start, end *time.Time) (data ChannelCodeBlocStatList, err error) {
|
||||
if len(channelCodes) == 0 {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"channelCode": bson.M{"$in": channelCodes}, "appId": appId}
|
||||
if start != nil && end != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
||||
} else if start != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start}
|
||||
} else if end != nil {
|
||||
filter["sumDate"] = bson.M{"$lt": end}
|
||||
}
|
||||
|
||||
sort := bson.D{{Key: "sumDate", Value: -1}, {Key: "newDirectCount", Value: -1}}
|
||||
|
||||
opts := options.Find().SetSort(sort)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 后台获取统计信息总数
|
||||
func GetBlocChannelCodeBlocStatListTotal(ctx context.Context, channel *string, appId *int32, start, end *time.Time) (data ChannelCodeBlocStatList, err error) {
|
||||
filter := bson.M{}
|
||||
|
||||
if channel != nil {
|
||||
filter["channelCode"] = channel
|
||||
}
|
||||
|
||||
if appId != nil {
|
||||
filter["appId"] = appId
|
||||
}
|
||||
if start != nil && end != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
||||
} else if start != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start}
|
||||
} else if end != nil {
|
||||
filter["sumDate"] = bson.M{"$lt": end}
|
||||
}
|
||||
|
||||
if err = db.Find(ctx, &data, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 后台获取统计信息
|
||||
func GetChannelCodeBlocStatList(ctx context.Context, channelCodes []string, appId *int32, start, end *time.Time, skip, limit int64) (count int64, data ChannelCodeBlocStatList, err error) {
|
||||
filter := bson.M{}
|
||||
if len(channelCodes) > 0 {
|
||||
filter["channelCode"] = bson.M{"$in": channelCodes}
|
||||
}
|
||||
if appId != nil {
|
||||
filter["appId"] = appId
|
||||
}
|
||||
if start != nil && end != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
||||
} else if start != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start}
|
||||
} else if end != nil {
|
||||
filter["sumDate"] = bson.M{"$lt": end}
|
||||
}
|
||||
|
||||
sort := bson.D{{Key: "sumDate", Value: -1}, {Key: "newDirectCount", Value: -1}}
|
||||
|
||||
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 后台获取统计信息
|
||||
func GetBlocChannelCodeBlocStatList(ctx context.Context, channel *string, appId *int32, start, end *time.Time, skip, limit int64) (count int64, data ChannelCodeBlocStatList, err error) {
|
||||
filter := bson.M{}
|
||||
|
||||
if channel != nil {
|
||||
filter["channelCode"] = channel
|
||||
}
|
||||
|
||||
if appId != nil {
|
||||
filter["appId"] = appId
|
||||
}
|
||||
if start != nil && end != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
||||
} else if start != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start}
|
||||
} else if end != nil {
|
||||
filter["sumDate"] = bson.M{"$lt": end}
|
||||
}
|
||||
|
||||
sort := bson.D{{Key: "sumDate", Value: -1}, {Key: "newDirectCount", Value: -1}}
|
||||
|
||||
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 后台获取统计信息
|
||||
func GetBlocChannelCodeStatTotal(ctx context.Context, channels []string, appId *int32, start, end *time.Time) (count int64, data ChannelCodeBlocStatList, err error) {
|
||||
filter := bson.M{}
|
||||
if len(channels) > 0 {
|
||||
filter["channelCode"] = bson.M{"$in": channels}
|
||||
}
|
||||
|
||||
if appId != nil {
|
||||
filter["appId"] = appId
|
||||
}
|
||||
if start != nil && end != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start, "$lt": end}
|
||||
} else if start != nil {
|
||||
filter["sumDate"] = bson.M{"$gte": start}
|
||||
} else if end != nil {
|
||||
filter["sumDate"] = bson.M{"$lt": end}
|
||||
}
|
||||
|
||||
if err = db.Find(ctx, &data, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllChannelCodeBlocStatList 根据渠道码获取统计信息
|
||||
func GetAllChannelCodeBlocStatList(ctx context.Context, appId int32, channelCode []string, sumDate time.Time) (data ChannelCodeBlocStatList, err error) {
|
||||
filter := bson.M{"sumDate": sumDate, "appId": appId, "channelCode": bson.M{"$in": channelCode}}
|
||||
|
||||
if err = db.Find(ctx, &data, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetChannelCodeBlocStatListTool 根据渠道码获取统计信息
|
||||
func GetChannelCodeBlocStatListTool(ctx context.Context, appId int32, channelCode string) (data ChannelCodeBlocStatList, err error) {
|
||||
filter := bson.M{"appId": appId, "channelCode": channelCode}
|
||||
|
||||
if err = db.Find(ctx, &data, filter); err != nil {
|
||||
log.Error("ChannelCodeBlocStat GetChannelCodeBlocStatList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UpsertOneDistrictRechargeStat 更新渠道的新增充值
|
||||
func UpsertOneDistrictFissionRechargeStat(ctx context.Context, code string, appId int32, sumDate time.Time, rechargeCount, rechargeAmount int64) (err error) {
|
||||
filter := bson.M{"channelCode": code, "appId": appId, "sumDate": sumDate}
|
||||
|
||||
update := bson.M{"fissionAmount": rechargeAmount}
|
||||
|
||||
exists, err := db.Exists(ctx, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
_, err = db.UpdateOne(ctx, filter, bson.M{"$set": update})
|
||||
} else {
|
||||
insertDoc := update
|
||||
insertDoc["sumDate"] = sumDate
|
||||
insertDoc["appId"] = appId
|
||||
insertDoc["channelCode"] = code
|
||||
|
||||
_, err = db.InsertOne(ctx, insertDoc)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package channelCodeBlocStatMod
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
)
|
||||
|
||||
const CollChannelCodeBlocStat = "channel_code_stat_bloc"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
// ChannelCodeStat 渠道码日常统计
|
||||
type ChannelCodeBlocStat struct {
|
||||
SumDate time.Time `bson:"sumDate"` // 记录时间 按天对齐
|
||||
ChannelCode string `bson:"channelCode"` // 渠道码
|
||||
AppId int32 `bson:"appId"` // appId
|
||||
Channel string `bson:"channel"` // 该渠道码所属渠道渠道
|
||||
NewDirectCount int64 `bson:"newDirectCount,omitempty"` // 新增直推
|
||||
NewFissionCount int64 `bson:"newFissionCount,omitempty"` // 新增裂变
|
||||
NewDeductionCount int64 `bson:"newDeductionCount,omitempty"` // 新增扣量(新增 = 新增直推+新增裂变+扣量)
|
||||
DirectVisitCount int64 `bson:"directVisitCount,omitempty"` // 直推访问数
|
||||
FissionVisitCount int64 `bson:"fissionVisitCount,omitempty"` // 裂变访问数
|
||||
DeductionVisitCount int64 `bson:"deductionVisitCount,omitempty"` // 扣量访问数
|
||||
Day1RetainRate float64 `bson:"day1RetainRate,omitempty"` // 次日留存率
|
||||
Day3RetainRate float64 `bson:"day3RetainRate,omitempty"` // 3日留存率
|
||||
Day7RetainRate float64 `bson:"day7RetainRate,omitempty"` // 7日留存率
|
||||
DirectAmount int64 `bson:"directAmount,omitempty"` // 直推充值金额
|
||||
FissionAmount int64 `bson:"fissionAmount,omitempty"` // 裂变充值金额
|
||||
DeductionAmount int64 `bson:"deductionAmount,omitempty"` // 扣量金额(总金额=充值金额+扣量金额+裂变充值金额)
|
||||
NewRechargeCount int64 `bson:"newRechargeCount,omitempty"` // 新增充值数
|
||||
NewRechargeAmount int64 `bson:"newRechargeAmount,omitempty"` // 新增充值金额
|
||||
LandPageVisitCount int64 `bson:"landPageVisitCount,omitempty"` // 落地页访问次数
|
||||
UpdatedAt time.Time `bson:"updatedAt,omitempty"` // 更新时间
|
||||
}
|
||||
|
||||
type ChannelCodeBlocStatList []*ChannelCodeBlocStat
|
||||
|
||||
func (p ChannelCodeBlocStatList) Len() int { return len(p) }
|
||||
func (p ChannelCodeBlocStatList) Less(i, j int) bool { return p[j].SumDate.Before(p[i].SumDate) }
|
||||
func (p ChannelCodeBlocStatList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
Reference in New Issue
Block a user