diff --git a/api/desc/monitoring/pcm-monitoring.api b/api/desc/monitoring/pcm-monitoring.api index c5ff3f5a..45c9b585 100644 --- a/api/desc/monitoring/pcm-monitoring.api +++ b/api/desc/monitoring/pcm-monitoring.api @@ -37,4 +37,8 @@ type ( data interface{} `json:"data"` msg string `json:"msg"` } -) \ No newline at end of file +) + +type alertListResp { + alertMap map[string]interface{} `json:"alertMap"` +} \ No newline at end of file diff --git a/api/desc/pcm.api b/api/desc/pcm.api index ee7ffd74..0c9725a7 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -961,4 +961,8 @@ service pcm { @doc "node resource load" @handler nodesLoadTopHandler get /monitoring/node/top (nodesLoadTopReq) returns (nodesLoadTopResp) + + @doc "alert list" + @handler alertListHandler + get /monitoring/alert/list returns (alertListResp) } \ No newline at end of file diff --git a/api/internal/handler/monitoring/alertlisthandler.go b/api/internal/handler/monitoring/alertlisthandler.go new file mode 100644 index 00000000..53c82a9b --- /dev/null +++ b/api/internal/handler/monitoring/alertlisthandler.go @@ -0,0 +1,17 @@ +package monitoring + +import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" + + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/monitoring" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" +) + +func AlertListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + l := monitoring.NewAlertListLogic(r.Context(), svcCtx) + resp, err := l.AlertList() + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/monitoring/clustersloadhandler.go b/api/internal/handler/monitoring/clustersloadhandler.go index e0fba0d0..6758c46a 100644 --- a/api/internal/handler/monitoring/clustersloadhandler.go +++ b/api/internal/handler/monitoring/clustersloadhandler.go @@ -1,6 +1,7 @@ package monitoring import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -19,10 +20,6 @@ func ClustersLoadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := monitoring.NewClustersLoadLogic(r.Context(), svcCtx) resp, err := l.ClustersLoad(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index c4d6163d..3acfb456 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -1207,6 +1207,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/monitoring/node/top", Handler: monitoring.NodesLoadTopHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/monitoring/alert/list", + Handler: monitoring.AlertListHandler(serverCtx), + }, }, rest.WithPrefix("/pcm/v1"), ) diff --git a/api/internal/logic/monitoring/alertlistlogic.go b/api/internal/logic/monitoring/alertlistlogic.go new file mode 100644 index 00000000..81c168e2 --- /dev/null +++ b/api/internal/logic/monitoring/alertlistlogic.go @@ -0,0 +1,63 @@ +package monitoring + +import ( + "context" + "github.com/pkg/errors" + v1 "github.com/prometheus/client_golang/api/prometheus/v1" + tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" + + "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 AlertListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAlertListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AlertListLogic { + return &AlertListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +type AlertListResp struct { + Mode int `json:"code"` + Msg string `json:"msg"` + Data map[string][]*v1.Alert `json:"data"` +} + +func (l *AlertListLogic) AlertList() (resp *types.AlertListResp, err error) { + // todo: add your logic here and delete this line + resp = &types.AlertListResp{} + + // query server http url. + var serverArray []string + l.svcCtx.DbEngin.Raw("select ta.server from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and label = 'kubernetes'").Scan(&serverArray) + + result := make(map[string][]*v1.Alert) + for _, server := range serverArray { + alertListResp := AlertListResp{} + response, err := l.svcCtx.HttpClient.R(). + SetResult(&alertListResp). + ForceContentType("application/json"). + Get(server + "/api/v1/alert/rule/list") + if err != nil { + logx.Error(response) + return nil, err + } + if response.IsError() { + return nil, errors.New(response.String()) + } + for k, v := range alertListResp.Data { + result[k] = v + } + } + tool.Convert(result, &resp.AlertMap) + return resp, nil +} diff --git a/api/internal/logic/monitoring/createalertrulelogic.go b/api/internal/logic/monitoring/createalertrulelogic.go index 828f5635..fadf50c3 100644 --- a/api/internal/logic/monitoring/createalertrulelogic.go +++ b/api/internal/logic/monitoring/createalertrulelogic.go @@ -59,6 +59,7 @@ func (l *CreateAlertRuleLogic) CreateAlertRule(req *types.CreateAlertRuleReq) er ForceContentType("application/json"). Get(server + "/api/v1/monitoring/rule/selector") if err != nil || response.IsError() { + logx.Error(response) return err } // Data Filling diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 274ccf66..ba19e5ea 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -5468,3 +5468,7 @@ type NodesLoadTopResp struct { Data interface{} `json:"data"` Msg string `json:"msg"` } + +type AlertListResp struct { + AlertMap map[string]interface{} `json:"alertMap"` +}