core 返回统一

This commit is contained in:
zhangwei 2023-04-27 16:57:11 +08:00
parent adeacc8b3f
commit a06028d87c
9 changed files with 29 additions and 62 deletions

View File

@ -184,19 +184,10 @@ type (
serviceName string `yaml:"serviceName"`
metadata interface{} `yaml:"metadata"`
}
scheduleTaskResp {
Code int32 `json:"code"`
Msg string `json:"msg"`
}
)
type (
taskListResp {
Code int32 `json:"code"`
Msg string `json:"msg"`
Data Data `json:"data"`
}
Data {
TotalCount int `json:"totalCount"`
CardTime float32 `json:"cardTime"`
TotalRunTime float32 `json:"totalRunTime"`

View File

@ -22,7 +22,7 @@ info(
)
service pcm {
@handler scheduleTaskHandler
post /core/scheduleTask (scheduleTaskReq) returns (scheduleTaskResp)
post /core/scheduleTask (scheduleTaskReq) returns ()
@handler TaskListHandler
get /core/taskList () returns (taskListResp)
@ -39,9 +39,6 @@ service pcm {
@handler listRegionHandler
get /core/listRegion () returns (listRegionResp)
// @handler syncInfoHandler
// get /core/syncInfo (syncInfoReq) returns (syncInfoResp)
@handler getComputingPowerHandler
get /core/getComputingPower returns (cpResp)

View File

@ -1,6 +1,7 @@
package core
import (
"PCM/common/result"
"PCM/common/tool"
"net/http"
@ -19,16 +20,17 @@ func ScheduleTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
// 解析yaml文件
_, fileHeader, err := r.FormFile("file")
if err != nil {
result.HttpResult(r, w, nil, err)
return
}
err = tool.Yaml2struct(fileHeader, &req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
result.HttpResult(r, w, nil, err)
return
}
l := core.NewScheduleTaskLogic(r.Context(), svcCtx)
resp, err := l.ScheduleTask(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
err = l.ScheduleTask(&req)
result.HttpResult(r, w, nil, err)
}
}

View File

@ -1,6 +1,7 @@
package core
import (
"PCM/common/result"
"net/http"
"PCM/adaptor/PCM-CORE/api/internal/logic/core"
@ -17,11 +18,7 @@ func SubmitJobHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := core.NewScheduleTaskLogic(r.Context(), svcCtx)
resp, err := l.ScheduleTask(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
err := l.ScheduleTask(&req)
result.HttpResult(r, w, nil, err)
}
}

View File

@ -1,21 +1,17 @@
package core
import (
"PCM/common/result"
"net/http"
"PCM/adaptor/PCM-CORE/api/internal/logic/core"
"PCM/adaptor/PCM-CORE/api/internal/svc"
"github.com/zeromicro/go-zero/rest/httpx"
)
func TaskListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := core.NewTaskListLogic(r.Context(), svcCtx)
resp, err := l.TaskList()
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
result.HttpResult(r, w, resp, err)
}
}

View File

@ -31,7 +31,7 @@ func NewScheduleTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sche
}
}
func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *types.ScheduleTaskResp, err error) {
func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (err error) {
// check param
//checkResult := l.checkSubmitReq(req)
//if checkResult != "" {
@ -39,7 +39,7 @@ func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *type
//}
bytes, err := json.Marshal(req)
if err != nil {
return nil, err
return err
}
// construct task info
taskModel := model.Task{
@ -54,7 +54,7 @@ func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *type
// save the task in mysql and return id
tx := l.svcCtx.DbEngin.Create(&taskModel)
if tx.Error != nil {
return nil, tx.Error
return tx.Error
}
// push message into topic
@ -63,7 +63,7 @@ func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *type
reqMessage, err := json.Marshal(task)
if err != nil {
logx.Error(err)
return nil, err
return err
}
switch task.ServiceName {
case "kubeNative":
@ -74,10 +74,7 @@ func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *type
l.svcCtx.ScheduleAiClient.Push(string(reqMessage))
}
}
return &types.ScheduleTaskResp{
Code: 200,
Msg: "Success",
}, nil
return nil
}
func (l *ScheduleTaskLogic) checkSubmitReq(req *types.ScheduleTaskReq) string {

View File

@ -29,7 +29,7 @@ func NewTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TaskList
func (l *TaskListLogic) TaskList() (resp *types.TaskListResp, err error) {
resp = &types.TaskListResp{}
// 查询总运行时长
tx := l.svcCtx.DbEngin.Raw("select sum(running_time)/3600 as total_run_time from (select sum(running_time) as running_time from hpc union all select sum(running_time) as running_time from cloud union all select sum(running_time) as running_time from ai) runtime").Scan(&resp.Data.TotalRunTime)
tx := l.svcCtx.DbEngin.Raw("select sum(running_time)/3600 as total_run_time from (select sum(running_time) as running_time from hpc union all select sum(running_time) as running_time from cloud union all select sum(running_time) as running_time from ai) runtime").Scan(&resp.TotalRunTime)
if tx.Error != nil {
}
@ -55,24 +55,22 @@ func (l *TaskListLogic) TaskList() (resp *types.TaskListResp, err error) {
}
task.ServiceName = strings.Join(names, ",")
}
resp.Data.Tasks = append(resp.Data.Tasks, types.Task{
resp.Tasks = append(resp.Tasks, types.Task{
ServiceName: task.ServiceName,
Name: task.Name,
Strategy: int(task.Strategy),
SynergyStatus: enum.Status(task.SynergyStatus).String(),
SynergyStatus: enum.SynergyStatus(task.SynergyStatus).String(),
Status: task.Status,
})
}
// 运行卡时数
tx = l.svcCtx.DbEngin.Model(&model.Hpc{}).Select("(CASE WHEN SUM(running_time * card_count)/3600 IS NULL THEN 0 ELSE SUM(running_time * card_count)/3600 END )as cardTime").Find(&resp.Data.CardTime)
tx = l.svcCtx.DbEngin.Model(&model.Hpc{}).Select("(CASE WHEN SUM(running_time * card_count)/3600 IS NULL THEN 0 ELSE SUM(running_time * card_count)/3600 END )as cardTime").Find(&resp.CardTime)
if tx.Error != nil {
return nil, tx.Error
}
// 运行任务合计数
resp.Data.TotalCount = len(tasks)
resp.Code = 200
resp.Msg = "Success"
resp.TotalCount = len(tasks)
return resp, nil
}

View File

@ -166,18 +166,7 @@ type TaskInfo struct {
Metadata interface{} `yaml:"metadata"`
}
type ScheduleTaskResp struct {
Code int32 `json:"code"`
Msg string `json:"msg"`
}
type TaskListResp struct {
Code int32 `json:"code"`
Msg string `json:"msg"`
Data Data `json:"data"`
}
type Data struct {
TotalCount int `json:"totalCount"`
CardTime float32 `json:"cardTime"`
TotalRunTime float32 `json:"totalRunTime"`

View File

@ -1,13 +1,13 @@
package enum
type synergyStatus int64
type SynergyStatus int64
const (
SYNERGIZED synergyStatus = 0
NOT_SYNERGIZED synergyStatus = 1
SYNERGIZED SynergyStatus = 0
NOT_SYNERGIZED SynergyStatus = 1
)
func (s synergyStatus) String() string {
func (s SynergyStatus) String() string {
switch s {
case SYNERGIZED:
return "已协同"