Initial commit
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
package srv_applicationstat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stat/common/stdLog"
|
||||
"stat/models/applicationmod"
|
||||
"stat/models/applicationstat"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// GetAppStatList 获取应用统计列表
|
||||
func GetAppStatList(ctx context.Context, param *GetApplicationStatReq) (*GetApplicationStatResp, *stdLog.SrvErr) {
|
||||
resp := &GetApplicationStatResp{}
|
||||
//先获取app列表
|
||||
appFilter := bson.M{}
|
||||
statFilter := bson.M{}
|
||||
if param.ParentAppId != nil {
|
||||
appFilter["parentAppId"] = bson.M{"$in": *param.ParentAppId}
|
||||
}
|
||||
if param.ApplicationId != nil {
|
||||
appFilter["_id"] = *param.ApplicationId
|
||||
}
|
||||
if param.ApplicationName != nil {
|
||||
appFilter["name"] = *param.ApplicationName
|
||||
}
|
||||
appList, sErr := applicationmod.FindManyApplication(ctx, appFilter)
|
||||
if sErr != nil {
|
||||
return nil, sErr
|
||||
}
|
||||
applicationId := make([]primitive.ObjectID, 0)
|
||||
for _, info := range appList {
|
||||
applicationId = append(applicationId, info.Id)
|
||||
}
|
||||
statFilter["applicationDbId"] = bson.M{"$in": applicationId}
|
||||
if param.Start != nil || param.End != nil {
|
||||
statFilter[applicationstat.FN.ApplicationStat.SumData] = bson.M{"$gte": *param.Start, "$lte": *param.End}
|
||||
}
|
||||
|
||||
opt := &options.FindOptions{}
|
||||
opt.SetSort(bson.M{applicationstat.FN.ApplicationStat.SumData: -1})
|
||||
opt.SetLimit(param.PageSize)
|
||||
opt.SetSkip((param.PageNum - 1) * param.PageSize)
|
||||
|
||||
totalStats, sErr := applicationstat.FindManyApplicationStat(ctx, statFilter)
|
||||
if sErr != nil {
|
||||
return nil, sErr
|
||||
}
|
||||
|
||||
pageStats, sErr := applicationstat.FindManyApplicationStat(ctx, statFilter, opt)
|
||||
if sErr != nil {
|
||||
return nil, sErr
|
||||
}
|
||||
|
||||
appDict := make(map[primitive.ObjectID]*applicationmod.Application)
|
||||
for _, app := range appList {
|
||||
appDict[app.Id] = app
|
||||
}
|
||||
|
||||
for _, stat := range totalStats {
|
||||
resp.Summary.TotalClick += stat.Total
|
||||
}
|
||||
|
||||
for _, stat := range pageStats {
|
||||
name := ""
|
||||
parentAppId := int64(0)
|
||||
app := appDict[stat.ApplicationDbId]
|
||||
if app != nil {
|
||||
name = app.Name
|
||||
parentAppId = app.ParentAppId
|
||||
}
|
||||
hours := make([]int64, 24)
|
||||
for hour := range hours {
|
||||
hours[hour] = stat.HourData[hour]
|
||||
}
|
||||
doc := &AppStatInfo{
|
||||
Id: stat.Id,
|
||||
ApplicationDbId: stat.ApplicationDbId,
|
||||
ParentAppId: parentAppId,
|
||||
AppName: name,
|
||||
SumDate: stat.SumDate,
|
||||
Total: stat.Total,
|
||||
HourData: hours,
|
||||
CreatedAt: stat.CreatedAt,
|
||||
UpdatedAt: stat.UpdatedAt,
|
||||
}
|
||||
resp.List = append(resp.List, doc)
|
||||
}
|
||||
|
||||
resp.Total = int64(len(totalStats))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetAppStatList 获取应用统计列表
|
||||
func GetAppTotalStatList(ctx context.Context, param *GetApplicationStatTotalReq) (*GetApplicationStatTotalResp, *stdLog.SrvErr) {
|
||||
resp := &GetApplicationStatTotalResp{
|
||||
List: make([]*AppStatTotalInfo, 0),
|
||||
}
|
||||
|
||||
//先获取app列表
|
||||
appFilter := bson.M{}
|
||||
if param.ParentAppId != nil {
|
||||
appFilter["parentAppId"] = bson.M{"$in": *param.ParentAppId}
|
||||
}
|
||||
if param.ApplicationId != nil {
|
||||
appFilter["_id"] = *param.ApplicationId
|
||||
}
|
||||
if param.ApplicationName != nil {
|
||||
appFilter["name"] = *param.ApplicationName
|
||||
}
|
||||
appList, sErr := applicationmod.FindManyApplication(ctx, appFilter)
|
||||
|
||||
if sErr != nil {
|
||||
return nil, sErr
|
||||
}
|
||||
|
||||
applicationId := make([]primitive.ObjectID, 0)
|
||||
appid := make(map[primitive.ObjectID]*applicationmod.Application)
|
||||
for _, info := range appList {
|
||||
applicationId = append(applicationId, info.Id)
|
||||
appid[info.Id] = info
|
||||
}
|
||||
|
||||
filter := bson.M{}
|
||||
filter["applicationDbId"] = bson.M{"$in": applicationId}
|
||||
|
||||
if param.Start != nil && param.End != nil {
|
||||
filter[applicationstat.FN.ApplicationStat.SumData] = bson.M{"$gte": *param.Start, "$lte": *param.End}
|
||||
}
|
||||
|
||||
pipeline := []bson.M{
|
||||
{
|
||||
"$match": filter,
|
||||
},
|
||||
{
|
||||
"$group": bson.M{
|
||||
"_id": "$applicationDbId",
|
||||
"total": bson.M{"$sum": "$total"},
|
||||
},
|
||||
},
|
||||
{"$sort": bson.D{{Key: "_id", Value: -1}}},
|
||||
}
|
||||
|
||||
list := make([]StatTotalInfo, 0)
|
||||
serr := applicationstat.AggregateApplicationStat(ctx, &list, pipeline)
|
||||
|
||||
pipeline = []bson.M{
|
||||
{
|
||||
"$match": filter,
|
||||
},
|
||||
{
|
||||
"$group": bson.M{
|
||||
"_id": "$applicationDbId",
|
||||
"total": bson.M{"$sum": "$total"},
|
||||
},
|
||||
},
|
||||
{"$sort": bson.D{{Key: "_id", Value: -1}}},
|
||||
{"$skip": (param.PageNum - 1) * param.PageSize},
|
||||
{"$limit": param.PageSize},
|
||||
}
|
||||
info := make([]StatTotalInfo, 0)
|
||||
serr = applicationstat.AggregateApplicationStat(ctx, &info, pipeline)
|
||||
if serr != nil {
|
||||
return nil, serr
|
||||
}
|
||||
|
||||
if len(list) > 0 {
|
||||
for _, app := range list {
|
||||
resp.Summary.TotalClick += app.Total
|
||||
}
|
||||
}
|
||||
|
||||
if len(info) > 0 {
|
||||
for _, app := range info {
|
||||
tmp := &AppStatTotalInfo{
|
||||
Id: app.Id,
|
||||
Total: app.Total,
|
||||
}
|
||||
if value, ok := appid[app.Id]; ok {
|
||||
tmp.ParentAppId = value.ParentAppId
|
||||
tmp.AppName = value.Name
|
||||
}
|
||||
resp.List = append(resp.List, tmp)
|
||||
}
|
||||
}
|
||||
resp.Total = int64(len(list))
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package srv_applicationstat
|
||||
|
||||
import (
|
||||
"stat/common"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type AppStatSummary struct {
|
||||
TotalClick int64 `json:"totalClick"` // 总点击
|
||||
}
|
||||
|
||||
type AppStatInfo struct {
|
||||
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
ApplicationDbId primitive.ObjectID `json:"applicationDbId" bson:"applicationDbId"` //应用ID
|
||||
ParentAppId int64 `json:"parentAppId" bson:"parentAppId"` //所属应用ID
|
||||
AppName string `json:"appName" bson:"appName"` //应用名称
|
||||
SumDate time.Time `json:"sumDate" bson:"sumDate"` //记录时间
|
||||
Total int64 `json:"total" bson:"total"` //当日点击
|
||||
HourData []int64 `json:"hourData" bson:"hourData"` //小时数据
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //更新时间
|
||||
}
|
||||
|
||||
// GetApplicationStatReq 获取app点击统计请求
|
||||
type GetApplicationStatReq struct {
|
||||
common.Page
|
||||
Start *time.Time `json:"start"` // 开始时间
|
||||
End *time.Time `json:"end"` // 结束时间
|
||||
ParentAppId *[]int64 `json:"parentAppId"` // 所属应用ID
|
||||
ApplicationId *primitive.ObjectID `json:"applicationId"` // 应用ID
|
||||
ApplicationName *string `json:"applicationName"` // 应用名称
|
||||
}
|
||||
|
||||
type GetApplicationStatResp struct {
|
||||
Total int64 `json:"total"`
|
||||
List []*AppStatInfo `json:"list"`
|
||||
Summary AppStatSummary `json:"summary"`
|
||||
}
|
||||
|
||||
// GetApplicationStatReq 获取app点击统计请求
|
||||
type GetApplicationStatTotalReq struct {
|
||||
common.Page
|
||||
Start *time.Time `json:"start"` // 开始时间
|
||||
End *time.Time `json:"end"` // 结束时间
|
||||
ParentAppId *[]int64 `json:"parentAppId"` // 所属应用ID
|
||||
ApplicationId *primitive.ObjectID `json:"applicationId"` // 应用ID
|
||||
ApplicationName *string `json:"applicationName"` // 应用名称
|
||||
}
|
||||
|
||||
type AppStatTotalInfo struct {
|
||||
Id primitive.ObjectID `json:"id" bson:"id"` //应用ID
|
||||
ParentAppId int64 `json:"parentAppId" bson:"parentAppId"` //所属应用ID
|
||||
AppName string `json:"appName" bson:"appName"` //应用名称
|
||||
Total int64 `json:"total" bson:"total"` //总次数
|
||||
}
|
||||
type StatTotalInfo struct {
|
||||
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` //应用ID
|
||||
Total int64 `json:"total"` //总次数
|
||||
}
|
||||
|
||||
type GetApplicationStatTotalResp struct {
|
||||
Total int64 `json:"total"`
|
||||
List []*AppStatTotalInfo `json:"list"`
|
||||
Summary AppStatSummary `json:"summary"`
|
||||
}
|
||||
Reference in New Issue
Block a user