From 1f43569d85d4d001c8fe2e35a4fe5e6564aafa71 Mon Sep 17 00:00:00 2001 From: "894646498@qq.com" <13786100335> Date: Thu, 20 Apr 2023 09:19:30 +0800 Subject: [PATCH] =?UTF-8?q?gorm=E6=9B=BF=E6=8D=A2sqlx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PCM-AI/PCM-MODELARTS/rpc/pcmmodelarts.go | 25 +------ .../internal/logic/core/listcenterlogic.go | 2 +- .../internal/logic/core/scheduletasklogic.go | 44 ++++++------ .../api/internal/logic/core/tasklistlogic.go | 49 +++---------- .../api/internal/mqs/kq/ScheduleAi.go | 22 +++--- .../api/internal/mqs/kq/ScheduleCloud.go | 6 +- .../api/internal/mqs/kq/ScheduleHpc.go | 22 +++--- .../api/internal/svc/servicecontext.go | 15 +--- adaptor/PCM-CORE/api/pcm.go | 55 --------------- adaptor/PCM-CORE/model/aimodel_gen.go | 33 +++++---- adaptor/PCM-CORE/model/cloudmodel_gen.go | 35 +++++----- adaptor/PCM-CORE/model/hpcmodel_gen.go | 37 +++++----- .../rpc/internal/logic/infolistlogic.go | 70 ++++++------------- .../rpc/internal/logic/syncinfologic.go | 20 +++--- .../rpc/internal/svc/servicecontext.go | 21 ++++-- adaptor/PCM-CORE/rpc/pcmcore.go | 23 ------ go.mod | 3 +- go.sum | 7 -- 18 files changed, 157 insertions(+), 332 deletions(-) diff --git a/adaptor/PCM-AI/PCM-MODELARTS/rpc/pcmmodelarts.go b/adaptor/PCM-AI/PCM-MODELARTS/rpc/pcmmodelarts.go index b07a930e..3626f2c7 100644 --- a/adaptor/PCM-AI/PCM-MODELARTS/rpc/pcmmodelarts.go +++ b/adaptor/PCM-AI/PCM-MODELARTS/rpc/pcmmodelarts.go @@ -23,30 +23,7 @@ import ( var configFile = flag.String("f", "adaptor/PCM-AI/PCM-MODELARTS/rpc/etc/pcmmodelarts.yaml", "the config file") func main() { - //flag.Parse() - // - //var c config.Config - //conf.MustLoad(*configFile, &c) - //// start log component - //logx.MustSetup(c.LogConf) - //ctx := svc.NewServiceContext(c) - //ctx.Cron.Start() - //s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { - // modelarts.RegisterModelArtsServer(grpcServer, server.NewModelArtsServer(ctx)) - // - // if c.Mode == service.DevMode || c.Mode == service.TestMode { - // reflection.Register(grpcServer) - // } - //}) - // - ////rpc log - //s.AddUnaryInterceptors(rpcserver.LoggerInterceptor) - //defer s.Stop() - //logx.Infof("Starting rpc server at %s...\n", c.ListenOn) - //initCron(ctx) - //s.Start() - - //-------------- + flag.Parse() var bootstrapConfig commonConfig.BootstrapConfig diff --git a/adaptor/PCM-CORE/api/internal/logic/core/listcenterlogic.go b/adaptor/PCM-CORE/api/internal/logic/core/listcenterlogic.go index aef88fb7..5d9375c3 100644 --- a/adaptor/PCM-CORE/api/internal/logic/core/listcenterlogic.go +++ b/adaptor/PCM-CORE/api/internal/logic/core/listcenterlogic.go @@ -29,7 +29,7 @@ func (l *ListCenterLogic) ListCenter() (*types.ListCenterResp, error) { //var centersModel []model.ComputeCenter var resp types.ListCenterResp - centersModel, _ = l.svcCtx.CenterOverviewModel.FindAll(l.ctx) + l.svcCtx.DbEngin.Find(¢ersModel) var centerModelV = *centersModel diff --git a/adaptor/PCM-CORE/api/internal/logic/core/scheduletasklogic.go b/adaptor/PCM-CORE/api/internal/logic/core/scheduletasklogic.go index d9fdc286..9719e571 100644 --- a/adaptor/PCM-CORE/api/internal/logic/core/scheduletasklogic.go +++ b/adaptor/PCM-CORE/api/internal/logic/core/scheduletasklogic.go @@ -1,14 +1,10 @@ package core import ( - "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/modelartsclient" "PCM/adaptor/PCM-CORE/api/internal/svc" "PCM/adaptor/PCM-CORE/api/internal/types" "PCM/adaptor/PCM-CORE/model" - "PCM/adaptor/PCM-HPC/PCM-AC/rpc/hpcacclient" - "PCM/common/tool" "context" - "database/sql" "encoding/json" "github.com/zeromicro/go-zero/core/logx" appv1 "k8s.io/api/apps/v1" @@ -58,12 +54,12 @@ func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *type } // save the task in mysql and return id - result, err := l.svcCtx.TaskModel.Insert(l.ctx, &task) - if err != nil { - return nil, err + tx := l.svcCtx.DbEngin.Create(&task) + + if tx.Error != nil { + return nil, tx.Error } - id, _ := result.LastInsertId() - req.TaskId = id + req.TaskId = task.Id reqMessage, err := json.Marshal(req) if err != nil { return nil, err @@ -81,7 +77,7 @@ func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *type } func (l *ScheduleTaskLogic) checkSubmitReq(req *types.ScheduleTaskReq) string { - var rows *sql.Rows + //var rows *sql.Rows switch req.ServiceName { case "kubeNative": //bytes, err := json.Marshal(req.Metadata) @@ -100,20 +96,20 @@ func (l *ScheduleTaskLogic) checkSubmitReq(req *types.ScheduleTaskReq) string { // } // } //} - case "modelArts": - var modelArtsReq modelartsclient.CreateTrainingJobReq - tool.Convert(req.Metadata, &modelArtsReq) - rows, _ = l.svcCtx.Db.Query("select id from ai where project_id = ? and name = ?", modelArtsReq.ProjectId, modelArtsReq.Metadata.Name) - if rows != nil && rows.Next() { - return "data already exists." - } - case "ac": - var acReq *hpcacclient.SubmitJobReq - tool.Convert(req.Metadata, &acReq) - rows, _ = l.svcCtx.Db.Query("select id from hpc where name = ?", acReq.Appname) - if rows != nil && rows.Next() { - return "data already exists." - } + //case "modelArts": + // var modelArtsReq modelartsclient.CreateTrainingJobReq + // tool.Convert(req.Metadata, &modelArtsReq) + // rows, _ = l.svcCtx.Db.Query("select id from ai where project_id = ? and name = ?", modelArtsReq.ProjectId, modelArtsReq.Metadata.Name) + // if rows != nil && rows.Next() { + // return "data already exists." + // } + //case "ac": + // var acReq *hpcacclient.SubmitJobReq + // tool.Convert(req.Metadata, &acReq) + // rows, _ = l.svcCtx.Db.Query("select id from hpc where name = ?", acReq.Appname) + // if rows != nil && rows.Next() { + // return "data already exists." + // } } return "" diff --git a/adaptor/PCM-CORE/api/internal/logic/core/tasklistlogic.go b/adaptor/PCM-CORE/api/internal/logic/core/tasklistlogic.go index 76072b68..6defe120 100644 --- a/adaptor/PCM-CORE/api/internal/logic/core/tasklistlogic.go +++ b/adaptor/PCM-CORE/api/internal/logic/core/tasklistlogic.go @@ -27,45 +27,18 @@ func NewTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TaskList func (l *TaskListLogic) TaskList() (resp *types.TaskListResp, err error) { resp = &types.TaskListResp{} // 查询总运行时长 - runningTimeRows, err := l.svcCtx.Db.Query("select sum(running_time)/3600 as running_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") - if err != nil { - logx.Error(err) - return nil, err - } - for runningTimeRows.Next() { - runningTimeRows.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.Data.TotalRunTime) + if tx.Error != nil { + } // 查询任务数据 var tasks []model.Task - err = l.svcCtx.SqlConn.QueryRows(&tasks, "select * from task where deleted_flag = 0") - if err != nil { - logx.Error(err) - return nil, err - } - // 查询超算数据 - var hpcInfos []model.Hpc - err = l.svcCtx.SqlConn.QueryRows(&hpcInfos, "select * from hpc where deleted_flag = 0") - if err != nil { - logx.Error(err) - return nil, err - } - // 查询智算数据 - var aiInfos []model.Ai - err = l.svcCtx.SqlConn.QueryRows(&aiInfos, "select * from ai where deleted_flag = 0") - if err != nil { + tx = l.svcCtx.DbEngin.Find(&tasks) + if tx.Error != nil { logx.Error(err) - return nil, err - } - // 查询云算数据 - var cloudInfos []model.Cloud - err = l.svcCtx.SqlConn.QueryRows(&cloudInfos, "select * from cloud where deleted_flag = 0") - - if err != nil { - logx.Error(err) - return nil, err + return nil, tx.Error } for _, task := range tasks { - resp.Data.Tasks = append(resp.Data.Tasks, types.Task{ ServiceName: task.ServiceName, Name: task.Name, @@ -75,13 +48,11 @@ func (l *TaskListLogic) TaskList() (resp *types.TaskListResp, err error) { }) } // 运行卡时数 - rows, err := l.svcCtx.Db.Query("select SUM(running_time * card_count)/3600 from hpc where deleted_flag = 0") - if err != nil { - return nil, err - } - for rows.Next() { - rows.Scan(&resp.Data.CardTime) + tx = l.svcCtx.DbEngin.Model(&model.Hpc{}).Select("SUM(running_time * card_count)/3600 as cardTime").Find(&resp.Data.CardTime) + if tx.Error != nil { + return nil, tx.Error } + // 运行任务合计数 resp.Data.TotalCount = len(tasks) resp.Code = 200 diff --git a/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleAi.go b/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleAi.go index 9e35c1ae..124b2236 100644 --- a/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleAi.go +++ b/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleAi.go @@ -3,7 +3,7 @@ package kq import ( "PCM/adaptor/PCM-CORE/api/internal/svc" "PCM/adaptor/PCM-CORE/api/internal/types" - "PCM/common/param" + "PCM/adaptor/PCM-CORE/model" "PCM/common/tool" "context" "encoding/json" @@ -30,13 +30,13 @@ func (l *ScheduleAiMq) Consume(_, val string) error { // 接受消息 var req *types.ScheduleTaskReq json.Unmarshal([]byte(val), &req) - var aiBaseList []param.AiBase - tool.Convert(req.Metadata, &aiBaseList) - for index, _ := range aiBaseList { - aiBaseList[index].TaskId = req.TaskId - aiBaseList[index].Status = "Saved" + var aiList []model.Ai + tool.Convert(req.Metadata, &aiList) + for index, _ := range aiList { + aiList[index].TaskId = req.TaskId + aiList[index].Status = "Saved" // 解析超算信息以yaml形式存储到数据库中 - jsonBytes, err := json.Marshal(aiBaseList[index]) + jsonBytes, err := json.Marshal(aiList[index]) if err != nil { return err } @@ -44,12 +44,12 @@ func (l *ScheduleAiMq) Consume(_, val string) error { if err != nil { return err } - aiBaseList[index].YamlString = string(bytes) + aiList[index].YamlString = string(bytes) } // 存储数据 - _, err := l.svcCtx.Db.NamedExec("insert into ai (task_id,service_name,name,status,project_id) values (:task_id,:service_name,:name,:status,:project_id)", aiBaseList) - if err != nil { - return err + tx := l.svcCtx.DbEngin.Create(aiList) + if tx.Error != nil { + return tx.Error } return nil } diff --git a/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleCloud.go b/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleCloud.go index 4f0039d0..5ade3142 100644 --- a/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleCloud.go +++ b/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleCloud.go @@ -49,9 +49,9 @@ func (l *ScheduleCloudMq) Consume(_, val string) error { } // 存储数据 - _, err = l.svcCtx.Db.NamedExec("insert into cloud (task_id,kind,namespace,name,api_version,status,service_name,yaml_string) values (:task_id,:kind,:namespace,:name,:api_version,:status,:service_name,:yaml_string)", clouds) - if err != nil { - return err + tx := l.svcCtx.DbEngin.Create(clouds) + if tx.Error != nil { + return tx.Error } return nil diff --git a/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleHpc.go b/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleHpc.go index bb412bb7..a4a756fc 100644 --- a/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleHpc.go +++ b/adaptor/PCM-CORE/api/internal/mqs/kq/ScheduleHpc.go @@ -3,7 +3,7 @@ package kq import ( "PCM/adaptor/PCM-CORE/api/internal/svc" "PCM/adaptor/PCM-CORE/api/internal/types" - "PCM/common/param" + "PCM/adaptor/PCM-CORE/model" "PCM/common/tool" "context" "encoding/json" @@ -30,13 +30,13 @@ func (l *ScheduleHpcMq) Consume(_, val string) error { // 接受消息 var req *types.ScheduleTaskReq json.Unmarshal([]byte(val), &req) - var hpcBaseList []param.HpcBase - tool.Convert(req.Metadata, &hpcBaseList) - for index, _ := range hpcBaseList { - hpcBaseList[index].TaskId = req.TaskId - hpcBaseList[index].Status = "Saved" + var hpcList []model.Hpc + tool.Convert(req.Metadata, &hpcList) + for index, _ := range hpcList { + hpcList[index].TaskId = req.TaskId + hpcList[index].Status = "Saved" // 解析超算信息以yaml形式存储到数据库中 - jsonBytes, err := json.Marshal(hpcBaseList[index]) + jsonBytes, err := json.Marshal(hpcList[index]) if err != nil { return err } @@ -44,12 +44,12 @@ func (l *ScheduleHpcMq) Consume(_, val string) error { if err != nil { return err } - hpcBaseList[index].YamlString = string(bytes) + hpcList[index].YamlString = string(bytes) } // 存储数据 - _, err := l.svcCtx.Db.NamedExec("insert into hpc (task_id,service_name,card_count,name,work_dir,wall_time,status) values (:task_id,:service_name,:card_count,:name,:work_dir,:wall_time,:status)", hpcBaseList) - if err != nil { - return err + tx := l.svcCtx.DbEngin.Create(hpcList) + if tx.Error != nil { + return tx.Error } return nil } diff --git a/adaptor/PCM-CORE/api/internal/svc/servicecontext.go b/adaptor/PCM-CORE/api/internal/svc/servicecontext.go index 478b6cfb..98aafb3e 100644 --- a/adaptor/PCM-CORE/api/internal/svc/servicecontext.go +++ b/adaptor/PCM-CORE/api/internal/svc/servicecontext.go @@ -3,14 +3,11 @@ package svc import ( "PCM/adaptor/PCM-AI/PCM-MODELARTS/rpc/modelartsclient" "PCM/adaptor/PCM-CORE/api/internal/config" - "PCM/adaptor/PCM-CORE/model" "PCM/adaptor/PCM-HPC/PCM-AC/rpc/hpcacclient" "PCM/adaptor/PCM-HPC/PCM-TH/rpc/hpcthclient" "github.com/go-redis/redis/v8" - sql "github.com/jmoiron/sqlx" "github.com/robfig/cron/v3" "github.com/zeromicro/go-queue/kq" - "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/zrpc" "gorm.io/driver/mysql" "gorm.io/gorm" @@ -19,14 +16,10 @@ import ( type ServiceContext struct { Config config.Config - ScheduleHpcClient *kq.Pusher RedisClient *redis.Client + ScheduleHpcClient *kq.Pusher ScheduleCloudClient *kq.Pusher ScheduleAiClient *kq.Pusher - TaskModel model.TaskModel - CenterOverviewModel model.CenterOverviewModel - SqlConn sqlx.SqlConn - Db *sql.DB Cron *cron.Cron ModelArtsRpc modelartsclient.ModelArts DbEngin *gorm.DB @@ -35,8 +28,6 @@ type ServiceContext struct { } func NewServiceContext(c config.Config) *ServiceContext { - sqlConn := sqlx.NewMysql(c.DB.DataSource) - db, _ := sql.Open("mysql", c.DB.DataSource) //启动Gorm支持 dbEngin, _ := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{ NamingStrategy: schema.NamingStrategy{ @@ -47,14 +38,10 @@ func NewServiceContext(c config.Config) *ServiceContext { Cron: cron.New(cron.WithSeconds()), DbEngin: dbEngin, Config: c, - Db: db, RedisClient: redis.NewClient(&redis.Options{ Addr: c.Redis.Host, Password: c.Redis.Pass, }), - SqlConn: sqlx.NewMysql(c.DB.DataSource), - TaskModel: model.NewTaskModel(sqlConn, c.Cache), - CenterOverviewModel: model.NewCenterOverviewModel(sqlConn, c.Cache), ScheduleHpcClient: kq.NewPusher(c.KqProducerConf.Brokers, c.KqProducerConf.HpcTopic), ScheduleCloudClient: kq.NewPusher(c.KqProducerConf.Brokers, c.KqProducerConf.CloudTopic), ScheduleAiClient: kq.NewPusher(c.KqProducerConf.Brokers, c.KqProducerConf.AiTopic), diff --git a/adaptor/PCM-CORE/api/pcm.go b/adaptor/PCM-CORE/api/pcm.go index 52221b95..15627376 100644 --- a/adaptor/PCM-CORE/api/pcm.go +++ b/adaptor/PCM-CORE/api/pcm.go @@ -5,58 +5,19 @@ import ( "PCM/adaptor/PCM-CORE/api/internal/handler" kqMq "PCM/adaptor/PCM-CORE/api/internal/mqs/kq" "PCM/adaptor/PCM-CORE/api/internal/svc" - "PCM/adaptor/PCM-CORE/model" commonConfig "PCM/common/config" "context" "flag" - "github.com/go-redis/redis/v8" "github.com/zeromicro/go-queue/kq" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/service" - "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/rest" ) var configFile = flag.String("f", "adaptor/PCM-CORE/api/etc/pcm.yaml", "the config file") func main() { - //flag.Parse() - // - //var c config.Config - //conf.MustLoad(*configFile, &c) - // - //serviceGroup := service.NewServiceGroup() - //defer serviceGroup.Stop() - // - //server := rest.MustNewServer(c.RestConf) - // - //ctx := svc.NewServiceContext(c) - //// start log component - //logx.MustSetup(c.LogConf) - //ctx.Cron.Start() - //handler.RegisterHandlers(server, ctx) - // - //serviceGroup.Add(server) - //services := []service.Service{ - // //Listening for changes in consumption flow status - // kq.MustNewQueue(c.HpcConsumerConf, kqMq.NewScheduleHpcMq(context.Background(), ctx)), - // kq.MustNewQueue(c.CloudConsumerConf, kqMq.NewScheduleCloudMq(context.Background(), ctx)), - // kq.MustNewQueue(c.AiConsumerConf, kqMq.NewScheduleAiMq(context.Background(), ctx)), - // - // //..... - //} - //for _, mq := range services { - // serviceGroup.Add(mq) - //} - // - //// 初始化数据到缓存 - //initRedisData(ctx.SqlConn, ctx.RedisClient) - //logx.Infof("Starting server at %s:%d...\n", c.Host, c.Port) - //serviceGroup.Start() - - //---------------------- - flag.Parse() var bootstrapConfig commonConfig.BootstrapConfig @@ -104,23 +65,7 @@ func main() { serviceGroup.Add(mq) } - // 初始化数据到缓存 - initRedisData(ctx.SqlConn, ctx.RedisClient) logx.Infof("Starting server at %s:%d...\n", c.Host, c.Port) serviceGroup.Start() } - -func initRedisData(sql sqlx.SqlConn, redisClient *redis.Client) { - // 查询出字典数据列表 - var dictList []model.Dict - err := sql.QueryRows(&dictList, "select * from dict") - if err != nil { - return - } - - for _, dict := range dictList { - redisClient.Set(context.Background(), dict.DictValue, dict.DictCode, 0) - } - -} diff --git a/adaptor/PCM-CORE/model/aimodel_gen.go b/adaptor/PCM-CORE/model/aimodel_gen.go index b42e3516..9da02e6a 100644 --- a/adaptor/PCM-CORE/model/aimodel_gen.go +++ b/adaptor/PCM-CORE/model/aimodel_gen.go @@ -7,7 +7,6 @@ import ( "database/sql" "fmt" "strings" - "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/sqlc" @@ -37,22 +36,22 @@ type ( } Ai struct { - Id int64 `db:"id"` // id - TaskId int64 `db:"task_id"` // 任务id - ProjectId string `db:"project_id"` // 项目id - Name string `db:"name"` // 名称 - Status string `db:"status"` // 状态 - StartTime string `db:"start_time"` // 开始时间 - RunningTime string `db:"running_time"` // 运行时间 - CreatedBy string `db:"created_by"` // 创建人 - CreatedTime time.Time `db:"created_time"` // 创建时间 - UpdatedBy string `db:"updated_by"` // 更新人 - UpdatedTime time.Time `db:"updated_time"` // 更新时间 - DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) - ServiceName string `db:"service_name"` - Result sql.NullString `db:"result"` - YamlString string `db:"yaml_string"` - JobId string `db:"job_id"` + Id int64 `db:"id"` // id + TaskId int64 `db:"task_id"` // 任务id + ProjectId string `db:"project_id"` // 项目id + Name string `db:"name"` // 名称 + Status string `db:"status"` // 状态 + StartTime string `db:"start_time"` // 开始时间 + RunningTime string `db:"running_time"` // 运行时间 + CreatedBy string `db:"created_by"` // 创建人 + CreatedTime sql.NullTime `db:"created_time"` // 创建时间 + UpdatedBy string `db:"updated_by"` // 更新人 + UpdatedTime sql.NullTime `db:"updated_time"` // 更新时间 + DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) + ServiceName string `db:"service_name"` + Result string `db:"result"` + YamlString string `db:"yaml_string"` + JobId string `db:"job_id"` } ) diff --git a/adaptor/PCM-CORE/model/cloudmodel_gen.go b/adaptor/PCM-CORE/model/cloudmodel_gen.go index 0a665f91..2d6b5e00 100644 --- a/adaptor/PCM-CORE/model/cloudmodel_gen.go +++ b/adaptor/PCM-CORE/model/cloudmodel_gen.go @@ -7,7 +7,6 @@ import ( "database/sql" "fmt" "strings" - "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/sqlc" @@ -37,23 +36,23 @@ type ( } Cloud struct { - Id int64 `db:"id"` // id - TaskId int64 `db:"task_id"` // 任务id - ApiVersion string `db:"api_version"` - Name string `db:"name"` // 名称 - Namespace string `db:"namespace"` // 命名空间 - Kind string `db:"kind"` // 种类 - Status string `db:"status"` // 状态 - StartTime string `db:"start_time"` // 开始时间 - RunningTime int64 `db:"running_time"` // 运行时长 - CreatedBy int64 `db:"created_by"` // 创建人 - CreatedTime time.Time `db:"created_time"` // 创建时间 - UpdatedBy int64 `db:"updated_by"` // 更新人 - UpdatedTime time.Time `db:"updated_time"` // 更新时间 - DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) - ServiceName string `db:"service_name"` - YamlString string `db:"yaml_string"` - Result sql.NullString `db:"result"` + Id int64 `db:"id"` // id + TaskId int64 `db:"task_id"` // 任务id + ApiVersion string `db:"api_version"` + Name string `db:"name"` // 名称 + Namespace string `db:"namespace"` // 命名空间 + Kind string `db:"kind"` // 种类 + Status string `db:"status"` // 状态 + StartTime string `db:"start_time"` // 开始时间 + RunningTime int64 `db:"running_time"` // 运行时长 + CreatedBy int64 `db:"created_by"` // 创建人 + CreatedTime sql.NullTime `db:"created_time"` // 创建时间 + UpdatedBy int64 `db:"updated_by"` // 更新人 + UpdatedTime sql.NullTime `db:"updated_time"` // 更新时间 + DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) + ServiceName string `db:"service_name"` + YamlString string `db:"yaml_string"` + Result string `db:"result"` } ) diff --git a/adaptor/PCM-CORE/model/hpcmodel_gen.go b/adaptor/PCM-CORE/model/hpcmodel_gen.go index 31b12ba9..68babe43 100644 --- a/adaptor/PCM-CORE/model/hpcmodel_gen.go +++ b/adaptor/PCM-CORE/model/hpcmodel_gen.go @@ -7,7 +7,6 @@ import ( "database/sql" "fmt" "strings" - "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/sqlc" @@ -37,24 +36,24 @@ type ( } Hpc struct { - Id int64 `db:"id"` // id - TaskId sql.NullInt64 `db:"task_id"` // 任务id - JobId sql.NullString `db:"job_id"` // 作业id - ServiceName sql.NullString `db:"service_name"` // 服务名称 - Name sql.NullString `db:"name"` // 名称 - Status sql.NullString `db:"status"` // 状态 - StartTime sql.NullString `db:"start_time"` // 开始时间 - RunningTime sql.NullInt64 `db:"running_time"` // 运行时间 - CardCount sql.NullInt64 `db:"card_count"` // 卡数 - CreatedBy sql.NullInt64 `db:"created_by"` // 创建人 - CreatedTime time.Time `db:"created_time"` // 创建时间 - UpdatedBy sql.NullInt64 `db:"updated_by"` // 更新人 - UpdatedTime time.Time `db:"updated_time"` // 更新时间 - DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) - WorkDir sql.NullString `db:"work_dir"` - WallTime sql.NullString `db:"wall_time"` - Result sql.NullString `db:"result"` - YamlString sql.NullString `db:"yaml_string"` + Id int64 `db:"id"` // id + TaskId int64 `db:"task_id"` // 任务id + JobId string `db:"job_id"` // 作业id + ServiceName string `db:"service_name"` // 服务名称 + Name string `db:"name"` // 名称 + Status string `db:"status"` // 状态 + StartTime string `db:"start_time"` // 开始时间 + RunningTime int64 `db:"running_time"` // 运行时间 + CardCount int64 `db:"card_count"` // 卡数 + CreatedBy int64 `db:"created_by"` // 创建人 + CreatedTime sql.NullTime `db:"created_time"` // 创建时间 + UpdatedBy int64 `db:"updated_by"` // 更新人 + UpdatedTime sql.NullTime `db:"updated_time"` // 更新时间 + DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) + WorkDir string `db:"work_dir"` + WallTime string `db:"wall_time"` + Result string `db:"result"` + YamlString string `db:"yaml_string"` } ) diff --git a/adaptor/PCM-CORE/rpc/internal/logic/infolistlogic.go b/adaptor/PCM-CORE/rpc/internal/logic/infolistlogic.go index 5f50545c..b5062518 100644 --- a/adaptor/PCM-CORE/rpc/internal/logic/infolistlogic.go +++ b/adaptor/PCM-CORE/rpc/internal/logic/infolistlogic.go @@ -4,6 +4,7 @@ import ( "PCM/adaptor/PCM-CORE/model" "PCM/adaptor/PCM-CORE/rpc/internal/svc" "PCM/adaptor/PCM-CORE/rpc/pcmCore" + "PCM/common/tool" "context" "github.com/zeromicro/go-zero/core/logx" @@ -29,59 +30,32 @@ func (l *InfoListLogic) InfoList(in *pcmCore.InfoListReq) (*pcmCore.InfoListResp // 查询云智超中的数据列表 switch in.Kind { case "hpc": - rows, err := l.svcCtx.Db.Query("select task_id,name,status,work_dir,wall_time from hpc where service_name = ? and status not in ('Succeed', 'Completed')", in.ServiceName) - if err != nil { - return nil, err + var hpcModelList []model.Hpc + tx := l.svcCtx.DbEngin.Where("service_name = ? AND status NOT IN ?", in.ServiceName, []string{"Succeed", "Completed"}).Find(&hpcModelList) + if tx.Error != nil { + return nil, tx.Error } - for rows.Next() { - var hpc model.Hpc - rows.Scan(&hpc.TaskId, &hpc.Name, &hpc.Status, &hpc.WorkDir, &hpc.WallTime) - hpcInfo := pcmCore.HpcInfo{ - TaskId: hpc.TaskId.Int64, - Name: hpc.Name.String, - Status: hpc.Status.String, - WorkDir: hpc.WorkDir.String, - WallTime: hpc.WallTime.String, - } - result.HpcInfoList = append(result.HpcInfoList, &hpcInfo) - } - + var hpcInfoList []*pcmCore.HpcInfo + tool.Convert(hpcModelList, &hpcInfoList) + result.HpcInfoList = hpcInfoList case "cloud": - rows, err := l.svcCtx.Db.Query("select task_id,namespace,name,status,yaml_string from cloud where service_name = ? and status not in ('Succeed', 'Completed')", in.ServiceName) - if err != nil { - return nil, err - } - for rows.Next() { - var cloud model.Cloud - rows.Scan(&cloud.TaskId, &cloud.Namespace, &cloud.Name, &cloud.Status, &cloud.YamlString) - var cloudInfo pcmCore.CloudInfo - cloudInfo = pcmCore.CloudInfo{ - TaskId: cloud.TaskId, - Namespace: cloud.Namespace, - Name: cloud.Name, - Status: cloud.Status, - YamlString: cloud.YamlString, - } - result.CloudInfoList = append(result.CloudInfoList, &cloudInfo) + var cloudModelList []model.Cloud + tx := l.svcCtx.DbEngin.Where("service_name = ? AND status NOT IN ?", in.ServiceName, []string{"Succeed", "Completed"}).Find(&cloudModelList) + if tx.Error != nil { + return nil, tx.Error } + var cloudInfoList []*pcmCore.CloudInfo + tool.Convert(cloudModelList, &cloudInfoList) + result.CloudInfoList = cloudInfoList case "ai": - rows, err := l.svcCtx.Db.Query("select task_id,name,status,project_id,job_id from ai where service_name = ? and status not in ('Succeed', 'Completed')", in.ServiceName) - if err != nil { - return nil, err - } - for rows.Next() { - var ai model.Ai - rows.Scan(&ai.TaskId, &ai.Name, &ai.Status, &ai.ProjectId, &ai.JobId) - var aiInfo pcmCore.AiInfo - aiInfo = pcmCore.AiInfo{ - TaskId: ai.TaskId, - ProjectId: ai.ProjectId, - Name: ai.Name, - Status: ai.Status, - JobId: ai.JobId, - } - result.AiInfoList = append(result.AiInfoList, &aiInfo) + var aiModelList []model.AiModel + tx := l.svcCtx.DbEngin.Where("service_name = ? AND status NOT IN ?", in.ServiceName, []string{"Succeed", "Completed"}).Find(&aiModelList) + if tx.Error != nil { + return nil, tx.Error } + var aiInfoList []*pcmCore.AiInfo + tool.Convert(aiModelList, &aiInfoList) + result.AiInfoList = aiInfoList } return &result, nil } diff --git a/adaptor/PCM-CORE/rpc/internal/logic/syncinfologic.go b/adaptor/PCM-CORE/rpc/internal/logic/syncinfologic.go index 573755fa..58e2a4b4 100644 --- a/adaptor/PCM-CORE/rpc/internal/logic/syncinfologic.go +++ b/adaptor/PCM-CORE/rpc/internal/logic/syncinfologic.go @@ -33,24 +33,24 @@ func NewSyncInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncInfo // SyncInfo Synchronous data information func (l *SyncInfoLogic) SyncInfo(in *pcmCore.SyncInfoReq) (*pcmCore.SyncInfoResp, error) { - conn, err := l.svcCtx.Db.Begin() - if err != nil { - return nil, err + db := l.svcCtx.DbEngin.Begin() + if db.Error != nil { + return nil, db.Error } switch in.Kind { case "cloud": for _, cloudInfo := range in.CloudInfoList { - _, err = conn.Exec("update cloud set status = ?,start_time = ?,running_time = ? where service_name = ? and task_id = ? and namespace = ? and name = ?", + db.Exec("update cloud set status = ?,start_time = ?,running_time = ? where service_name = ? and task_id = ? and namespace = ? and name = ?", cloudInfo.Status, cloudInfo.StartTime, cloudInfo.RunningTime, in.ServiceName, cloudInfo.TaskId, cloudInfo.Namespace, cloudInfo.Name) } case "hpc": for _, hpcInfo := range in.HpcInfoList { - _, err = conn.Exec("update hpc set status = ?,start_time = ?,running_time = ?,job_id = ? where service_name = ? and task_id = ? and name = ?", + db.Exec("update hpc set status = ?,start_time = ?,running_time = ?,job_id = ? where service_name = ? and task_id = ? and name = ?", hpcInfo.Status, hpcInfo.StartTime, hpcInfo.RunningTime, hpcInfo.JobId, in.ServiceName, hpcInfo.TaskId, hpcInfo.Name) } case "ai": for _, aiInfo := range in.AiInfoList { - _, err = conn.Exec("update ai set status = ?,start_time = ?,running_time = ?,project_id = ?,job_id = ?,created_time = ? where service_name = ? and task_id = ? and name = ?", + db.Exec("update ai set status = ?,start_time = ?,running_time = ?,project_id = ?,job_id = ?,created_time = ? where service_name = ? and task_id = ? and name = ?", aiInfo.Status, aiInfo.StartTime, aiInfo.RunningTime, aiInfo.ProjectId, aiInfo.JobId, aiInfo.CreateTime, in.ServiceName, aiInfo.TaskId, aiInfo.Name) } } @@ -58,13 +58,13 @@ func (l *SyncInfoLogic) SyncInfo(in *pcmCore.SyncInfoReq) (*pcmCore.SyncInfoResp // 执行回滚或者提交操作 defer func() { if p := recover(); p != nil { - conn.Rollback() + db.Rollback() logx.Error(p) - } else if err != nil { + } else if db.Error != nil { logx.Info("rollback") - conn.Rollback() + db.Rollback() } else { - err = conn.Commit() + db = db.Commit() logx.Info("commit success") } }() diff --git a/adaptor/PCM-CORE/rpc/internal/svc/servicecontext.go b/adaptor/PCM-CORE/rpc/internal/svc/servicecontext.go index 594985bb..111a9dd7 100644 --- a/adaptor/PCM-CORE/rpc/internal/svc/servicecontext.go +++ b/adaptor/PCM-CORE/rpc/internal/svc/servicecontext.go @@ -3,18 +3,27 @@ package svc import ( "PCM/adaptor/PCM-CORE/rpc/internal/config" _ "github.com/go-sql-driver/mysql" - "github.com/jmoiron/sqlx" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/logger" + "gorm.io/gorm/schema" ) type ServiceContext struct { - Config config.Config - Db *sqlx.DB + Config config.Config + DbEngin *gorm.DB } func NewServiceContext(c config.Config) *ServiceContext { - db, _ := sqlx.Open("mysql", c.DB.DataSource) + //启动Gorm支持 + dbEngin, _ := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{ + NamingStrategy: schema.NamingStrategy{ + SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user` + }, + Logger: logger.Default.LogMode(logger.Info), + }) return &ServiceContext{ - Config: c, - Db: db, + Config: c, + DbEngin: dbEngin, } } diff --git a/adaptor/PCM-CORE/rpc/pcmcore.go b/adaptor/PCM-CORE/rpc/pcmcore.go index 9682c466..8ccd688e 100644 --- a/adaptor/PCM-CORE/rpc/pcmcore.go +++ b/adaptor/PCM-CORE/rpc/pcmcore.go @@ -19,29 +19,6 @@ import ( var configFile = flag.String("f", "adaptor/PCM-CORE/rpc/etc/pcmcore.yaml", "the config file") func main() { - //flag.Parse() - // - //var c config.Config - //conf.MustLoad(*configFile, &c) - //ctx := svc.NewServiceContext(c) - // - //s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { - // pcmCore.RegisterPcmCoreServer(grpcServer, server.NewPcmCoreServer(ctx)) - // - // if c.Mode == service.DevMode || c.Mode == service.TestMode { - // reflection.Register(grpcServer) - // } - //}) - // - ////rpc log - //s.AddUnaryInterceptors(rpcserver.LoggerInterceptor) - // - //defer s.Stop() - // - //fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) - //s.Start() - - //------- flag.Parse() diff --git a/go.mod b/go.mod index 3d8f51b2..494cf9b0 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module PCM -go 1.19 +go 1.18 require ( github.com/JCCE-nudt/zero-contrib/zrpc/registry/nacos v0.0.0-20230419021610-13bbc83fbc3c @@ -10,7 +10,6 @@ require ( github.com/go-resty/resty/v2 v2.7.0 github.com/go-sql-driver/mysql v1.7.0 github.com/jinzhu/copier v0.3.5 - github.com/jmoiron/sqlx v1.3.5 github.com/nacos-group/nacos-sdk-go/v2 v2.2.1 github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 diff --git a/go.sum b/go.sum index eaaac83b..ebe88219 100644 --- a/go.sum +++ b/go.sum @@ -562,7 +562,6 @@ github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -744,8 +743,6 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= -github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= -github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= @@ -784,8 +781,6 @@ github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtB github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk= github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA= github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA= @@ -805,8 +800,6 @@ github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= -github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= -github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=