diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index 2064e2db..977b0605 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -229,22 +229,40 @@ type ( } ) +// 任务列表参数 type ( + taskListReq { + PageNum int `form:"pageNum"` + PageSize int `form:"pageSize"` + } taskListResp { - TotalCount int `json:"totalCount"` - CardTime float32 `json:"cardTime"` - TotalRunTime float32 `json:"totalRunTime"` + TotalCount int64 `json:"totalCount"` // 任务总数 + NormalCount int64 `json:"normalCount"` // 正常任务数 + AlarmCount int64 `json:"alarmCount"` // 任务告警数 Tasks []Task `json:"tasks"` } Task { + Id int64 `json:"id"` Name string `json:"name"` Status string `json:"status"` - Strategy int `json:"strategy"` + TaskType string `json:"taskType"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + ParticipantStatus string `json:"participantStatus"` + ParticipantId int64 `json:"participantId"` ParticipantName string `json:"participantName"` - SynergyStatus string `json:"synergyStatus"` } ) +// 任务列表参数 +type ( + taskDetailReq { + TaskId int64 `path:"taskId"` + } + taskDetailResp { + + } +) type ( listCenterResp { Code int32 `json:"code"` diff --git a/api/desc/pcm.api b/api/desc/pcm.api index 8d455eb5..0cf9af1d 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -36,8 +36,13 @@ service pcm { @handler scheduleTaskHandler post /core/scheduleTask (scheduleTaskReq) + // 任务列表接口 @handler TaskListHandler - get /core/taskList returns (taskListResp) + get /core/taskList (taskListReq)returns (taskListResp) + + // 任务详情接口 + @handler TaskDetailHandler + get /core/taskDetail (taskDetailReq) returns (taskDetailResp) @handler JobTotalHandler get /core/jobTotal returns (jobTotalResp) diff --git a/api/internal/handler/core/scheduletaskbyyamlhandler.go b/api/internal/handler/core/scheduletaskbyyamlhandler.go new file mode 100644 index 00000000..46c8b32a --- /dev/null +++ b/api/internal/handler/core/scheduletaskbyyamlhandler.go @@ -0,0 +1,28 @@ +package core + +import ( + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/core" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" +) + +func ScheduleTaskByYamlHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.ScheduleTaskByYamlReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := core.NewScheduleTaskByYamlLogic(r.Context(), svcCtx) + resp, err := l.ScheduleTaskByYaml(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/api/internal/handler/core/taskdetailhandler.go b/api/internal/handler/core/taskdetailhandler.go new file mode 100644 index 00000000..e141272a --- /dev/null +++ b/api/internal/handler/core/taskdetailhandler.go @@ -0,0 +1,25 @@ +package core + +import ( + "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/repository/result" + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/core" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" +) + +func TaskDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.TaskDetailReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := core.NewTaskDetailLogic(r.Context(), svcCtx) + resp, err := l.TaskDetail(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/core/tasklisthandler.go b/api/internal/handler/core/tasklisthandler.go deleted file mode 100644 index cd192330..00000000 --- a/api/internal/handler/core/tasklisthandler.go +++ /dev/null @@ -1,17 +0,0 @@ -package core - -import ( - "gitlink.org.cn/jcce-pcm/utils/result" - "net/http" - - "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/core" - "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" -) - -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() - result.HttpResult(r, w, resp, err) - } -} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 9478ad87..39b427ca 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -45,6 +45,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/core/taskList", Handler: core.TaskListHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/core/taskDetail", + Handler: core.TaskDetailHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/core/jobTotal", diff --git a/api/internal/logic/core/scheduletaskbyyamllogic.go b/api/internal/logic/core/scheduletaskbyyamllogic.go new file mode 100644 index 00000000..9f5fd538 --- /dev/null +++ b/api/internal/logic/core/scheduletaskbyyamllogic.go @@ -0,0 +1,30 @@ +package core + +import ( + "context" + + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ScheduleTaskByYamlLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewScheduleTaskByYamlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ScheduleTaskByYamlLogic { + return &ScheduleTaskByYamlLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ScheduleTaskByYamlLogic) ScheduleTaskByYaml(req *types.ScheduleTaskByYamlReq) (resp *types.ScheduleTaskByYamlResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/api/internal/logic/core/scheduletasklogic.go b/api/internal/logic/core/scheduletasklogic.go index bb580e0f..5beb123e 100644 --- a/api/internal/logic/core/scheduletasklogic.go +++ b/api/internal/logic/core/scheduletasklogic.go @@ -43,7 +43,7 @@ func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (err error) Description: req.Description, Name: req.Name, YamlString: string(bytes), - CommitTime: time.Now(), + CommitTime: time.Now().String(), } // save the task in mysql and return id tx := l.svcCtx.DbEngin.Create(&taskModel) diff --git a/api/internal/logic/core/taskdetaillogic.go b/api/internal/logic/core/taskdetaillogic.go new file mode 100644 index 00000000..49fca25a --- /dev/null +++ b/api/internal/logic/core/taskdetaillogic.go @@ -0,0 +1,30 @@ +package core + +import ( + "context" + + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type TaskDetailLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewTaskDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TaskDetailLogic { + return &TaskDetailLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *TaskDetailLogic) TaskDetail(req *types.TaskDetailReq) (resp *types.TaskDetailResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/api/internal/logic/core/tasklistlogic.go b/api/internal/logic/core/tasklistlogic.go deleted file mode 100644 index 1eb9424c..00000000 --- a/api/internal/logic/core/tasklistlogic.go +++ /dev/null @@ -1,73 +0,0 @@ -package core - -import ( - "context" - "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" - "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" - models2 "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models" - "gitlink.org.cn/jcce-pcm/utils/enum" - - "github.com/zeromicro/go-zero/core/logx" -) - -type TaskListLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TaskListLogic { - return &TaskListLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -func (l *TaskListLogic) TaskList() (resp *types.TaskListResp, err error) { - resp = &types.TaskListResp{} - - // 查询任务数据 - var tasks []models2.Task - tx := l.svcCtx.DbEngin.Find(&tasks) - if tx.Error != nil { - logx.Error(err) - return nil, tx.Error - } - if len(tasks) == 0 { - return nil, nil - } - for _, task := range tasks { - var participantName string - tx := l.svcCtx.DbEngin.Raw("SELECT name from sc_participant_phy_info where id in (SELECT CONCAT_WS(',',GROUP_CONCAT(DISTINCT h.participant_id) ,GROUP_CONCAT(DISTINCT a.participant_id) ,GROUP_CONCAT(DISTINCT c.participant_id))as service_name from task t left join hpc h on t.id = h.task_id left join cloud c on t.id = c.task_id left join ai a on t.id = a.task_id where t.id = ?)", task.Id).Scan(&participantName) - if tx.Error != nil { - logx.Error(err) - return nil, tx.Error - } - // 承接方转义 - - resp.Tasks = append(resp.Tasks, types.Task{ - ParticipantName: participantName, - Name: task.Name, - Strategy: int(task.Strategy), - SynergyStatus: enum.SynergyStatus(task.SynergyStatus).String(), - Status: task.Status, - }) - - } - // 查询总运行时长 - 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 { - logx.Error(err) - return nil, tx.Error - } - // 运行卡时数 - tx = l.svcCtx.DbEngin.Model(&models2.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.TotalCount = len(tasks) - return resp, nil -} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index f61ed23c..a19c2bfe 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -207,19 +207,35 @@ type TrainJob struct { Strategy int `json:"strategy"` } +type TaskListReq struct { + PageNum int `form:"pageNum"` + PageSize int `form:"pageSize"` +} + type TaskListResp struct { - TotalCount int `json:"totalCount"` - CardTime float32 `json:"cardTime"` - TotalRunTime float32 `json:"totalRunTime"` - Tasks []Task `json:"tasks"` + TotalCount int64 `json:"totalCount"` // 任务总数 + NormalCount int64 `json:"normalCount"` // 正常任务数 + AlarmCount int64 `json:"alarmCount"` // 任务告警数 + Tasks []Task `json:"tasks"` } type Task struct { - Name string `json:"name"` - Status string `json:"status"` - Strategy int `json:"strategy"` - ParticipantName string `json:"participantName"` - SynergyStatus string `json:"synergyStatus"` + Id int64 `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + TaskType string `json:"taskType"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + ParticipantStatus string `json:"participantStatus"` + ParticipantId int64 `json:"participantId"` + ParticipantName string `json:"participantName"` +} + +type TaskDetailReq struct { + TaskId int64 `path:"taskId"` +} + +type TaskDetailResp struct { } type ListCenterResp struct { diff --git a/pkg/models/taskmodel_gen.go b/pkg/models/taskmodel_gen.go index bd79944a..ea5a41f5 100644 --- a/pkg/models/taskmodel_gen.go +++ b/pkg/models/taskmodel_gen.go @@ -7,7 +7,6 @@ import ( "database/sql" "gorm.io/gorm" "strings" - "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/sqlx" @@ -41,7 +40,8 @@ type ( Status string `db:"status"` // 作业状态 Strategy int64 `db:"strategy"` // 策略 SynergyStatus int64 `db:"synergy_status"` // 协同状态(0-未协同、1-已协同) - CommitTime time.Time `db:"commit_time"` // 提交时间 + CommitTime string `db:"commit_time"` // 提交时间 + StartTime string `db:"start_time"` // 开始时间 EndTime string `db:"end_time"` // 结束运行时间 RunningTime int64 `db:"running_time"` // 已运行时间(单位秒) YamlString string `db:"yaml_string"` diff --git a/pkg/tracker/interface.go b/pkg/tracker/interface.go new file mode 100644 index 00000000..21b5015d --- /dev/null +++ b/pkg/tracker/interface.go @@ -0,0 +1,30 @@ +package tracker + +/* +Copyright 2020 KubeSphere Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +type Interface interface { + //GetMetric(expr string, time time.Time) Metric + //GetMetricOverTime(expr string, start, end time.Time, step time.Duration) Metric + //GetNamedMetrics(metrics []string, time time.Time, opt QueryOption) []Metric + //GetNamedMetricsOverTime(metrics []string, start, end time.Time, step time.Duration, opt QueryOption) []Metric + //GetMetadata(namespace string) []Metadata + //GetMetricLabelSet(expr string, start, end time.Time) []map[string]string + // + //// meter + //GetNamedMeters(meters []string, time time.Time, opts []QueryOption) []Metric + //GetNamedMetersOverTime(metrics []string, start, end time.Time, step time.Duration, opts []QueryOption) []Metric +} diff --git a/pkg/tracker/tracker.go b/pkg/tracker/tracker.go new file mode 100644 index 00000000..8fdb0f6a --- /dev/null +++ b/pkg/tracker/tracker.go @@ -0,0 +1,19 @@ +package tracker + +import ( + "github.com/prometheus/client_golang/api" + v1 "github.com/prometheus/client_golang/api/prometheus/v1" +) + +type prometheus struct { + client v1.API +} + +func NewPrometheus(address string) (Interface, error) { + cfg := api.Config{ + Address: address, + } + + client, err := api.NewClient(cfg) + return prometheus{client: v1.NewAPI(client)}, err +}