forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
package accessmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stat/common/log"
|
||||
"stat/common/utils"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// 根据appId 获取appInfo
|
||||
func GetAccessUserByTime(ctx context.Context, appid int32, start, end time.Time) ([]uint64, error) {
|
||||
filter := bson.M{"appId": appid, "visitAt": bson.M{"$gte": start, "$lt": end}}
|
||||
results := make([]AccessMod, 0)
|
||||
if err := db.Find(ctx, &results, filter); err != nil {
|
||||
log.Error("GetAccessUserByTime Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
uids := make([]uint64, 0, len(results))
|
||||
for _, v := range results {
|
||||
uids = append(uids, v.UserId)
|
||||
}
|
||||
|
||||
return uids, nil
|
||||
}
|
||||
|
||||
func GetAccessUserDistrictCount(ctx context.Context, appid int32, sysTypes []string, start, end time.Time) (int64, error) {
|
||||
filter := bson.M{
|
||||
"appId": appid,
|
||||
"visitAt": bson.M{"$gte": start, "$lt": end},
|
||||
"isDirect": true,
|
||||
"$and": bson.A{
|
||||
bson.M{"districtCode": bson.M{"$gt": ""}},
|
||||
bson.M{"districtCode": bson.M{"$ne": "官方"}},
|
||||
bson.M{"districtCode": bson.M{"$ne": "modelweb"}},
|
||||
},
|
||||
}
|
||||
if len(sysTypes) > 0 {
|
||||
filter["sysType"] = bson.M{"$in": sysTypes}
|
||||
}
|
||||
if count, err := db.Count(ctx, filter); err != nil {
|
||||
log.Error("GetAccessUserDistrictCount Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return 0, err
|
||||
} else {
|
||||
return count, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 根据appId 获取appInfo
|
||||
func GetAndroidAccessUserByTime(ctx context.Context, appid int32, start, end time.Time) ([]uint64, error) {
|
||||
filter := bson.M{"appId": appid, "sysType": "android", "visitAt": bson.M{"$gte": start, "$lt": end}}
|
||||
results := make([]AccessMod, 0)
|
||||
if err := db.Find(ctx, &results, filter); err != nil {
|
||||
log.Error("GetAccessUserByTime FindOne err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
uids := make([]uint64, 0, len(results))
|
||||
for _, v := range results {
|
||||
uids = append(uids, v.UserId)
|
||||
}
|
||||
|
||||
return uids, nil
|
||||
}
|
||||
|
||||
// 根据appId 获取appInfo
|
||||
func GetIosAccessUserByTime(ctx context.Context, appid int32, start, end time.Time) ([]uint64, error) {
|
||||
filter := bson.M{"appId": appid, "sysType": "ios", "visitAt": bson.M{"$gte": start, "$lt": end}}
|
||||
results := make([]AccessMod, 0)
|
||||
if err := db.Find(ctx, &results, filter); err != nil {
|
||||
log.Error("GetAccessUserByTime FindOne err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
uids := make([]uint64, 0, len(results))
|
||||
for _, v := range results {
|
||||
uids = append(uids, v.UserId)
|
||||
}
|
||||
|
||||
return uids, nil
|
||||
}
|
||||
|
||||
// BlocDailyStatCount 集团日活
|
||||
func BlocDailyStatCount(ctx context.Context, appid int32, isDirect *bool, blocDeduction *bool, start, end time.Time) (int64, error) {
|
||||
filter := bson.M{"appId": appid, "visitAt": bson.M{"$gte": start, "$lt": end}}
|
||||
if isDirect != nil {
|
||||
filter["isDirect"] = *isDirect
|
||||
}
|
||||
if blocDeduction != nil {
|
||||
filter["blocDeduction"] = *blocDeduction
|
||||
}
|
||||
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Error("GetAccessUserByTime Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// BlocDailyVisitStatCount H5日活
|
||||
func BlocDailyVisitStatCount(ctx context.Context, appid int32, sysTypes []string, blocDeduction *bool, start, end time.Time) (int64, error) {
|
||||
filter := bson.M{
|
||||
"appId": appid,
|
||||
"sysType": bson.M{"$in": sysTypes},
|
||||
"visitAt": bson.M{"$gte": start, "$lt": end}}
|
||||
if blocDeduction != nil {
|
||||
filter["blocDeduction"] = *blocDeduction
|
||||
}
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Error("BlocDailyVisitStatCount Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// DailyUserCount 用户访问量
|
||||
func DailyUserVisitCountByRegister(ctx context.Context, appid int32, start, end, registerStart, registerEnd time.Time) (int64, error) {
|
||||
filter := bson.M{"appId": appid, "visitAt": bson.M{"$gte": start, "$lt": end}, "registerTime": bson.M{"$gte": registerStart, "$lt": registerEnd}}
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Error("DailyUserCount Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// VisitInfoByTime 访问信息
|
||||
func VisitInfoByTime(ctx context.Context, appid int32, start, end time.Time, blocDeduction *bool) ([]*AccessMod, error) {
|
||||
filter := bson.M{"appId": appid, "visitAt": bson.M{"$gte": start, "$lt": end}}
|
||||
|
||||
results := make([]*AccessMod, 0)
|
||||
|
||||
if err := db.Find(ctx, &results, filter); err != nil {
|
||||
log.Error("GetAccessUserByTime Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
type OnItem func(item *AccessMod)
|
||||
|
||||
// VisitInfoByTimeShard 访问信息
|
||||
func VisitInfoByTimeShard(ctx context.Context, appid int32, start, end time.Time, step time.Duration, onItem OnItem) error {
|
||||
segments := utils.CalcTimeSegments(start, end, step)
|
||||
for _, segment := range segments {
|
||||
results := make([]*AccessMod, 0)
|
||||
filter := bson.M{"appId": appid, "visitAt": bson.M{"$gte": segment.Begin, "$lt": segment.End}}
|
||||
if err := db.Find(ctx, &results, filter); err != nil {
|
||||
log.Error("GetAccessUserByTime Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return err
|
||||
}
|
||||
for _, item := range results {
|
||||
onItem(item)
|
||||
}
|
||||
results = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// VisitInfoByTime 访问信息
|
||||
func VisitInfoByTimeV1(ctx context.Context, appid int32, start, end time.Time, blocDeduction *bool) ([]AccessMod, error) {
|
||||
filter := bson.M{"appId": appid, "isDeduction": false, "visitAt": bson.M{"$gte": start, "$lt": end}}
|
||||
|
||||
results := make([]AccessMod, 0)
|
||||
|
||||
if err := db.Find(ctx, &results, filter); err != nil {
|
||||
log.Error("GetAccessUserByTime Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// DailyUserVisitCountByInfoShard 用户访问量
|
||||
func DailyUserVisitCountByInfoShard(ctx context.Context, appid int32, start, end time.Time, shard time.Duration, registerStart, registerEnd time.Time, onItem OnItem) error {
|
||||
segments := utils.CalcTimeSegments(start, end, shard)
|
||||
for _, segment := range segments {
|
||||
filter := bson.M{"appId": appid, "visitAt": bson.M{"$gte": segment.Begin, "$lt": segment.End}, "registerTime": bson.M{"$gte": registerStart, "$lt": registerEnd}}
|
||||
results := make([]*AccessMod, 0)
|
||||
|
||||
err := db.Find(ctx, &results, filter)
|
||||
if err != nil {
|
||||
log.Error("DailyUserVisitCountByInfo Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return err
|
||||
}
|
||||
for _, item := range results {
|
||||
onItem(item)
|
||||
}
|
||||
results = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DailyUserCount 用户访问量
|
||||
func DailyUserVisitCountByInfo(ctx context.Context, appid int32, start, end, registerStart, registerEnd time.Time) ([]AccessMod, error) {
|
||||
filter := bson.M{"appId": appid, "visitAt": bson.M{"$gte": start, "$lt": end}, "registerTime": bson.M{"$gte": registerStart, "$lt": registerEnd}}
|
||||
results := make([]AccessMod, 0)
|
||||
|
||||
err := db.Find(ctx, &results, filter)
|
||||
if err != nil {
|
||||
log.Error("DailyUserVisitCountByInfo Find err", log.Any("start", start), log.Any("end", end), log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// FindManyVisitInfo 查找一批渠道信息数据
|
||||
func FindManyVisitInfo(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (VisitInfoList, error) {
|
||||
data := make(VisitInfoList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("FindManyVisitInfo dbChannel.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user