From 9fc367a32390897b1aab470b303635f2c546ed5a Mon Sep 17 00:00:00 2001 From: Chang lue Tsen Date: Wed, 2 Jul 2025 13:18:34 -0400 Subject: [PATCH] fix(statistics): add mock data for revenue and user statistics in demo mode --- ...ld github.com_perfect-panel_server.run.xml | 3 + .../console/queryRevenueStatisticsLogic.go | 58 +++++++++++++- .../console/queryServerTotalDataLogic.go | 77 +++++++++++++++++++ .../admin/console/queryUserStatisticsLogic.go | 41 ++++++++++ 4 files changed, 178 insertions(+), 1 deletion(-) diff --git a/.run/go build github.com_perfect-panel_server.run.xml b/.run/go build github.com_perfect-panel_server.run.xml index 608afe7..454ba95 100644 --- a/.run/go build github.com_perfect-panel_server.run.xml +++ b/.run/go build github.com_perfect-panel_server.run.xml @@ -3,6 +3,9 @@ + + + diff --git a/internal/logic/admin/console/queryRevenueStatisticsLogic.go b/internal/logic/admin/console/queryRevenueStatisticsLogic.go index 5ef848f..9412b76 100644 --- a/internal/logic/admin/console/queryRevenueStatisticsLogic.go +++ b/internal/logic/admin/console/queryRevenueStatisticsLogic.go @@ -2,6 +2,8 @@ package console import ( "context" + "os" + "strings" "time" "github.com/perfect-panel/server/internal/svc" @@ -17,7 +19,7 @@ type QueryRevenueStatisticsLogic struct { svcCtx *svc.ServiceContext } -// Query revenue statistics +// NewQueryRevenueStatisticsLogic Query revenue statistics func NewQueryRevenueStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryRevenueStatisticsLogic { return &QueryRevenueStatisticsLogic{ Logger: logger.WithContext(ctx), @@ -27,6 +29,9 @@ func NewQueryRevenueStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceCont } func (l *QueryRevenueStatisticsLogic) QueryRevenueStatistics() (resp *types.RevenueStatisticsResponse, err error) { + if strings.ToLower(os.Getenv("PPANEL_MODE")) == "demo" { + return l.mockRevenueStatistics(), nil + } var today, monthly, all types.OrdersStatistics now := time.Now() @@ -75,3 +80,54 @@ func (l *QueryRevenueStatisticsLogic) QueryRevenueStatistics() (resp *types.Reve All: all, }, nil } + +// mockRevenueStatistics is a mock function to simulate revenue statistics data. +func (l *QueryRevenueStatisticsLogic) mockRevenueStatistics() *types.RevenueStatisticsResponse { + now := time.Now() + + // Generate daily data for the past 7 days (oldest first) + monthlyList := make([]types.OrdersStatistics, 7) + for i := 0; i < 7; i++ { + dayDate := now.AddDate(0, 0, -(6 - i)) + baseAmount := int64(25000 + ((6 - i) * 3000) + ((6-i)%3)*8000) + monthlyList[i] = types.OrdersStatistics{ + Date: dayDate.Format("2006-01-02"), + AmountTotal: baseAmount, + NewOrderAmount: int64(float64(baseAmount) * 0.68), + RenewalOrderAmount: int64(float64(baseAmount) * 0.32), + } + } + + // Generate monthly data for the past 6 months (oldest first) + allList := make([]types.OrdersStatistics, 6) + for i := 0; i < 6; i++ { + monthDate := now.AddDate(0, -(5 - i), 0) + baseAmount := int64(1800000 + ((5 - i) * 200000) + ((5-i)%2)*500000) + allList[i] = types.OrdersStatistics{ + Date: monthDate.Format("2006-01"), + AmountTotal: baseAmount, + NewOrderAmount: int64(float64(baseAmount) * 0.68), + RenewalOrderAmount: int64(float64(baseAmount) * 0.32), + } + } + + return &types.RevenueStatisticsResponse{ + Today: types.OrdersStatistics{ + AmountTotal: 35888, + NewOrderAmount: 22888, + RenewalOrderAmount: 13000, + }, + Monthly: types.OrdersStatistics{ + AmountTotal: 888888, + NewOrderAmount: 588888, + RenewalOrderAmount: 300000, + List: monthlyList, + }, + All: types.OrdersStatistics{ + AmountTotal: 12888888, + NewOrderAmount: 8588888, + RenewalOrderAmount: 4300000, + List: allList, + }, + } +} diff --git a/internal/logic/admin/console/queryServerTotalDataLogic.go b/internal/logic/admin/console/queryServerTotalDataLogic.go index 55793b8..ac7a5f8 100644 --- a/internal/logic/admin/console/queryServerTotalDataLogic.go +++ b/internal/logic/admin/console/queryServerTotalDataLogic.go @@ -2,6 +2,8 @@ package console import ( "context" + "os" + "strings" "time" "github.com/perfect-panel/server/pkg/xerr" @@ -28,6 +30,11 @@ func NewQueryServerTotalDataLogic(ctx context.Context, svcCtx *svc.ServiceContex } func (l *QueryServerTotalDataLogic) QueryServerTotalData() (resp *types.ServerTotalDataResponse, err error) { + + if strings.ToLower(os.Getenv("PPANEL_MODE")) == "demo" { + return l.mockRevenueStatistics(), nil + } + resp = &types.ServerTotalDataResponse{ ServerTrafficRankingToday: make([]types.ServerTrafficData, 0), ServerTrafficRankingYesterday: make([]types.ServerTrafficData, 0), @@ -152,3 +159,73 @@ func (l *QueryServerTotalDataLogic) QueryServerTotalData() (resp *types.ServerTo return resp, nil } + +func (l *QueryServerTotalDataLogic) mockRevenueStatistics() *types.ServerTotalDataResponse { + now := time.Now() + + // Generate server traffic ranking data for today (top 10) + serverTrafficToday := make([]types.ServerTrafficData, 10) + serverNames := []string{"香港-01", "美国-洛杉矶", "日本-东京", "新加坡-01", "韩国-首尔", "台湾-01", "德国-法兰克福", "英国-伦敦", "加拿大-多伦多", "澳洲-悉尼"} + for i := 0; i < 10; i++ { + upload := int64(500000000 + (i * 100000000) + (i%3)*200000000) // 500MB - 1.5GB + download := int64(2000000000 + (i * 300000000) + (i%4)*500000000) // 2GB - 8GB + serverTrafficToday[i] = types.ServerTrafficData{ + ServerId: int64(i + 1), + Name: serverNames[i], + Upload: upload, + Download: download, + } + } + + // Generate server traffic ranking data for yesterday (top 10) + serverTrafficYesterday := make([]types.ServerTrafficData, 10) + for i := 0; i < 10; i++ { + upload := int64(480000000 + (i * 95000000) + (i%3)*180000000) + download := int64(1900000000 + (i * 280000000) + (i%4)*450000000) + serverTrafficYesterday[i] = types.ServerTrafficData{ + ServerId: int64(i + 1), + Name: serverNames[i], + Upload: upload, + Download: download, + } + } + + //// Generate user traffic ranking data for today (top 10) + //userTrafficToday := make([]types.UserTrafficData, 10) + //for i := 0; i < 10; i++ { + // upload := int64(100000000 + (i*20000000) + (i%5)*50000000) // 100MB - 400MB + // download := int64(800000000 + (i*150000000) + (i%3)*300000000) // 800MB - 3GB + // userTrafficToday[i] = types.UserTrafficData{ + // SID: int64(10001 + i), + // Upload: upload, + // Download: download, + // } + //} + + //// Generate user traffic ranking data for yesterday (top 10) + //userTrafficYesterday := make([]types.UserTrafficData, 10) + //for i := 0; i < 10; i++ { + // upload := int64(95000000 + (i*18000000) + (i%5)*45000000) + // download := int64(750000000 + (i*140000000) + (i%3)*280000000) + // userTrafficYesterday[i] = types.UserTrafficData{ + // SID: int64(10001 + i), + // Upload: upload, + // Download: download, + // } + //} + // + return &types.ServerTotalDataResponse{ + OnlineUserIPs: 1688, + OnlineServers: 8, + OfflineServers: 2, + TodayUpload: 8888888888, // ~8.3GB + TodayDownload: 28888888888, // ~26.9GB + MonthlyUpload: 288888888888, // ~269GB + MonthlyDownload: 888888888888, // ~828GB + UpdatedAt: now.Unix(), + ServerTrafficRankingToday: serverTrafficToday, + ServerTrafficRankingYesterday: serverTrafficYesterday, + //UserTrafficRankingToday: userTrafficToday, + //UserTrafficRankingYesterday: userTrafficYesterday, + } +} diff --git a/internal/logic/admin/console/queryUserStatisticsLogic.go b/internal/logic/admin/console/queryUserStatisticsLogic.go index 54480a2..03d2a83 100644 --- a/internal/logic/admin/console/queryUserStatisticsLogic.go +++ b/internal/logic/admin/console/queryUserStatisticsLogic.go @@ -2,6 +2,8 @@ package console import ( "context" + "os" + "strings" "time" "github.com/perfect-panel/server/internal/svc" @@ -25,6 +27,9 @@ func NewQueryUserStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext } func (l *QueryUserStatisticsLogic) QueryUserStatistics() (resp *types.UserStatisticsResponse, err error) { + if strings.ToLower(os.Getenv("PPANEL_MODE")) == "demo" { + return l.mockRevenueStatistics(), nil + } resp = &types.UserStatisticsResponse{} now := time.Now() // query today user register count @@ -69,3 +74,39 @@ func (l *QueryUserStatisticsLogic) QueryUserStatistics() (resp *types.UserStatis } return } + +func (l *QueryUserStatisticsLogic) mockRevenueStatistics() *types.UserStatisticsResponse { + now := time.Now() + + // Generate daily user statistics for the past 7 days (oldest first) + monthlyList := make([]types.UserStatistics, 7) + for i := 0; i < 7; i++ { + dayDate := now.AddDate(0, 0, -(6 - i)) + baseRegister := int64(18 + ((6 - i) * 3) + ((6-i)%3)*8) + monthlyList[i] = types.UserStatistics{ + Date: dayDate.Format("2006-01-02"), + Register: baseRegister, + NewOrderUsers: int64(float64(baseRegister) * 0.65), + RenewalOrderUsers: int64(float64(baseRegister) * 0.35), + } + } + + return &types.UserStatisticsResponse{ + Today: types.UserStatistics{ + Register: 28, + NewOrderUsers: 18, + RenewalOrderUsers: 10, + }, + Monthly: types.UserStatistics{ + Register: 888, + NewOrderUsers: 588, + RenewalOrderUsers: 300, + List: monthlyList, + }, + All: types.UserStatistics{ + Register: 18888, + NewOrderUsers: 0, // This field is not used in All statistics + RenewalOrderUsers: 0, // This field is not used in All statistics + }, + } +}