boweniac hai 3 meses
pai
achega
a4c653bd24

+ 1 - 17
desc/wechat/dashboard.api

@@ -24,6 +24,7 @@ type (
         ConsumeToken *ChartsUint `json:"consume_token"`
         ActiveUser *ChartsUint `json:"active_user"`
         NewUser *ChartsInt `json:"new_user"`
+        LabelDist []LabelsData `json:"label_dist"`
     }
 
     ChartsUint {
@@ -40,19 +41,6 @@ type (
         Val []int64 `json:"val"`
     }
 
-    LabelsReq {
-        EndDate *string `json:"end_date"`
-
-        // 租户id
-        OrganizationId  *uint64 `json:"organizationId,optional"`
-    }
-
-    LabelsResp {
-        BaseDataInfo
-
-        Data []LabelsData `json:"data"`
-    }
-
     LabelsData {
         Value uint64 `json:"value"`
         Name string `json:"name"`
@@ -97,10 +85,6 @@ service Wechat {
     @handler getCharts
     post /dashboard/charts (ChartsReq) returns (ChartsResp)
 
-    // get labels | 获取图表数据
-    @handler getLabels
-    post /dashboard/labels (LabelsReq) returns (LabelsResp)
-
     // get wxs | 获取图表数据
     @handler getWxs
     post /dashboard/wx (WxReq) returns (WxResp)

+ 5 - 0
ent/custom_types/types.go

@@ -19,3 +19,8 @@ type ActionForward struct {
 	Wxid   string   `json:"wxid"`
 	Action []Action `json:"action"`
 }
+
+type LabelDist struct {
+	Value uint64 `json:"value"`
+	Name  string `json:"name"`
+}

+ 4 - 0
ent/schema/usage_statistic_hour.go

@@ -1,6 +1,7 @@
 package schema
 
 import (
+	"wechat-api/ent/custom_types"
 	"wechat-api/ent/schema/localmixin"
 
 	"entgo.io/ent"
@@ -29,6 +30,9 @@ func (UsageStatisticHour) Fields() []ent.Field {
 		field.Uint64("consume_token").Comment("消耗token数"),
 		field.Uint64("active_user").Comment("活跃用户数"),
 		field.Int64("new_user").Comment("新增用户数"),
+		field.JSON("label_dist", []custom_types.LabelDist{}).
+			Annotations(entsql.WithComments(true)).
+			Comment("标签分布"),
 	}
 }
 

+ 0 - 44
internal/handler/dashboard/get_labels_handler.go

@@ -1,44 +0,0 @@
-package dashboard
-
-import (
-	"net/http"
-
-	"github.com/zeromicro/go-zero/rest/httpx"
-
-	"wechat-api/internal/logic/dashboard"
-	"wechat-api/internal/svc"
-	"wechat-api/internal/types"
-)
-
-// swagger:route post /dashboard/labels dashboard GetLabels
-//
-// get labels | 获取图表数据
-//
-// get labels | 获取图表数据
-//
-// Parameters:
-//  + name: body
-//    require: true
-//    in: body
-//    type: LabelsReq
-//
-// Responses:
-//  200: LabelsResp
-
-func GetLabelsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
-	return func(w http.ResponseWriter, r *http.Request) {
-		var req types.LabelsReq
-		if err := httpx.Parse(r, &req, true); err != nil {
-			httpx.ErrorCtx(r.Context(), w, err)
-			return
-		}
-
-		l := dashboard.NewGetLabelsLogic(r.Context(), svcCtx)
-		resp, err := l.GetLabels(&req)
-		if err != nil {
-			httpx.ErrorCtx(r.Context(), w, err)
-		} else {
-			httpx.OkJsonCtx(r.Context(), w, resp)
-		}
-	}
-}

+ 0 - 5
internal/handler/routes.go

@@ -1500,11 +1500,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				},
 				{
 					Method:  http.MethodPost,
-					Path:    "/dashboard/labels",
-					Handler: dashboard.GetLabelsHandler(serverCtx),
-				},
-				{
-					Method:  http.MethodPost,
 					Path:    "/dashboard/wx",
 					Handler: dashboard.GetWxsHandler(serverCtx),
 				},

+ 93 - 100
internal/logic/dashboard/get_charts_logic.go

@@ -64,7 +64,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 			break
 		}
 	}
