mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-28 00:06:36 +08:00
优化操作系统和浏览器统计相关程序
This commit is contained in:
143
internal/rpc/services/service_formal_client_browser.go
Normal file
143
internal/rpc/services/service_formal_client_browser.go
Normal file
@@ -0,0 +1,143 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
// FormalClientBrowserService 浏览器信息库服务
|
||||
type FormalClientBrowserService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// CreateFormalClientBrowser 创建浏览器信息
|
||||
func (this *FormalClientBrowserService) CreateFormalClientBrowser(ctx context.Context, req *pb.CreateFormalClientBrowserRequest) (*pb.CreateFormalClientBrowserResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查dataId是否存在
|
||||
var tx = this.NullTx()
|
||||
browser, err := models.SharedFormalClientBrowserDAO.FindBrowserWithDataId(tx, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if browser != nil {
|
||||
return nil, errors.New("dataId '" + req.DataId + "' already exists")
|
||||
}
|
||||
|
||||
browserId, err := models.SharedFormalClientBrowserDAO.CreateBrowser(tx, req.Name, req.Codes, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateFormalClientBrowserResponse{
|
||||
FormalClientBrowserId: browserId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CountFormalClientBrowsers 计算浏览器信息数量
|
||||
func (this *FormalClientBrowserService) CountFormalClientBrowsers(ctx context.Context, req *pb.CountFormalClientBrowsersRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
count, err := models.SharedFormalClientBrowserDAO.CountBrowsers(tx, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListFormalClientBrowsers 列出单页浏览器信息
|
||||
func (this *FormalClientBrowserService) ListFormalClientBrowsers(ctx context.Context, req *pb.ListFormalClientBrowsersRequest) (*pb.ListFormalClientBrowsersResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
browsers, err := models.SharedFormalClientBrowserDAO.ListBrowsers(tx, req.Keyword, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbBrowsers = []*pb.FormalClientBrowser{}
|
||||
for _, browser := range browsers {
|
||||
pbBrowsers = append(pbBrowsers, &pb.FormalClientBrowser{
|
||||
Id: int64(browser.Id),
|
||||
Name: browser.Name,
|
||||
Codes: browser.DecodeCodes(),
|
||||
DataId: browser.DataId,
|
||||
State: types.Int32(browser.State),
|
||||
})
|
||||
}
|
||||
return &pb.ListFormalClientBrowsersResponse{
|
||||
FormalClientBrowsers: pbBrowsers,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateFormalClientBrowser 修改浏览器信息
|
||||
func (this *FormalClientBrowserService) UpdateFormalClientBrowser(ctx context.Context, req *pb.UpdateFormalClientBrowserRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(req.DataId) == 0 {
|
||||
return nil, errors.New("invalid dataId")
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 检查dataId是否已经被使用
|
||||
oldBrowser, err := models.SharedFormalClientBrowserDAO.FindBrowserWithDataId(tx, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if oldBrowser != nil && int64(oldBrowser.Id) != req.FormalClientBrowserId {
|
||||
return nil, errors.New("the dataId '" + req.DataId + "' already has been used")
|
||||
}
|
||||
|
||||
err = models.SharedFormalClientBrowserDAO.UpdateBrowser(tx, req.FormalClientBrowserId, req.Name, req.Codes, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindFormalClientBrowserWithDataId 通过dataId查询浏览器信息
|
||||
func (this *FormalClientBrowserService) FindFormalClientBrowserWithDataId(ctx context.Context, req *pb.FindFormalClientBrowserWithDataIdRequest) (*pb.FindFormalClientBrowserWithDataIdResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
browser, err := models.SharedFormalClientBrowserDAO.FindBrowserWithDataId(tx, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if browser == nil {
|
||||
return &pb.FindFormalClientBrowserWithDataIdResponse{
|
||||
FormalClientBrowser: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &pb.FindFormalClientBrowserWithDataIdResponse{
|
||||
FormalClientBrowser: &pb.FormalClientBrowser{
|
||||
Id: int64(browser.Id),
|
||||
Name: browser.Name,
|
||||
Codes: browser.DecodeCodes(),
|
||||
DataId: browser.DataId,
|
||||
State: types.Int32(browser.State),
|
||||
}}, nil
|
||||
}
|
||||
143
internal/rpc/services/service_formal_client_system.go
Normal file
143
internal/rpc/services/service_formal_client_system.go
Normal file
@@ -0,0 +1,143 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
// FormalClientSystemService 操作系统信息库服务
|
||||
type FormalClientSystemService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// CreateFormalClientSystem 创建操作系统信息
|
||||
func (this *FormalClientSystemService) CreateFormalClientSystem(ctx context.Context, req *pb.CreateFormalClientSystemRequest) (*pb.CreateFormalClientSystemResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查dataId是否存在
|
||||
var tx = this.NullTx()
|
||||
system, err := models.SharedFormalClientSystemDAO.FindSystemWithDataId(tx, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if system != nil {
|
||||
return nil, errors.New("dataId '" + req.DataId + "' already exists")
|
||||
}
|
||||
|
||||
systemId, err := models.SharedFormalClientSystemDAO.CreateSystem(tx, req.Name, req.Codes, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateFormalClientSystemResponse{
|
||||
FormalClientSystemId: systemId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CountFormalClientSystems 计算操作系统信息数量
|
||||
func (this *FormalClientSystemService) CountFormalClientSystems(ctx context.Context, req *pb.CountFormalClientSystemsRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
count, err := models.SharedFormalClientSystemDAO.CountSystems(tx, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListFormalClientSystems 列出单页操作系统信息
|
||||
func (this *FormalClientSystemService) ListFormalClientSystems(ctx context.Context, req *pb.ListFormalClientSystemsRequest) (*pb.ListFormalClientSystemsResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
systems, err := models.SharedFormalClientSystemDAO.ListSystems(tx, req.Keyword, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbSystems = []*pb.FormalClientSystem{}
|
||||
for _, system := range systems {
|
||||
pbSystems = append(pbSystems, &pb.FormalClientSystem{
|
||||
Id: int64(system.Id),
|
||||
Name: system.Name,
|
||||
Codes: system.DecodeCodes(),
|
||||
DataId: system.DataId,
|
||||
State: types.Int32(system.State),
|
||||
})
|
||||
}
|
||||
return &pb.ListFormalClientSystemsResponse{
|
||||
FormalClientSystems: pbSystems,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateFormalClientSystem 修改操作系统信息
|
||||
func (this *FormalClientSystemService) UpdateFormalClientSystem(ctx context.Context, req *pb.UpdateFormalClientSystemRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(req.DataId) == 0 {
|
||||
return nil, errors.New("invalid dataId")
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 检查dataId是否已经被使用
|
||||
oldSystem, err := models.SharedFormalClientSystemDAO.FindSystemWithDataId(tx, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if oldSystem != nil && int64(oldSystem.Id) != req.FormalClientSystemId {
|
||||
return nil, errors.New("the dataId '" + req.DataId + "' already has been used")
|
||||
}
|
||||
|
||||
err = models.SharedFormalClientSystemDAO.UpdateSystem(tx, req.FormalClientSystemId, req.Name, req.Codes, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindFormalClientSystemWithDataId 通过dataId查询操作系统信息
|
||||
func (this *FormalClientSystemService) FindFormalClientSystemWithDataId(ctx context.Context, req *pb.FindFormalClientSystemWithDataIdRequest) (*pb.FindFormalClientSystemWithDataIdResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
system, err := models.SharedFormalClientSystemDAO.FindSystemWithDataId(tx, req.DataId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if system == nil {
|
||||
return &pb.FindFormalClientSystemWithDataIdResponse{
|
||||
FormalClientSystem: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &pb.FindFormalClientSystemWithDataIdResponse{
|
||||
FormalClientSystem: &pb.FormalClientSystem{
|
||||
Id: int64(system.Id),
|
||||
Name: system.Name,
|
||||
Codes: system.DecodeCodes(),
|
||||
DataId: system.DataId,
|
||||
State: types.Int32(system.State),
|
||||
}}, nil
|
||||
}
|
||||
@@ -1709,17 +1709,20 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req
|
||||
return nil
|
||||
}
|
||||
|
||||
systemId, err := models.SharedClientSystemDAO.FindSystemIdWithNameCacheable(tx, result.Name)
|
||||
systemId, err := models.SharedFormalClientSystemDAO.FindSystemIdWithNameCacheable(tx, result.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if systemId == 0 {
|
||||
systemId, err = models.SharedClientSystemDAO.CreateSystem(tx, result.Name)
|
||||
err = models.SharedClientSystemDAO.CreateSystemIfNotExists(tx, result.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 直接返回不再进行操作
|
||||
return nil
|
||||
}
|
||||
key := fmt.Sprintf("%d@%d@%s@%s", result.ServerId, systemId, result.Version, month)
|
||||
var key = fmt.Sprintf("%d@%d@%s@%s", result.ServerId, systemId, result.Version, month)
|
||||
serverStatLocker.Lock()
|
||||
serverHTTPSystemStatMap[key] += result.Count
|
||||
serverStatLocker.Unlock()
|
||||
@@ -1737,15 +1740,18 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req
|
||||
return nil
|
||||
}
|
||||
|
||||
browserId, err := models.SharedClientBrowserDAO.FindBrowserIdWithNameCacheable(tx, result.Name)
|
||||
browserId, err := models.SharedFormalClientBrowserDAO.FindBrowserIdWithNameCacheable(tx, result.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if browserId == 0 {
|
||||
browserId, err = models.SharedClientBrowserDAO.CreateBrowser(tx, result.Name)
|
||||
err = models.SharedClientBrowserDAO.CreateBrowserIfNotExists(tx, result.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 直接返回不再进行操作
|
||||
return nil
|
||||
}
|
||||
key := fmt.Sprintf("%d@%d@%s@%s", result.ServerId, browserId, result.Version, month)
|
||||
serverStatLocker.Lock()
|
||||
|
||||
@@ -31,13 +31,13 @@ func (this *ServerClientBrowserMonthlyStatService) FindTopServerClientBrowserMon
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbStats := []*pb.FindTopServerClientBrowserMonthlyStatsResponse_Stat{}
|
||||
var pbStats = []*pb.FindTopServerClientBrowserMonthlyStatsResponse_Stat{}
|
||||
for _, stat := range statList {
|
||||
pbStat := &pb.FindTopServerClientBrowserMonthlyStatsResponse_Stat{
|
||||
Count: int64(stat.Count),
|
||||
Version: stat.Version,
|
||||
}
|
||||
browser, err := models.SharedClientBrowserDAO.FindEnabledClientBrowser(tx, int64(stat.BrowserId))
|
||||
browser, err := models.SharedFormalClientBrowserDAO.FindEnabledFormalClientBrowser(tx, int64(stat.BrowserId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -48,7 +48,6 @@ func (this *ServerClientBrowserMonthlyStatService) FindTopServerClientBrowserMon
|
||||
Id: int64(browser.Id),
|
||||
Name: browser.Name,
|
||||
}
|
||||
|
||||
pbStats = append(pbStats, pbStat)
|
||||
}
|
||||
return &pb.FindTopServerClientBrowserMonthlyStatsResponse{Stats: pbStats}, nil
|
||||
|
||||
@@ -31,13 +31,13 @@ func (this *ServerClientSystemMonthlyStatService) FindTopServerClientSystemMonth
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbStats := []*pb.FindTopServerClientSystemMonthlyStatsResponse_Stat{}
|
||||
var pbStats = []*pb.FindTopServerClientSystemMonthlyStatsResponse_Stat{}
|
||||
for _, stat := range statList {
|
||||
pbStat := &pb.FindTopServerClientSystemMonthlyStatsResponse_Stat{
|
||||
Count: int64(stat.Count),
|
||||
Version: stat.Version,
|
||||
}
|
||||
system, err := models.SharedClientSystemDAO.FindEnabledClientSystem(tx, int64(stat.SystemId))
|
||||
system, err := models.SharedFormalClientSystemDAO.FindEnabledFormalClientSystem(tx, int64(stat.SystemId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user