From 25d06ff73875ad942c7392d6085d06809f909079 Mon Sep 17 00:00:00 2001 From: Jake <450705171@qq.com> Date: Tue, 8 Jul 2025 16:11:32 +0800 Subject: [PATCH] create algorithm --- internal/handler/ai/createalgorithmhandler.go | 16 +++- internal/handler/routes.go | 2 +- internal/logic/ai/createalgorithmlogic.go | 37 ++++---- internal/types/ai/option.go | 93 +++++++++++++++++++ 4 files changed, 125 insertions(+), 23 deletions(-) create mode 100644 internal/types/ai/option.go diff --git a/internal/handler/ai/createalgorithmhandler.go b/internal/handler/ai/createalgorithmhandler.go index 92b074d40..f65479764 100644 --- a/internal/handler/ai/createalgorithmhandler.go +++ b/internal/handler/ai/createalgorithmhandler.go @@ -1,18 +1,26 @@ package ai import ( - "github.com/zeromicro/go-zero/rest/httpx" + "encoding/json" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/ai" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" - "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" + algorithm "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/ai" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "io" "net/http" ) func CreateAlgorithmHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.CreateAlgorithmReq - if err := httpx.Parse(r, &req); err != nil { + var req algorithm.CreateAlgorithmReq + + body, err := io.ReadAll(r.Body) + if err != nil { + result.ParamErrorResult(r, w, err) + return + } + + if err = json.Unmarshal(body, &req); err != nil { result.ParamErrorResult(r, w, err) return } diff --git a/internal/handler/routes.go b/internal/handler/routes.go index ab90894a2..9ec7c23ec 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -98,7 +98,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { { // 创建算法 Method: http.MethodPost, - Path: "/ai/CreateAlgorithm/:projectId", + Path: "/ai/CreateAlgorithm", Handler: ai.CreateAlgorithmHandler(serverCtx), }, { diff --git a/internal/logic/ai/createalgorithmlogic.go b/internal/logic/ai/createalgorithmlogic.go index 89eec6af1..bfc2f83be 100644 --- a/internal/logic/ai/createalgorithmlogic.go +++ b/internal/logic/ai/createalgorithmlogic.go @@ -16,14 +16,12 @@ package ai import ( "context" - "github.com/jinzhu/copier" + "errors" + "github.com/go-resty/resty/v2" "github.com/zeromicro/go-zero/core/logx" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" - "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" - "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" - "gitlink.org.cn/JointCloud/pcm-modelarts/modelarts" - "k8s.io/apimachinery/pkg/util/json" + algorithm "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/ai" ) type CreateAlgorithmLogic struct { @@ -40,18 +38,21 @@ func NewCreateAlgorithmLogic(ctx context.Context, svcCtx *svc.ServiceContext) *C } } -func (l *CreateAlgorithmLogic) CreateAlgorithm(req *types.CreateAlgorithmReq) (resp *types.CreateAlgorithmResp, err error) { - modelartsReq := &modelarts.CreateAlgorithmReq{} - err = copier.CopyWithOption(modelartsReq, req, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: utils.Converters}) - ListAlgorithmsResp, err := l.svcCtx.ModelArtsRpc.CreateAlgorithm(l.ctx, modelartsReq) - if err != nil { - return nil, result.NewDefaultError(err.Error()) +func (l *CreateAlgorithmLogic) CreateAlgorithm(req *algorithm.CreateAlgorithmReq) (resp *algorithm.CreateAlgorithmResp, err error) { + + cluster := &types.GetClusterByIdResp{} + tx := l.svcCtx.DbEngin.Raw("select * from t_cluster where id = ?", req.ClusterId).Scan(&cluster.ClusterInfo) + if tx.Error != nil { + logx.Errorf(tx.Error.Error()) + return nil, errors.New("cluster create failed") } - marshal, err := json.Marshal(&ListAlgorithmsResp) - if err != nil { - return nil, result.NewDefaultError(err.Error()) - } - json.Unmarshal(marshal, &resp) - err = copier.CopyWithOption(&resp, &ListAlgorithmsResp, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: utils.Converters}) - return resp, nil + + httpClient := resty.New().R() + createAlgorithmResp := &algorithm.CreateAlgorithmResp{} + _, err = httpClient.SetHeader("Content-Type", "application/json"). + SetQueryParams(map[string]string{"pfId": cluster.ClusterInfo.Id}). + SetBody(req). + SetResult(&createAlgorithmResp). + Post(cluster.ClusterInfo.Server + "/ai/algorithm/create") + return createAlgorithmResp, err } diff --git a/internal/types/ai/option.go b/internal/types/ai/option.go new file mode 100644 index 000000000..6d4bc6b34 --- /dev/null +++ b/internal/types/ai/option.go @@ -0,0 +1,93 @@ +package algorithm + +import ( + "encoding/json" + "fmt" +) + +type CreateParameter interface { + AlgorithmCreateParam() +} + +type Source struct { + Jcs JcsBase `json:"jcs,omitempty"` +} + +type JcsBase struct { + UserID int `json:"userID" binding:"required"` + PackageId int `json:"packageId" binding:"required"` + BucketID int `json:"bucketID" binding:"required"` +} + +type CreateAlgorithmReq struct { + Name string `json:"name" binding:"required"` + ClusterId string `json:"clusterId"` + Desc string `json:"desc"` + Src Source `json:"src,omitempty"` + Param CreateParameter `json:"param,omitempty"` +} + +type CreateAlgorithmResp struct { + Code int32 `json:"code,omitempty" copier:"Code"` + Msg string `json:"msg,omitempty" copier:"Msg"` + ErrorMsg string `json:"errorMsg,omitempty" copier:"ErrorMsg"` +} + +type OpenI struct { + BootFile string `json:"bootFile,omitempty"` + DefaultBranch string `json:"defaultBranch,omitempty"` +} + +func (o *OpenI) AlgorithmCreateParam() { +} + +type Octopus struct { +} + +func (o *Octopus) AlgorithmCreateParam() { +} + +func (cp *CreateAlgorithmReq) UnmarshalJSON(data []byte) error { + // 临时结构体:用于捕获原始 JSON 中的 param 字段数据 + type TempCreateParam struct { + Name string `json:"name"` + ClusterId string `json:"clusterId"` + Desc string `json:"desc"` + Src Source `json:"src,omitempty"` + Param json.RawMessage `json:"param,omitempty"` // 捕获原始 JSON 数据 + } + var temp TempCreateParam + if err := json.Unmarshal(data, &temp); err != nil { + return err + } + + // 将临时结构体的字段赋值给原结构体(除 Param 外) + cp.Name = temp.Name + cp.ClusterId = temp.ClusterId + cp.Desc = temp.Desc + cp.Src = temp.Src + + // 解析 param 字段的原始数据为具体类型 + if temp.Param != nil { + // 尝试解析为 OpenI 类型 + var openi OpenI + if err := json.Unmarshal(temp.Param, &openi); err != nil { + // 打印详细错误(如字段不匹配、类型错误等) + fmt.Printf("解析 OpenI 失败: %v\n", err) // 关键调试日志 + } else { + cp.Param = &openi + return nil + } + + // 新增:尝试解析为 Octopus 类型 + var octopus Octopus + if err := json.Unmarshal(temp.Param, &octopus); err == nil { + cp.Param = &octopus + return nil + } + + return fmt.Errorf("unsupported param type in CreateParam") + } + + return nil +} -- 2.34.1