-	l.Infof("----------------layoutsType--------------: %d", layoutsType)
+
 	if err != nil {
 		fmt.Println("解析开始时间失败:", err)
 		return
@@ -93,14 +93,38 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 	}
 
 	// 定义变量
-	var aiResponse types.ChartsUint
-	var sopRun types.ChartsUint
-	var totalFriend types.ChartsUint
-	var totalGroup types.ChartsUint
-	var accountBalance types.ChartsUint
-	var consumeToken types.ChartsUint
-	var activeUser types.ChartsUint
-	var newUser types.ChartsInt
+	aiResponse := types.ChartsUint{
+		Count: 0,
+		Val:   []uint64{}, // 初始化切片
+	}
+	sopRun := types.ChartsUint{
+		Count: 0,
+		Val:   []uint64{}, // 初始化切片
+	}
+	totalFriend := types.ChartsUint{
+		Count: 0,
+		Val:   []uint64{}, // 初始化切片
+	}
+	totalGroup := types.ChartsUint{
+		Count: 0,
+		Val:   []uint64{}, // 初始化切片
+	}
+	accountBalance := types.ChartsUint{
+		Count: 0,
+		Val:   []uint64{}, // 初始化切片
+	}
+	consumeToken := types.ChartsUint{
+		Count: 0,
+		Val:   []uint64{}, // 初始化切片
+	}
+	activeUser := types.ChartsUint{
+		Count: 0,
+		Val:   []uint64{}, // 初始化切片
+	}
+	newUser := types.ChartsInt{
+		Count: 0,
+		Val:   []int64{}, // 初始化切片
+	}
 
 	var aiResponseSum []SumUint
 	var sopRunSum []SumUint
@@ -131,32 +155,24 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		_ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldActiveUser), "value")).Scan(l.ctx, &activeUserSum)
 		_ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldNewUser), "value")).Scan(l.ctx, &newUserSum)
 		for _, ar := range aiResponseSum {
-			aiResponse = types.ChartsUint{
-				Count: aiResponse.Count + ar.Value,
-				Val:   append(aiResponse.Val, ar.Value),
-			}
+			aiResponse.Count = aiResponse.Count + ar.Value
+			aiResponse.Val = append(aiResponse.Val, ar.Value)
 		}
 		for _, sr := range sopRunSum {
-			sopRun = types.ChartsUint{
-				Count: sopRun.Count + sr.Value,
-				Val:   append(sopRun.Val, sr.Value),
-			}
+			sopRun.Count = sopRun.Count + sr.Value
+			sopRun.Val = append(sopRun.Val, sr.Value)
 		}
 		for _, tf := range totalFriendSum {
-			totalFriend = types.ChartsUint{
-				Count: tf.Value,
-				Val:   append(totalFriend.Val, tf.Value),
-			}
+			totalFriend.Count = tf.Value
+			totalFriend.Val = append(totalFriend.Val, tf.Value)
 		}
 		totalFriendLen := len(totalFriendSum)
 		if totalFriendLen > 0 && totalFriendSum[0].Value > 0 {
 			totalFriend.Rate = float32((totalFriendSum[totalFriendLen-1].Value - totalFriendSum[0].Value) / totalFriendSum[0].Value)
 		}
 		for _, tg := range totalGroupSum {
-			totalGroup = types.ChartsUint{
-				Count: tg.Value,
-				Val:   append(totalGroup.Val, tg.Value),
-			}
+			totalGroup.Count = tg.Value
+			totalGroup.Val = append(totalGroup.Val, tg.Value)
 		}
 		totalGroupLen := len(totalGroupSum)
 		if totalGroupLen > 0 && totalGroupSum[0].Value > 0 {
@@ -164,17 +180,13 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		}
 		for _, ct := range consumeTokenSum {
 			addtimeLastTwoDigits := ct.Addtime % 100
-			consumeToken = types.ChartsUint{
-				Count: consumeToken.Count + ct.Value,
-				Val:   append(consumeToken.Val, ct.Value),
-				Label: append(consumeToken.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)),
-			}
+			consumeToken.Count = consumeToken.Count + ct.Value
+			consumeToken.Val = append(consumeToken.Val, ct.Value)
+			consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits))
 		}
 		for _, au := range activeUserSum {
-			activeUser = types.ChartsUint{
-				Count: au.Value,
-				Val:   append(activeUser.Val, au.Value),
-			}
+			activeUser.Count = au.Value
+			activeUser.Val = append(activeUser.Val, au.Value)
 		}
 		activeUserLen := len(activeUserSum)
 		if activeUserLen > 0 && activeUserSum[0].Value > 0 {
@@ -182,11 +194,9 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		}
 		for _, nu := range newUserSum {
 			addtimeLastTwoDigits := nu.Addtime % 100
-			newUser = types.ChartsInt{
-				Count: nu.Value,
-				Val:   append(newUser.Val, nu.Value),
-				Label: append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)),
-			}
+			newUser.Count = nu.Value
+			newUser.Val = append(newUser.Val, nu.Value)
+			newUser.Label = append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits))
 		}
 		newUserLen := len(newUserSum)
 		if newUserLen > 0 && newUserSum[0].Value > 0 {
@@ -213,6 +223,11 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				usagestatisticday.AddtimeLTE(endAddTime),
 			).
 			GroupBy(usagestatisticday.FieldAddtime)
