Compare commits

...

4 Commits

2 changed files with 115 additions and 29 deletions

View File

@ -1,6 +1,7 @@
package api
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
@ -8,6 +9,7 @@ import (
"gitlink.org.cn/JointCloud/pcm-participant-ai/dataset"
aiModel "gitlink.org.cn/JointCloud/pcm-participant-ai/model"
"gitlink.org.cn/JointCloud/pcm-participant-ai/service"
"gitlink.org.cn/JointCloud/pcm-participant-ai/task"
"gitlink.org.cn/JointCloud/pcm-participant-openi/model"
"net/http"
"strconv"
@ -42,7 +44,7 @@ func (a *aiApi) TrainAlgorithmsHandler(c *gin.Context) {
}
algorithms, err := a.service.TrainAlgorithms(c, pfId)
if err != nil {
model.Response(c, http.StatusInternalServerError, "failed to get train algorithms", nil)
model.Response(c, http.StatusBadRequest, "failed to get train algorithms", err.Error())
return
}
model.Response(c, http.StatusOK, "success", algorithms)
@ -52,7 +54,7 @@ func (a *aiApi) TrainAlgorithmsHandler(c *gin.Context) {
func (a *aiApi) AllTrainAlgorithmsHandler(c *gin.Context) {
algorithms, err := a.service.AllTrainAlgorithms(c)
if err != nil {
model.Response(c, http.StatusInternalServerError, "failed to get all train algorithms", nil)
model.Response(c, http.StatusInternalServerError, "failed to get all train algorithms", err)
return
}
model.Response(c, http.StatusOK, "success", algorithms)
@ -69,23 +71,19 @@ func (a *aiApi) CreateAlgorithmHandler(c *gin.Context) {
var param algorithm.CreateParam
if err := c.ShouldBindJSON(&param); err != nil {
if ve, ok := err.(validator.ValidationErrors); ok {
var ve validator.ValidationErrors
if errors.As(err, &ve) {
var errorMsg []string
for _, e := range ve {
errorMsg = append(errorMsg, fmt.Sprintf("字段 %s 验证失败: %s", e.Field(), e.Tag()))
}
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), nil)
} else {
model.Response(c, http.StatusBadRequest, "请求体解析失败: "+err.Error(), nil)
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), err)
}
return
//model.Response(c, http.StatusBadRequest, "invalid request body", nil)
//return
}
resp, err := a.service.CreateAlgorithm(c.Request.Context(), pfId, &param)
if err != nil {
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", nil)
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", err)
return
}
@ -103,21 +101,20 @@ func (a *aiApi) CreateDatasetHandler(c *gin.Context) {
var param dataset.CreateParam
if err := c.ShouldBindJSON(&param); err != nil {
if ve, ok := err.(validator.ValidationErrors); ok {
var ve validator.ValidationErrors
if errors.As(err, &ve) {
var errorMsg []string
for _, e := range ve {
errorMsg = append(errorMsg, fmt.Sprintf("字段 %s 验证失败: %s", e.Field(), e.Tag()))
}
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), nil)
} else {
model.Response(c, http.StatusBadRequest, "请求体解析失败: "+err.Error(), nil)
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), err)
}
return
}
resp, err := a.service.CreateDataset(c.Request.Context(), pfId, &param)
if err != nil {
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", nil)
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", err)
return
}
@ -129,27 +126,26 @@ func (a *aiApi) CreateModelHandler(c *gin.Context) {
pfIdStr := c.Query("pfId")
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
var param aiModel.CreateParam
if err := c.ShouldBindJSON(&param); err != nil {
if ve, ok := err.(validator.ValidationErrors); ok {
var ve validator.ValidationErrors
if errors.As(err, &ve) {
var errorMsg []string
for _, e := range ve {
errorMsg = append(errorMsg, fmt.Sprintf("字段 %s 验证失败: %s", e.Field(), e.Tag()))
}
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), nil)
} else {
model.Response(c, http.StatusBadRequest, "请求体解析失败: "+err.Error(), nil)
model.Response(c, http.StatusBadRequest, "请求体格式错误: "+strings.Join(errorMsg, "; "), err)
}
return
}
resp, err := a.service.CreateModel(c.Request.Context(), pfId, &param)
if err != nil {
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", nil)
model.Response(c, http.StatusInternalServerError, "failed to create algorithm", err)
return
}
@ -162,11 +158,16 @@ func (a *aiApi) GetResourceSpecsHandler(c *gin.Context) {
rtype := c.Query("rType")
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
a.service.GetResourceSpecs(c, pfId, rtype)
model.Response(c, http.StatusOK, "success", nil)
_, err = a.service.GetResourceSpecs(c, pfId, rtype)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
model.Response(c, http.StatusOK, "success", err)
}
// TrainResourcesHandler 处理获取特定平台训练资源的请求
@ -174,12 +175,12 @@ func (a *aiApi) TrainResourcesHandler(c *gin.Context) {
pfIdStr := c.Query("pfId")
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", nil)
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
resources, err := a.service.TrainResources(c, pfId)
if err != nil {
model.Response(c, http.StatusInternalServerError, "failed to get train resources", nil)
model.Response(c, http.StatusInternalServerError, "failed to get train resources", err)
return
}
model.Response(c, http.StatusOK, "success", resources)
@ -189,7 +190,7 @@ func (a *aiApi) TrainResourcesHandler(c *gin.Context) {
func (a *aiApi) AllTrainResourcesHandler(c *gin.Context) {
resources, err := a.service.AllTrainResources(c)
if err != nil {
model.Response(c, http.StatusInternalServerError, "failed to get all train resources", nil)
model.Response(c, http.StatusInternalServerError, "failed to get all train resources", err)
return
}
model.Response(c, http.StatusOK, "success", resources)
@ -199,7 +200,7 @@ func (a *aiApi) AllTrainResourcesHandler(c *gin.Context) {
func (a *aiApi) CreateTrainTaskHandler(c *gin.Context) {
var param service.CreateTrainTaskParam
if err := c.ShouldBindJSON(&param); err != nil {
model.Response(c, http.StatusBadRequest, "invalid request body", nil)
model.Response(c, http.StatusBadRequest, "invalid request body", err)
return
}
resp, err := a.service.CreateTrainTask(c, &param)
@ -209,3 +210,78 @@ func (a *aiApi) CreateTrainTaskHandler(c *gin.Context) {
}
model.Response(c, http.StatusOK, "success", resp)
}
// TaskResultSyncHandler 同步任务结果数据
func (a *aiApi) TaskResultSyncHandler(c *gin.Context) {
pfIdStr := c.Query("pfId")
var param task.ResultSyncParam
if err := c.ShouldBindJSON(&param); err != nil {
model.Response(c, http.StatusBadRequest, "invalid request body", err)
return
}
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
err = a.service.TaskResultSync(c, pfId, &param)
if err != nil {
model.Response(c, http.StatusBadRequest, "task Sync failed", err)
return
}
model.Response(c, http.StatusOK, "success", err)
}
// TaskLogHandler 查询任务日志
func (a *aiApi) TaskLogHandler(c *gin.Context) {
pfIdStr := c.Query("pfId")
taskId := c.Query("taskId")
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
_, err = a.service.TaskLog(c, pfId, taskId)
if err != nil {
model.Response(c, http.StatusBadRequest, "get task log failed", err)
return
}
model.Response(c, http.StatusOK, "success", err)
}
// TrainTaskDetailHandler 查询训练任务详情
func (a *aiApi) TrainTaskDetailHandler(c *gin.Context) {
pfIdStr := c.Query("pfId")
taskId := c.Query("taskId")
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
_, err = a.service.GetTrainingTask(c, pfId, taskId)
if err != nil {
model.Response(c, http.StatusBadRequest, "get task detail failed", err)
return
}
model.Response(c, http.StatusOK, "success", err)
}
// InferTaskDetailHandler 查询推理任务详情
func (a *aiApi) InferTaskDetailHandler(c *gin.Context) {
pfIdStr := c.Query("pfId")
taskId := c.Query("taskId")
pfId, err := strconv.ParseInt(pfIdStr, 10, 64)
if err != nil {
model.Response(c, http.StatusBadRequest, "invalid pfId", err)
return
}
_, err = a.service.GetInferenceTask(c, pfId, taskId)
if err != nil {
model.Response(c, http.StatusBadRequest, "get infer task detail failed", err)
return
}
model.Response(c, http.StatusOK, "success", err)
}

View File

@ -9,18 +9,28 @@ func AIRoutes(server *gin.Engine) {
ai := server.Group("/ai")
aiApi := api.AiApi
{
//算法路由
ai.GET("/algorithm/get", aiApi.TrainAlgorithmsHandler)
ai.GET("/algorithm/list", aiApi.AllTrainAlgorithmsHandler)
ai.POST("/algorithm/create", aiApi.CreateAlgorithmHandler)
//数据集路由
ai.POST("/dataset/create", aiApi.CreateDatasetHandler)
// 模型相关路由
ai.POST("/model/create", aiApi.CreateModelHandler)
// 资源相关路由
ai.GET("/resource/specs", aiApi.GetResourceSpecsHandler)
ai.GET("/resource/get", aiApi.TrainResourcesHandler)
ai.GET("/resource/list", aiApi.AllTrainResourcesHandler)
// 算法相关路由
// 任务相关路由
ai.GET("/task/train", aiApi.CreateTrainTaskHandler)
ai.GET("/task/sync", aiApi.TaskResultSyncHandler)
ai.GET("/task/log", aiApi.TaskLogHandler)
ai.GET("/task/train/detail", aiApi.TrainTaskDetailHandler)
ai.GET("/task/infer/detail", aiApi.InferTaskDetailHandler)
}
}