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
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package downloadstat
|
||||
|
||||
func StatSummation(base, value *Stat) {
|
||||
base.VisitTotal += value.VisitTotal
|
||||
base.VisitUnique += value.VisitUnique
|
||||
base.DownTotal += value.DownTotal
|
||||
base.DownUnique += value.DownUnique
|
||||
base.InstallTotal += value.InstallTotal
|
||||
base.InstallUnique += value.InstallUnique
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package downloadstat
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// 表名
|
||||
const CollDownloadStat = "download_stat"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
type StatType int32
|
||||
|
||||
const (
|
||||
StatTypeNone StatType = 0
|
||||
StatTypeDaily StatType = 1
|
||||
StatTypeHour StatType = 2
|
||||
)
|
||||
|
||||
type Stat struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
AppId int32 `json:"appId" bson:"appId"`
|
||||
AppName string `json:"appName" bson:"appName,omitempty"`
|
||||
SumDate time.Time `json:"sumDate" bson:"sumDate"` // 日期
|
||||
EndDate time.Time `json:"endDate" bson:"endDate"` // 这个不展示
|
||||
Hour int `json:"hour" bson:"hour"` // 小时统计展示,每日统计不展示
|
||||
Type StatType `json:"type" bson:"type"` // 这个不展示
|
||||
|
||||
VisitTotal int64 `json:"visitCount" bson:"visitCount"` // 访问总数
|
||||
VisitUnique int64 `json:"visitUnique" bson:"visitUnique"` // 去重访问数
|
||||
|
||||
DownTotal int64 `json:"downTotal" bson:"downTotal"` // 下载总数
|
||||
DownUnique int64 `json:"downUnique" bson:"downUnique"` // 去重下载数(IP来自独立访问集合)
|
||||
|
||||
InstallTotal int64 `json:"installTotal" bson:"installTotal"` // 安装总数
|
||||
InstallUnique int64 `json:"installUnique" bson:"installUnique"` // 去重安装数(IP来自下载独立集合)
|
||||
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
}
|
||||
|
||||
type StatDoc struct {
|
||||
VisitTotal *int64 `json:"visitCount" bson:"visitCount"` // 访问总数
|
||||
VisitUnique *int64 `json:"visitIpCount" bson:"visitIpCount"` // 访问独立数
|
||||
|
||||
DownTotal *int64 `json:"downTotal" bson:"downTotal"` // 下载总数
|
||||
DownUnique *int64 `json:"downUnique" bson:"downUnique"` // 下载独立数(IP来自访问独立集合)
|
||||
|
||||
InstallTotal *int64 `json:"installTotal" bson:"installTotal"` // 安装总数
|
||||
InstallUnique *int64 `json:"installUnique" bson:"installUnique"` // 安装独立数(IP来自下载独立集合)
|
||||
|
||||
UpdatedAt *time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type StatList []*Stat
|
||||
Reference in New Issue
Block a user