forked from all_user/stat_center
192 lines
4.8 KiB
Go
192 lines
4.8 KiB
Go
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
|
|
}
|