forked from JointCloud/pcm-coordinator
parent
4529c5686d
commit
92dc1d294f
|
@ -1403,10 +1403,15 @@ type ResourceSpecReq {
|
|||
Name string `form:"name,optional"`
|
||||
Status string `form:"status,optional"`
|
||||
changeType string `form:"changeType,optional"`
|
||||
Tag string `form:"tag,optional"` // 标签(0: 训练,1:推理,-1:通用)
|
||||
Tag string `form:"tag,optional"` // 标签(Train: 训练,Inference:推理)
|
||||
PageInfo
|
||||
}
|
||||
|
||||
type FetchResourceSpecReq {
|
||||
ClusterId string `form:"clusterId,optional"`
|
||||
Tag string `form:"tag,optional"`
|
||||
}
|
||||
|
||||
type IdReq {
|
||||
Id string `form:"id,optional" json:"id,optional"`
|
||||
}
|
||||
|
|
|
@ -182,7 +182,7 @@ service pcm {
|
|||
//集群资源规格----- 开始
|
||||
@doc "与Api接口对比集群资源规格"
|
||||
@handler compareResourceSpecHandler
|
||||
get /core/ai/resourceSpec/compare (ResourceSpecReq) returns (PageResult)
|
||||
get /core/ai/resourceSpec/compare (FetchResourceSpecReq) returns (PageResult)
|
||||
|
||||
@doc "同步指定资源规格"
|
||||
@handler syncResourceSpecHandler
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
func CompareResourceSpecHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ResourceSpecReq
|
||||
var req types.FetchResourceSpecReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
|
|
|
@ -63,8 +63,7 @@ func NewCompareResourceSpecLogic(ctx context.Context, svcCtx *svc.ServiceContext
|
|||
}
|
||||
}
|
||||
|
||||
func (l *CompareResourceSpecLogic) CompareResourceSpec(req *types.ResourceSpecReq) (resp *types.PageResult, err error) {
|
||||
log.Debug().Msgf("开始比较资源规格,ClusterId: %s, PageNum: %d, PageSize: %d", req.ClusterId, req.PageNum, req.PageSize)
|
||||
func (l *CompareResourceSpecLogic) CompareResourceSpec(req *types.FetchResourceSpecReq) (resp *types.PageResult, err error) {
|
||||
if req.ClusterId == "" {
|
||||
return resp, nil
|
||||
}
|
||||
|
@ -75,7 +74,8 @@ func (l *CompareResourceSpecLogic) CompareResourceSpec(req *types.ResourceSpecRe
|
|||
log.Debug().Msgf("调用获取ai训练资源接口耗时: %v", time.Since(startTime))
|
||||
if err != nil {
|
||||
log.Error().Msgf("调用第三方接口获取集群资源失败: %v", err)
|
||||
return nil, fmt.Errorf("failed to fetch cluster resources: %w", err)
|
||||
|
||||
return nil, fmt.Errorf("调用第三方接口获取集群资源失败")
|
||||
}
|
||||
|
||||
// 同步资源到数据库
|
||||
|
@ -83,8 +83,7 @@ func (l *CompareResourceSpecLogic) CompareResourceSpec(req *types.ResourceSpecRe
|
|||
return nil, fmt.Errorf("failed to sync resources: %w", err)
|
||||
}
|
||||
|
||||
// 查询数据库结果
|
||||
return l.queryDatabaseResults(req)
|
||||
return
|
||||
}
|
||||
|
||||
func (l *CompareResourceSpecLogic) FetchClusterResources(clusterId string, tag string) ([]APIResponse, error) {
|
||||
|
@ -363,54 +362,3 @@ func (l *CompareResourceSpecLogic) convertToResourceSpec(ClusterId int64, res Re
|
|||
|
||||
return spec
|
||||
}
|
||||
|
||||
func (l *CompareResourceSpecLogic) queryDatabaseResults(req *types.ResourceSpecReq) (*types.PageResult, error) {
|
||||
result := &types.PageResult{
|
||||
PageNum: req.PageNum,
|
||||
PageSize: req.PageSize,
|
||||
}
|
||||
|
||||
query := l.buildBaseQuery(req)
|
||||
if err := query.Count(&result.Total).Error; err != nil {
|
||||
return nil, fmt.Errorf("failed to count records: %w", err)
|
||||
}
|
||||
|
||||
var specs []*models.TResourceSpec
|
||||
if err := query.Model(models.TResourceSpec{}).Preload("BaseResourceSpecs").
|
||||
Scopes(paginate(req.PageNum, req.PageSize)).
|
||||
Find(&specs).Error; err != nil {
|
||||
return nil, fmt.Errorf("failed to query resources: %w", err)
|
||||
}
|
||||
|
||||
result.List = specs
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *CompareResourceSpecLogic) buildBaseQuery(req *types.ResourceSpecReq) *gorm.DB {
|
||||
query := l.svcCtx.DbEngin.Model(&models.TResourceSpec{}).
|
||||
Where("cluster_id = ?", utils.StringToInt64(req.ClusterId)).
|
||||
Where("tag = ?", req.Tag).
|
||||
Where("deleted_at IS NULL")
|
||||
|
||||
if req.Status != "" {
|
||||
query = query.Where("status = ?", req.Status)
|
||||
}
|
||||
if req.ChangeType != "" {
|
||||
query = query.Where("change_type = ?", req.ChangeType)
|
||||
}
|
||||
if req.Type != "" {
|
||||
query = query.Where("type = ?", req.Type)
|
||||
}
|
||||
if req.Name != "" {
|
||||
query = query.Where("name LIKE ?", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
func paginate(pageNum, pageSize int) func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
offset := (pageNum - 1) * pageSize
|
||||
return db.Offset(offset).Limit(pageSize).Order("create_time DESC")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,3 +76,10 @@ func (l *PageResourceRangeLogic) buildBaseQuery(req *types.ResourceSpecReq) *gor
|
|||
|
||||
return query
|
||||
}
|
||||
|
||||
func paginate(pageNum, pageSize int) func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
offset := (pageNum - 1) * pageSize
|
||||
return db.Offset(offset).Limit(pageSize).Order("create_time DESC")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,8 @@ func (l *SyncResourceSpecLogic) SyncResourceSpec(req *types.SyncResourceReq) (re
|
|||
apiResources, err := compareLogic.FetchClusterResources(strconv.FormatInt(mainSpec.ClusterId, 10), mainSpec.Tag)
|
||||
log.Debug().Msgf("调用获取ai训练资源接口耗时: %v", time.Since(startTime))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch cluster resources: %w", err)
|
||||
log.Error().Err(err).Msg("同步集群资源失败")
|
||||
return nil, fmt.Errorf("同步集群资源失败,请稍后重试")
|
||||
}
|
||||
for _, response := range apiResources {
|
||||
// 转换API响应到数据库模型
|
||||
|
|
|
@ -2276,6 +2276,11 @@ type Fault struct {
|
|||
Details string `json:"details " copier:"Details"`
|
||||
}
|
||||
|
||||
type FetchResourceSpecReq struct {
|
||||
ClusterId string `form:"clusterId,optional"`
|
||||
Tag string `form:"tag,optional"`
|
||||
}
|
||||
|
||||
type Fields struct {
|
||||
}
|
||||
|
||||
|
@ -4640,7 +4645,7 @@ type ResourceSpecReq struct {
|
|||
Name string `form:"name,optional"`
|
||||
Status string `form:"status,optional"`
|
||||
ChangeType string `form:"changeType,optional"`
|
||||
Tag string `form:"tag,optional"` // 标签(0: 训练,1:推理,-1:通用)
|
||||
Tag string `form:"tag,optional"` // 标签(Train: 训练,Inference:推理)
|
||||
PageInfo
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue