Initial commit
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package applicationstat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"stat/common/stdLog"
|
||||
)
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
// 获取数据库表
|
||||
db = mongos.New(mongos.MgoManager, Collapplicationstat)
|
||||
// 获取数据库表
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: "applicationDbId", Value: 1},
|
||||
{Key: FN.ApplicationStat.SumData, Value: -1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.ApplicationStat.Id, Value: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(applicationstat:initApplicationStatIndex) Create ApplicationStat Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneApplicationStat 插入一个应用
|
||||
func InsertOneApplicationStat(ctx context.Context, applicationStat *ApplicationStat) *stdLog.SrvErr {
|
||||
if _, err := db.InsertOne(ctx, applicationStat); err != nil {
|
||||
log.Warn("applicationstat:InsertOneApplicationStat db.InsertOne failed", log.E(err))
|
||||
return stdLog.New(stdLog.InsertDataError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelOneApplicationStat 删除一个应用tag
|
||||
func DelOneApplicationStat(ctx context.Context, filter bson.M) (
|
||||
int64, *stdLog.SrvErr) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:DelOneApplicationStat db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, stdLog.New(stdLog.DelDataError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindOneApplicationStat 查找一个应用
|
||||
func FindOneApplicationStat(ctx context.Context, filter bson.M) (
|
||||
*ApplicationStat, *stdLog.SrvErr) {
|
||||
var applicationStat ApplicationStat
|
||||
if err := db.FindOneEx(
|
||||
ctx, &applicationStat, filter); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Info("applicationstat:FindOneApplicationStat db.FindOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, stdLog.New(stdLog.QueryDataError, "", "")
|
||||
}
|
||||
return nil, stdLog.New(stdLog.DataNotExist, "", "")
|
||||
}
|
||||
return &applicationStat, nil
|
||||
}
|
||||
|
||||
// CountApplicationStats 统计应用数量
|
||||
func CountApplicationStats(ctx context.Context, filter bson.M) (
|
||||
int64, *stdLog.SrvErr) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:CountApplicationStats db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, stdLog.New(stdLog.QueryDataError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneApplicationStat 更新一个应用
|
||||
func UpdateOneApplicationStat(ctx context.Context, filter bson.M, update bson.M) *stdLog.SrvErr {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:UpdateOneApplicationStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return stdLog.New(stdLog.UpdateDataError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
log.Warn("applicationstat:UpdateOneApplicationStat db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return stdLog.New(stdLog.DataNotExist, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateManyApplicationStat 更新一批应用
|
||||
func UpdateManyApplicationStat(ctx context.Context, filter bson.M, update bson.M) *stdLog.SrvErr {
|
||||
result, err := db.UpdateMMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("applicationstat:UpdateManyApplicationStat db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return stdLog.New(stdLog.UpdateDataError, "", "")
|
||||
}
|
||||
if result.MatchedCount <= 0 {
|
||||
log.Warn("applicationstat:UpdateManyApplicationStat db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
//return srvdef.New(srvdef.ErrApplicationNotExsit, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AggregateTopicInfos 聚合查找一批用户信息
|
||||
func AggregateApplicationStat(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) *stdLog.SrvErr {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("feedback:AggregateApplicationStat db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return stdLog.New(stdLog.QueryDataError, "", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyApplicationStat 查找一批应用
|
||||
func FindManyApplicationStat(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) (ApplicationStatList, *stdLog.SrvErr) {
|
||||
data := make(ApplicationStatList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("applicationstat:FindManyApplicationStat db.Find failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, stdLog.New(stdLog.QueryDataError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package applicationstat
|
||||
|
||||
type TagApplicationStat struct {
|
||||
Id string
|
||||
//AppId string
|
||||
SumData string
|
||||
Total string
|
||||
HourData string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
ApplicationStat TagApplicationStat
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
ApplicationStat string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.ApplicationStat.Id = "_id"
|
||||
//FN.ApplicationStat.AppId = "appId"
|
||||
FN.ApplicationStat.SumData = "sumDate"
|
||||
FN.ApplicationStat.Total = "total"
|
||||
FN.ApplicationStat.HourData = "hourData"
|
||||
FN.ApplicationStat.CreatedAt = "createdAt"
|
||||
FN.ApplicationStat.UpdatedAt = "updatedAt"
|
||||
TN.ApplicationStat = "applicationstat"
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package applicationstat
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const Collapplicationstat = "applicationstat"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
type THourStat map[int]int64
|
||||
|
||||
func (m THourStat) Total() int64 {
|
||||
var sum int64
|
||||
for _, v := range m {
|
||||
sum += v
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Total *int64 `json:"total" bson:"total,omitempty"` //今日点击
|
||||
HourData *THourStat `json:"hourData" bson:"hourData,omitempty"` //小时数据
|
||||
UpdatedAt *time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //更新时间
|
||||
}
|
||||
|
||||
// 应用统计
|
||||
// @table:applicationstat
|
||||
type ApplicationStat struct {
|
||||
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
ApplicationDbId primitive.ObjectID `json:"applicationDbId" bson:"applicationDbId"`
|
||||
//AppId int64 `json:"appId" bson:"appId"` //应用ID
|
||||
//ParentAppId int64 `json:"parentAppId" bson:"parentAppId"` //所属应用ID
|
||||
SumDate time.Time `json:"sumDate" bson:"sumDate"` //记录时间
|
||||
Total int64 `json:"total" bson:"total"` //今日点击
|
||||
HourData THourStat `json:"hourData" bson:"hourData"` //小时数据
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //更新时间
|
||||
}
|
||||
|
||||
type ApplicationStatList []*ApplicationStat
|
||||
Reference in New Issue
Block a user