+		//count, _ := usageStatisticDay..Count(l.ctx)
+		//l.Infof("----------------count--------------: %d", count)
+		var v []SumUint
+		_ = usageStatisticDay.Aggregate(ent.As(ent.Count(), "value")).Scan(l.ctx, &v)
+		l.Infof("----------------count--------------: %d", v)
 		_ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldAiResponse), "value")).Scan(l.ctx, &aiResponseSum)
 		_ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldSopRun), "value")).Scan(l.ctx, &sopRunSum)
 		_ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldTotalFriend), "value")).Scan(l.ctx, &totalFriendSum)
@@ -222,23 +237,17 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		_ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldNewUser), "value")).Scan(l.ctx, &newUserSum)
 
 		for _, ar := range aiResponseSum {
-			aiResponse = types.ChartsUint{
-				Count: aiResponse.Count + ar.Value,
-				Val:   append(aiResponse.Val, ar.Value),
-			}
+			aiResponse.Count = aiResponse.Count + ar.Value
+			aiResponse.Val = append(aiResponse.Val, ar.Value)
 		}
 		for _, sr := range sopRunSum {
-			sopRun = types.ChartsUint{
-				Count: sopRun.Count + sr.Value,
-				Val:   append(sopRun.Val, sr.Value),
-			}
+			sopRun.Count = sopRun.Count + sr.Value
+			sopRun.Val = append(sopRun.Val, sr.Value)
 		}
 
 		for _, tf := range totalFriendSum {
-			totalFriend = types.ChartsUint{
-				Count: tf.Value,
-				Val:   append(totalFriend.Val, tf.Value),
-			}
+			totalFriend.Count = tf.Value
+			totalFriend.Val = append(totalFriend.Val, tf.Value)
 		}
 		totalFriendLen := len(totalFriendSum)
 		if totalFriendLen > 0 && totalFriendSum[0].Value > 0 {
@@ -246,10 +255,8 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		}
 
 		for _, tg := range totalGroupSum {
-			totalGroup = types.ChartsUint{
-				Count: tg.Value,
-				Val:   append(totalGroup.Val, tg.Value),
-			}
+			totalGroup.Count = tg.Value
+			totalGroup.Val = append(totalGroup.Val, tg.Value)
 		}
 		totalGroupLen := len(totalGroupSum)
 		if totalGroupLen > 0 && totalGroupSum[0].Value > 0 {
@@ -257,18 +264,14 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		}
 
 		for _, ct := range consumeTokenSum {
-			addtimeLastTwoDigits := ct.Addtime % 100
-			consumeToken = types.ChartsUint{
-				Count: consumeToken.Count + ct.Value,
-				Val:   append(consumeToken.Val, ct.Value),
-				Label: append(consumeToken.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)),
-			}
+			//addtimeLastTwoDigits := ct.Addtime % 100
+			consumeToken.Count = consumeToken.Count + ct.Value
+			consumeToken.Val = append(consumeToken.Val, ct.Value)
+			consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%d", ct.Addtime))
 		}
 		for _, au := range activeUserSum {
-			activeUser = types.ChartsUint{
-				Count: au.Value,
-				Val:   append(activeUser.Val, au.Value),
-			}
+			activeUser.Count = au.Value
+			activeUser.Val = append(activeUser.Val, au.Value)
 		}
 		activeUserLen := len(activeUserSum)
 		if activeUserLen > 0 && activeUserSum[0].Value > 0 {
@@ -276,11 +279,9 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		}
 		for _, nu := range newUserSum {
 			addtimeLastTwoDigits := nu.Addtime % 100
-			newUser = types.ChartsInt{
-				Count: nu.Value,
-				Val:   append(newUser.Val, nu.Value),
-				Label: append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)),
-			}
+			newUser.Count = nu.Value
+			newUser.Val = append(newUser.Val, nu.Value)
+			newUser.Label = append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits))
 		}
 		newUserLen := len(newUserSum)
 		if newUserLen > 0 && newUserSum[0].Value > 0 {
@@ -323,48 +324,40 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				activeUserOfDay = usageStatisticHour[hourLen-1].ActiveUser
 				newUserOfDay = usageStatisticHour[hourLen-1].NewUser
 
-				aiResponse = types.ChartsUint{
-					Count: aiResponse.Count + aiResponseOfDay,
-					Val:   append(aiResponse.Val, aiResponseOfDay),
-				}
-				sopRun = types.ChartsUint{
-					Count: sopRun.Count + sopRunOfDay,
-					Val:   append(sopRun.Val, sopRunOfDay),
-				}
-				totalFriend = types.ChartsUint{
-					Count: totalFriendOfDay,
-					Val:   append(totalFriend.Val, totalFriendOfDay),
-				}
+				aiResponse.Count = aiResponse.Count + aiResponseOfDay
+				aiResponse.Val = append(aiResponse.Val, aiResponseOfDay)
+
+				sopRun.Count = sopRun.Count + sopRunOfDay
+				sopRun.Val = append(sopRun.Val, sopRunOfDay)
+
+				totalFriend.Count = totalFriendOfDay
+				totalFriend.Val = append(totalFriend.Val, totalFriendOfDay)
 				tfLen := len(totalFriend.Val)
 				if tfLen > 0 && totalFriend.Val[0] > 0 {
 					totalFriend.Rate = float32((totalFriend.Val[tfLen-1] - totalFriend.Val[0]) / totalFriend.Val[0])
 				}
-				totalGroup = types.ChartsUint{
-					Count: totalGroupOfDay,
-					Val:   append(totalGroup.Val, totalGroupOfDay),
-				}
+
+				totalGroup.Count = totalGroupOfDay
+				totalGroup.Val = append(totalGroup.Val, totalGroupOfDay)
 				tgLen := len(totalGroup.Val)
 				if tgLen > 0 && totalGroup.Val[0] > 0 {
 					totalGroup.Rate = float32((totalGroup.Val[tgLen-1] - totalGroup.Val[0]) / totalGroup.Val[0])
 				}
-				consumeToken = types.ChartsUint{
-					Count: consumeToken.Count + consumeTokenOfDay,
-					Val:   append(consumeToken.Val, consumeTokenOfDay),
-					Label: append(consumeToken.Label, "今日"),
-				}
-				activeUser = types.ChartsUint{
-					Count: activeUserOfDay,
-					Val:   append(activeUser.Val, activeUserOfDay),
-				}
+
+				consumeToken.Count = consumeToken.Count + consumeTokenOfDay
+				consumeToken.Val = append(consumeToken.Val, consumeTokenOfDay)
+				consumeToken.Label = append(consumeToken.Label, "今日")
+
+				activeUser.Count = activeUserOfDay
+				activeUser.Val = append(activeUser.Val, activeUserOfDay)
 				auLen := len(activeUser.Val)
 				if auLen > 0 && activeUser.Val[0] > 0 {
 					activeUser.Rate = float32((activeUser.Val[auLen-1] - activeUser.Val[0]) / activeUser.Val[0])
 				}
-				newUser = types.ChartsInt{
-					Count: newUserOfDay,
-					Val:   append(newUser.Val, newUserOfDay),
-					Label: append(newUser.Label, "今日"),
-				}
+
+				newUser.Count = newUserOfDay
+				newUser.Val = append(newUser.Val, newUserOfDay)
+				newUser.Label = append(newUser.Label, "今日")
 				nuLen := len(newUser.Val)
 				if nuLen > 0 && newUser.Val[0] > 0 {
 					newUser.Rate = float32((newUser.Val[nuLen-1] - newUser.Val[0]) / newUser.Val[0])

+ 0 - 47
internal/logic/dashboard/get_labels_logic.go

@@ -1,47 +0,0 @@
-package dashboard
-
-import (
-	"context"
-	"wechat-api/ent/labelrelationship"
-	"wechat-api/ent/predicate"
-	"wechat-api/internal/utils/dberrorhandler"
-
-	"wechat-api/internal/svc"
-	"wechat-api/internal/types"
-
-	"github.com/zeromicro/go-zero/core/logx"
-)
-
-type GetLabelsLogic struct {
-	logx.Logger
-	ctx    context.Context
-	svcCtx *svc.ServiceContext
-}
-
-func NewGetLabelsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLabelsLogic {
-	return &GetLabelsLogic{
-		Logger: logx.WithContext(ctx),
-		ctx:    ctx,
-		svcCtx: svcCtx}
-}
-
-func (l *GetLabelsLogic) GetLabels(req *types.LabelsReq) (resp *types.LabelsResp, err error) {
-	// 获取组织id
-	var organizationId uint64 = 0
-	isAdmin := l.ctx.Value("isAdmin").(bool)
-	if isAdmin && req.OrganizationId != nil && *req.OrganizationId != 0 {
-		organizationId = *req.OrganizationId
-	} else {
-		organizationId = l.ctx.Value("organizationId").(uint64)
-	}
-
-	var predicates []predicate.LabelRelationship
-	predicates = append(predicates, labelrelationship.OrganizationIDEQ(organizationId))
-	data, err := l.svcCtx.DB.LabelRelationship.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
-
-	if err != nil {
-		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-	}
-
-	return
-}

+ 9 - 21
internal/types/types.go

@@ -2920,14 +2920,15 @@ type ChartsResp struct {
 }
 
 type ChartsData struct {
-	AiResponse     *ChartsUint `json:"ai_response"`
-	SopRun         *ChartsUint `json:"sop_run"`
-	TotalFriend    *ChartsUint `json:"total_friend"`
-	TotalGroup     *ChartsUint `json:"total_group"`
-	AccountBalance *ChartsUint `json:"account_balance"`
-	ConsumeToken   *ChartsUint `json:"consume_token"`
-	ActiveUser     *ChartsUint `json:"active_user"`
-	NewUser        *ChartsInt  `json:"new_user"`
+	AiResponse     *ChartsUint  `json:"ai_response"`
+	SopRun         *ChartsUint  `json:"sop_run"`
+	TotalFriend    *ChartsUint  `json:"total_friend"`
+	TotalGroup     *ChartsUint  `json:"total_group"`
+	AccountBalance *ChartsUint  `json:"account_balance"`
+	ConsumeToken   *ChartsUint  `json:"consume_token"`
+	ActiveUser     *ChartsUint  `json:"active_user"`
+	NewUser        *ChartsInt   `json:"new_user"`
+	LabelDist      []LabelsData `json:"label_dist"`
 }
 
 type ChartsUint struct {
@@ -2944,19 +2945,6 @@ type ChartsInt struct {
 	Val   []int64  `json:"val"`
 }
 
-// swagger:model LabelsReq
-type LabelsReq struct {
-	EndDate *string `json:"end_date"`
-	// 租户id
-	OrganizationId *uint64 `json:"organizationId,optional"`
-}
-
-// swagger:model LabelsResp
-type LabelsResp struct {
-	BaseDataInfo
-	Data []LabelsData `json:"data"`
-}
-
 type LabelsData struct {
 	Value uint64 `json:"value"`
 	Name  string `json:"name"`