pcm-coordinator/internal/logic/core/pulltaskinfologic.go

132 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package core
import (
"context"
"encoding/json"
"fmt"
"github.com/jinzhu/copier"
clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/client"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"gorm.io/gorm"
"log"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
)
type PullTaskInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewPullTaskInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PullTaskInfoLogic {
return &PullTaskInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *PullTaskInfoLogic) PullTaskInfo(req *clientCore.PullTaskInfoReq) (*clientCore.PullTaskInfoResp, error) {
resp := clientCore.PullTaskInfoResp{}
// check the kind of adapter
var kind int32
l.svcCtx.DbEngin.Raw("select type as kind from `t_adapter` where id = ?", req.AdapterId).Scan(&kind)
// pull task list from database
switch kind {
case 2:
var hpcModelList []models.TaskHpc
err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &hpcModelList)
if err != nil {
return nil, err
}
utils.Convert(hpcModelList, &resp.HpcInfoList)
if len(resp.HpcInfoList) > 0 {
for i, hpcInfo := range hpcModelList {
err := copier.CopyWithOption(resp.HpcInfoList[i], hpcInfo, copier.Option{Converters: utils.Converters})
if err != nil {
return nil, err
}
var clusterType string
l.svcCtx.DbEngin.Raw("SELECT label FROM `t_cluster` where id = ? ", hpcInfo.ClusterId).Scan(&clusterType)
utils.Convert(hpcInfo.Environment, &resp.HpcInfoList[i].Environment)
resp.HpcInfoList[i].ClusterType = clusterType
}
}
case 0:
var resourceType int32
l.svcCtx.DbEngin.Raw("select resource_type as resourceType from `t_adapter` where id = ?", req.AdapterId).Scan(&resourceType)
switch resourceType {
case 01:
var cloudModelList []cloud.TaskCloudModel
err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &cloudModelList)
if err != nil {
return nil, err
}
utils.Convert(cloudModelList, &resp.CloudInfoList)
case 02:
var vmModelList []models.TaskVm
err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &vmModelList)
if err != nil {
return nil, err
}
utils.Convert(vmModelList, &resp.VmInfoList)
}
case 1:
var aiModelList []models.TaskAi
err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &aiModelList)
if err != nil {
return nil, err
}
utils.Convert(aiModelList, &resp.AiInfoList)
if len(resp.AiInfoList) > 0 {
for i, aiInfo := range aiModelList {
if resp.AiInfoList[i].Environments != "" {
// 定义一个map来存储解析后的JSON数据
var result map[string]interface{}
// 解析JSON字符串
err := json.Unmarshal([]byte(resp.AiInfoList[i].Environments), &result)
if err != nil {
log.Fatalf("Error parsing JSON: %v", err)
}
// 如果你需要将解析后的map再次转换为JSON字符串可以使用json.MarshalIndent
formattedJSON, err := json.MarshalIndent(result, "", " ")
aiInfo.Environments = string(formattedJSON)
fmt.Println(aiInfo.Environments)
resp.AiInfoList[i].Environments = aiInfo.Environments
}
if resp.AiInfoList[i].Parameters != "" {
// 定义一个map来存储解析后的JSON数据
var result []interface{}
// 解析JSON字符串
err := json.Unmarshal([]byte(resp.AiInfoList[i].Parameters), &result)
if err != nil {
log.Fatalf("Error parsing JSON: %v", err)
}
// 如果你需要将解析后的map再次转换为JSON字符串可以使用json.MarshalIndent
formattedJSON, err := json.MarshalIndent(result, "", " ")
aiInfo.Parameters = string(formattedJSON)
fmt.Println(aiInfo.Parameters)
resp.AiInfoList[i].Parameters = aiInfo.Parameters
}
}
}
}
return &resp, nil
}
func findModelList(adapterId int64, dbEngin *gorm.DB, data interface{}) error {
tx := dbEngin.Where("cluster_id in (select id from t_cluster where adapter_id = ?) AND status not in "+
"('Deleted', 'Succeeded', 'COMPLETED', 'Completed', 'Failed','FAIL','statC','statE')", adapterId).Find(data)
if tx.Error != nil {
return tx.Error
}
return nil
}