query task num

Former-commit-id: 2e88ecb4d707b7d9f662146415a6999610fe5e53
This commit is contained in:
zhangwei 2024-04-25 10:09:57 +08:00
parent 9d1f98b478
commit 8fd635b4a7
8 changed files with 101 additions and 8 deletions

View File

@ -58,4 +58,16 @@ type (
type SyncClusterAlertReq {
AlertRecordsMap map[string]interface{} `json:"alertRecordsMap"`
}
}
type (
taskNumReq {
clusterId string `form:"clusterId"`
}
taskNumResp {
current int `json:"current"`
today int `json:"today"`
history int `json:"history"`
failed int `json:"failed"`
}
)

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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"),
)

View File

@ -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)

View File

@ -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 {

View File

@ -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
}

View File

@ -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"`
}