diff --git a/api/desc/monitoring/pcm-monitoring.api b/api/desc/monitoring/pcm-monitoring.api index 06f84557..cdd39fdc 100644 --- a/api/desc/monitoring/pcm-monitoring.api +++ b/api/desc/monitoring/pcm-monitoring.api @@ -58,4 +58,16 @@ type ( type SyncClusterAlertReq { AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"` -} \ No newline at end of file +} + +type ( + taskNumReq { + clusterId string `form:"clusterId"` + } + taskNumResp { + current int `json:"current"` + today int `json:"today"` + history int `json:"history"` + failed int `json:"failed"` + } +) \ No newline at end of file diff --git a/api/desc/pcm.api b/api/desc/pcm.api index 2c76692c..51e5502c 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -132,11 +132,11 @@ service pcm { @doc "paging queries the task list" @handler pageListTaskHandler - get /core/task/list (pageTaskReq) returns(PageResult) + get /core/task/list (pageTaskReq) returns (PageResult) @doc "Statistical task status" @handler countTaskStatus - get /core/task/countTaskStatus () returns(TaskStatusResp) + get /core/task/countTaskStatus () returns (TaskStatusResp) } //hpc二级接口 @@ -210,7 +210,7 @@ service pcm { @doc "Create cloud computing common tasks" @handler commitGeneralTask - post /cloud/task/create (GeneralTaskReq) returns() + post /cloud/task/create (GeneralTaskReq) returns () } //智算二级接口 @@ -975,7 +975,7 @@ service pcm { @doc "alert rules" @handler alertRulesHandler - get /monitoring/alert/rule (AlertRulesReq)returns (AlertRulesResp) + get /monitoring/alert/rule (AlertRulesReq) returns (AlertRulesResp) @doc "cluster resource load" @handler clustersLoadHandler @@ -991,5 +991,8 @@ service pcm { @doc "Synchronize Cluster alert Information" @handler syncClusterAlertHandler - post /core/syncClusterAlert (SyncClusterAlertReq) + post /monitoring/syncClusterAlert (SyncClusterAlertReq) + + @handler taskNumHandler + get /monitoring/task/num (taskNumReq) returns (taskNumResp) } \ No newline at end of file diff --git a/api/internal/handler/monitoring/tasknumhandler.go b/api/internal/handler/monitoring/tasknumhandler.go new file mode 100644 index 00000000..4b7751bc --- /dev/null +++ b/api/internal/handler/monitoring/tasknumhandler.go @@ -0,0 +1,25 @@ +package monitoring + +import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/monitoring" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" +) + +func TaskNumHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.TaskNumReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := monitoring.NewTaskNumLogic(r.Context(), svcCtx) + resp, err := l.TaskNum(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index a68efc58..9fe292b5 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -1244,9 +1244,14 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodPost, - Path: "/core/syncClusterAlert", + Path: "/monitoring/syncClusterAlert", Handler: monitoring.SyncClusterAlertHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/monitoring/task/num", + Handler: monitoring.TaskNumHandler(serverCtx), + }, }, rest.WithPrefix("/pcm/v1"), ) diff --git a/api/internal/logic/monitoring/alertruleslogic.go b/api/internal/logic/monitoring/alertruleslogic.go index d7d2dde8..7c63dcff 100644 --- a/api/internal/logic/monitoring/alertruleslogic.go +++ b/api/internal/logic/monitoring/alertruleslogic.go @@ -29,7 +29,7 @@ func (l *AlertRulesLogic) AlertRules(req *types.AlertRulesReq) (resp *types.Aler var alertRules []types.AlertRule sql := fmt.Sprintf("SELECT ar.*,GROUP_CONCAT(tc.`name` ORDER BY tc.`name` ASC SEPARATOR ',') as cluster_name FROM alert_rule ar JOIN t_cluster tc ON ar.cluster_id = tc.id WHERE ar.alert_type = %s AND ar.deleted_at IS NULL AND tc.deleted_at IS NULL GROUP BY ar.id", req.AlertType) if req.AdapterId != "" { - sql = fmt.Sprintf("SELECT tar.*,GROUP_CONCAT( tc.`name` ORDER BY tc.`name` ASC SEPARATOR ',' ) AS cluster_name FROM talert_rule ar JOIN t_cluster tc ON ar.cluster_id = tc.id JOIN t_adapter ta ON ta.id = tc.adapter_id WHERE tar.alert_type = %s AND ta.id = %s AND ar.deleted_at IS NULL AND tc.deleted_at IS NULL GROUP BY tar.id", req.AlertType, req.AdapterId) + sql = fmt.Sprintf("SELECT ar.*,GROUP_CONCAT( tc.`name` ORDER BY tc.`name` ASC SEPARATOR ',' ) AS cluster_name FROM alert_rule ar JOIN t_cluster tc ON ar.cluster_id = tc.id JOIN t_adapter ta ON ta.id = tc.adapter_id WHERE ar.alert_type = %s AND ta.id = %s AND ar.deleted_at IS NULL AND tc.deleted_at IS NULL GROUP BY ar.id", req.AlertType, req.AdapterId) } if req.ClusterId != "" { sql = fmt.Sprintf("SELECT ar.*,GROUP_CONCAT(tc.`name` ORDER BY tc.`name` ASC SEPARATOR ',') as cluster_name FROM alert_rule ar JOIN t_cluster tc ON ar.cluster_id = tc.id WHERE ar.alert_type = %s AND ar.cluster_id = %s AND ar.deleted_at IS NULL AND tc.deleted_at IS NULL GROUP BY ar.id", req.AlertType, req.ClusterId) diff --git a/api/internal/logic/monitoring/createalertrulelogic.go b/api/internal/logic/monitoring/createalertrulelogic.go index fadf50c3..c9685c98 100644 --- a/api/internal/logic/monitoring/createalertrulelogic.go +++ b/api/internal/logic/monitoring/createalertrulelogic.go @@ -11,6 +11,7 @@ import ( v12 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/json" + "strconv" ) type CreateAlertRuleLogic struct { @@ -38,6 +39,7 @@ func (l *CreateAlertRuleLogic) CreateAlertRule(req *types.CreateAlertRuleReq) er // save to db var alertRule models.AlertRule tool.Convert(req, &alertRule) + alertRule.ClusterId, _ = strconv.ParseInt(req.CLusterId, 10, 64) alertRule.Id = tool.GenSnowflakeID() tx := l.svcCtx.DbEngin.Save(&alertRule) if tx.Error != nil { diff --git a/api/internal/logic/monitoring/tasknumlogic.go b/api/internal/logic/monitoring/tasknumlogic.go new file mode 100644 index 00000000..0b7f1659 --- /dev/null +++ b/api/internal/logic/monitoring/tasknumlogic.go @@ -0,0 +1,33 @@ +package monitoring + +import ( + "context" + + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type TaskNumLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewTaskNumLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TaskNumLogic { + return &TaskNumLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *TaskNumLogic) TaskNum(req *types.TaskNumReq) (resp *types.TaskNumResp, err error) { + resp = &types.TaskNumResp{} + l.svcCtx.DbEngin.Raw("SELECT COUNT(id) from task_cloud where cluster_id = ? and status = 'running'", req.ClusterId).Scan(resp.Current) + l.svcCtx.DbEngin.Raw("SELECT COUNT(id) from task_cloud where cluster_id = '' and DATE(start_time) = CURDATE()", req.ClusterId).Scan(resp.Today) + l.svcCtx.DbEngin.Raw("SELECT COUNT(id) from task_cloud where cluster_id = ?", req.ClusterId).Scan(resp.History) + l.svcCtx.DbEngin.Raw("SELECT COUNT(id) from task_cloud where cluster_id = ? and status = 'failed'", req.ClusterId).Scan(resp.Failed) + return resp, nil +} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 59dd185d..b4e691db 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -5548,6 +5548,8 @@ type CreateAlertRuleReq struct { type AlertRulesReq struct { AlertType string `form:"alertType"` + AdapterId string `form:"adapterId,optional"` + ClusterId string `form:"clusterId,optional"` } type AlertRulesResp struct { @@ -5589,3 +5591,14 @@ type AlertListResp struct { type SyncClusterAlertReq struct { AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"` } + +type TaskNumReq struct { + ClusterId string `form:"clusterId"` +} + +type TaskNumResp struct { + Current int `json:"current"` + Today int `json:"today"` + History int `json:"history"` + Failed int `json:"failed"` +}