mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-06 18:10:25 +08:00
实现用户dashboard统计
This commit is contained in:
@@ -59,10 +59,61 @@ func (this *ServerDailyStatDAO) SaveStats(stats []*pb.ServerDailyStat) error {
|
|||||||
// 根据用户计算某月合计
|
// 根据用户计算某月合计
|
||||||
// month 格式为YYYYMM
|
// month 格式为YYYYMM
|
||||||
func (this *ServerDailyStatDAO) SumUserMonthly(userId int64, regionId int64, month string) (int64, error) {
|
func (this *ServerDailyStatDAO) SumUserMonthly(userId int64, regionId int64, month string) (int64, error) {
|
||||||
return this.Query().
|
query := this.Query()
|
||||||
Attr("regionId", regionId).
|
if regionId > 0 {
|
||||||
Between("day", month+"01", month+"32").
|
query.Attr("regionId", regionId)
|
||||||
|
}
|
||||||
|
return query.Between("day", month+"01", month+"32").
|
||||||
Where("serverId IN (SELECT id FROM "+SharedServerDAO.Table+" WHERE userId=:userId)").
|
Where("serverId IN (SELECT id FROM "+SharedServerDAO.Table+" WHERE userId=:userId)").
|
||||||
Param("userId", userId).
|
Param("userId", userId).
|
||||||
SumInt64("bytes", 0)
|
SumInt64("bytes", 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取某月带宽峰值
|
||||||
|
// month 格式为YYYYMM
|
||||||
|
func (this *ServerDailyStatDAO) SumUserMonthlyPeek(userId int64, regionId int64, month string) (int64, error) {
|
||||||
|
query := this.Query()
|
||||||
|
if regionId > 0 {
|
||||||
|
query.Attr("regionId", regionId)
|
||||||
|
}
|
||||||
|
max, err := query.Between("day", month+"01", month+"32").
|
||||||
|
Where("serverId IN (SELECT id FROM "+SharedServerDAO.Table+" WHERE userId=:userId)").
|
||||||
|
Param("userId", userId).
|
||||||
|
Max("bytes", 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int64(max), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取某天流量总和
|
||||||
|
// day 格式为YYYYMMDD
|
||||||
|
func (this *ServerDailyStatDAO) SumUserDaily(userId int64, regionId int64, day string) (int64, error) {
|
||||||
|
query := this.Query()
|
||||||
|
if regionId > 0 {
|
||||||
|
query.Attr("regionId", regionId)
|
||||||
|
}
|
||||||
|
return query.
|
||||||
|
Attr("day", day).
|
||||||
|
Where("serverId IN (SELECT id FROM "+SharedServerDAO.Table+" WHERE userId=:userId)").
|
||||||
|
Param("userId", userId).
|
||||||
|
SumInt64("bytes", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取某天带宽峰值
|
||||||
|
// day 格式为YYYYMMDD
|
||||||
|
func (this *ServerDailyStatDAO) SumUserDailyPeek(userId int64, regionId int64, day string) (int64, error) {
|
||||||
|
query := this.Query()
|
||||||
|
if regionId > 0 {
|
||||||
|
query.Attr("regionId", regionId)
|
||||||
|
}
|
||||||
|
max, err := query.
|
||||||
|
Attr("day", day).
|
||||||
|
Where("serverId IN (SELECT id FROM "+SharedServerDAO.Table+" WHERE userId=:userId)").
|
||||||
|
Param("userId", userId).
|
||||||
|
Max("bytes", 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int64(max), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -464,7 +464,7 @@ func (this *ServerDAO) UpdateServerReverseProxy(serverId int64, config []byte) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 计算所有可用服务数量
|
// 计算所有可用服务数量
|
||||||
func (this *ServerDAO) CountAllEnabledServersMatch(groupId int64, keyword string) (int64, error) {
|
func (this *ServerDAO) CountAllEnabledServersMatch(groupId int64, keyword string, userId int64) (int64, error) {
|
||||||
query := this.Query().
|
query := this.Query().
|
||||||
State(ServerStateEnabled)
|
State(ServerStateEnabled)
|
||||||
if groupId > 0 {
|
if groupId > 0 {
|
||||||
@@ -475,6 +475,9 @@ func (this *ServerDAO) CountAllEnabledServersMatch(groupId int64, keyword string
|
|||||||
query.Where("(name LIKE :keyword OR serverNames LIKE :keyword)").
|
query.Where("(name LIKE :keyword OR serverNames LIKE :keyword)").
|
||||||
Param("keyword", "%"+keyword+"%")
|
Param("keyword", "%"+keyword+"%")
|
||||||
}
|
}
|
||||||
|
if userId > 0{
|
||||||
|
query.Attr("userId", userId)
|
||||||
|
}
|
||||||
return query.Count()
|
return query.Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -381,7 +381,7 @@ func (this *ServerService) CountAllEnabledServersMatch(ctx context.Context, req
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
count, err := models.SharedServerDAO.CountAllEnabledServersMatch(req.GroupId, req.Keyword)
|
count, err := models.SharedServerDAO.CountAllEnabledServersMatch(req.GroupId, req.Keyword, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 用户相关服务
|
// 用户相关服务
|
||||||
@@ -215,3 +217,78 @@ func (this *UserService) UpdateUserLogin(ctx context.Context, req *pb.UpdateUser
|
|||||||
}
|
}
|
||||||
return this.Success()
|
return this.Success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 取得用户Dashboard数据
|
||||||
|
func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.ComposeUserDashboardRequest) (*pb.ComposeUserDashboardResponse, error) {
|
||||||
|
userId, err := this.ValidateUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if userId != req.UserId {
|
||||||
|
return nil, this.PermissionError()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网站数量
|
||||||
|
countServers, err := models.SharedServerDAO.CountAllEnabledServersMatch(0, "", req.UserId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 本月总流量
|
||||||
|
month := timeutil.Format("Ym")
|
||||||
|
monthlyTrafficBytes, err := models.SharedServerDailyStatDAO.SumUserMonthly(req.UserId, 0, month)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 本月带宽峰值
|
||||||
|
monthlyPeekTrafficBytes, err := models.SharedServerDailyStatDAO.SumUserMonthly(req.UserId, 0, month)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 今日总流量
|
||||||
|
day := timeutil.Format("Ymd")
|
||||||
|
dailyTrafficBytes, err := models.SharedServerDailyStatDAO.SumUserDaily(req.UserId, 0, day)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 今日带宽峰值
|
||||||
|
dailyPeekTrafficBytes, err := models.SharedServerDailyStatDAO.SumUserDailyPeek(req.UserId, 0, day)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 近 15 日流量带宽趋势
|
||||||
|
dailyTrafficStats := []*pb.ComposeUserDashboardResponse_DailyStat{}
|
||||||
|
dailyPeekTrafficStats := []*pb.ComposeUserDashboardResponse_DailyStat{}
|
||||||
|
|
||||||
|
for i := 14; i >= 0; i-- {
|
||||||
|
day := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -i))
|
||||||
|
|
||||||
|
dailyTrafficBytes, err := models.SharedServerDailyStatDAO.SumUserDaily(req.UserId, 0, day)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
dailyPeekTrafficBytes, err := models.SharedServerDailyStatDAO.SumUserDailyPeek(req.UserId, 0, day)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
dailyTrafficStats = append(dailyTrafficStats, &pb.ComposeUserDashboardResponse_DailyStat{Day: day, Count: dailyTrafficBytes})
|
||||||
|
dailyPeekTrafficStats = append(dailyPeekTrafficStats, &pb.ComposeUserDashboardResponse_DailyStat{Day: day, Count: dailyPeekTrafficBytes})
|
||||||
|
}
|
||||||
|
|
||||||
|
return &pb.ComposeUserDashboardResponse{
|
||||||
|
CountServers: countServers,
|
||||||
|
MonthlyTrafficBytes: monthlyTrafficBytes,
|
||||||
|
MonthlyPeekTrafficBytes: monthlyPeekTrafficBytes,
|
||||||
|
DailyTrafficBytes: dailyTrafficBytes,
|
||||||
|
DailyPeekTrafficBytes: dailyPeekTrafficBytes,
|
||||||
|
DailyTrafficStats: dailyTrafficStats,
|
||||||
|
DailyPeekTrafficStats: dailyPeekTrafficStats,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user