From 27c0aa6fc4a8eaf760eb69545a5e44cdc7e61642 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Tue, 30 Apr 2024 16:29:16 +0800 Subject: [PATCH 1/3] schedule situation Former-commit-id: 460875a761b3fad8b384c4bf5167ed24381e9d45 --- api/desc/monitoring/pcm-monitoring.api | 25 ++++++ api/desc/pcm.api | 3 + .../monitoring/schedulesituationhandler.go | 21 +++++ api/internal/handler/routes.go | 5 ++ .../monitoring/schedulesituationlogic.go | 83 +++++++++++++++++++ api/internal/types/types.go | 22 +++++ 6 files changed, 159 insertions(+) create mode 100644 api/internal/handler/monitoring/schedulesituationhandler.go create mode 100644 api/internal/logic/monitoring/schedulesituationlogic.go diff --git a/api/desc/monitoring/pcm-monitoring.api b/api/desc/monitoring/pcm-monitoring.api index 8dc7cdd9..360eafde 100644 --- a/api/desc/monitoring/pcm-monitoring.api +++ b/api/desc/monitoring/pcm-monitoring.api @@ -80,4 +80,29 @@ type ( name string `json:"name"` version string `json:"version"` } +) + +type ( +scheduleSituationResp{ + nodes []NodeRegion `json:"nodes"` + links []Link `json:"links"` + categories []Category `json:"categories"` +} + +NodeRegion{ + id int64 `json:"id"` + name string `json:"name"` + category string `json:"category"` + value int `json:"value"` +} + +Link{ + source string `json:"source"` + target string `json:"target"` +} + +Category{ + name string `json:"name"` +} + ) \ No newline at end of file diff --git a/api/desc/pcm.api b/api/desc/pcm.api index a8d3fb32..ba3c525d 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -1028,4 +1028,7 @@ service pcm { @handler adapterInfoHandler get /monitoring/adapter/info (adapterInfoReq) returns (adapterInfoResp) + + @handler scheduleSituationHandler + get /monitoring/schedule/situation returns (scheduleSituationResp) } \ No newline at end of file diff --git a/api/internal/handler/monitoring/schedulesituationhandler.go b/api/internal/handler/monitoring/schedulesituationhandler.go new file mode 100644 index 00000000..720c1944 --- /dev/null +++ b/api/internal/handler/monitoring/schedulesituationhandler.go @@ -0,0 +1,21 @@ +package monitoring + +import ( + "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" +) + +func ScheduleSituationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + l := monitoring.NewScheduleSituationLogic(r.Context(), svcCtx) + resp, err := l.ScheduleSituation() + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 73c617e3..f32fa672 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -1297,6 +1297,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/monitoring/adapter/info", Handler: monitoring.AdapterInfoHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/monitoring/schedule/situation", + Handler: monitoring.ScheduleSituationHandler(serverCtx), + }, }, rest.WithPrefix("/pcm/v1"), ) diff --git a/api/internal/logic/monitoring/schedulesituationlogic.go b/api/internal/logic/monitoring/schedulesituationlogic.go new file mode 100644 index 00000000..5d7ef556 --- /dev/null +++ b/api/internal/logic/monitoring/schedulesituationlogic.go @@ -0,0 +1,83 @@ +package monitoring + +import ( + "context" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + "strings" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ScheduleSituationLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewScheduleSituationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ScheduleSituationLogic { + return &ScheduleSituationLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ScheduleSituationLogic) ScheduleSituation() (resp *types.ScheduleSituationResp, err error) { + // todo: add your logic here and delete this line + resp = &types.ScheduleSituationResp{} + // node region + tx := l.svcCtx.DbEngin.Raw("SELECT c.id, c.name, tdi.id AS category, count(DISTINCT ta.id)+count(DISTINCT tc.id)+COUNT(DISTINCT th.id)+COUNT(tv.id) as value FROM t_cluster c LEFT JOIN t_dict_item tdi ON c.region_dict = tdi.id left JOIN task_ai ta ON ta.cluster_id = c.id left JOIN task_cloud tc ON tc.cluster_id = c.id left JOIN task_hpc th ON th.cluster_id = c.id left JOIN task_vm tv ON tv.cluster_id = c.id WHERE tc.deleted_at IS NULL GROUP BY c.id").Scan(&resp.Nodes) + if tx.Error != nil { + return nil, tx.Error + } + + // hpc + var hpcLinks []string + tx = l.svcCtx.DbEngin.Raw("SELECT GROUP_CONCAT(cluster_id SEPARATOR ',') as cluster_ids FROM task_hpc WHERE deleted_at IS NULL GROUP BY task_id HAVING COUNT(*) > 1;").Scan(&hpcLinks) + if tx.Error != nil { + return nil, tx.Error + } + LinksHandler(hpcLinks, resp) + // cloud + var cloudLinks []string + tx = l.svcCtx.DbEngin.Raw("SELECT GROUP_CONCAT(cluster_id SEPARATOR ',') as cluster_ids FROM task_cloud WHERE deleted_at IS NULL GROUP BY task_id HAVING COUNT(*) > 1;").Scan(&cloudLinks) + if tx.Error != nil { + return nil, tx.Error + } + LinksHandler(cloudLinks, resp) + // ai + var aiLinks []string + tx = l.svcCtx.DbEngin.Raw("SELECT GROUP_CONCAT(cluster_id SEPARATOR ',') as cluster_ids FROM task_ai WHERE deleted_at IS NULL GROUP BY task_id HAVING COUNT(*) > 1;").Scan(&aiLinks) + if tx.Error != nil { + return nil, tx.Error + } + LinksHandler(aiLinks, resp) + // vm + var vmLinks []string + tx = l.svcCtx.DbEngin.Raw("SELECT GROUP_CONCAT(cluster_id SEPARATOR ',') as cluster_ids FROM task_vm WHERE deleted_at IS NULL GROUP BY task_id HAVING COUNT(*) > 1;").Scan(&vmLinks) + if tx.Error != nil { + return nil, tx.Error + } + LinksHandler(vmLinks, resp) + + // categories + tx = l.svcCtx.DbEngin.Raw("select tdi.item_text as name from t_dict_item tdi,t_dict td where td.dict_code = 'cluster_region_dict' and tdi.dict_id = td.id").Scan(&resp.Categories) + if tx.Error != nil { + return nil, tx.Error + } + return resp, nil +} + +func LinksHandler(sources []string, resp *types.ScheduleSituationResp) { + for _, source := range sources { + links := strings.Split(source, ",") + + for i := 1; i < len(links); i++ { + if links[i] != links[i-1] { + resp.Links = append(resp.Links, types.Link{Source: links[i], Target: links[i-1]}) + } + } + } + +} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 48baec41..7e3ae940 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -5693,3 +5693,25 @@ type AdapterInfoResp struct { Name string `json:"name"` Version string `json:"version"` } + +type ScheduleSituationResp struct { + Nodes []NodeRegion `json:"nodes"` + Links []Link `json:"links"` + Categories []Category `json:"categories"` +} + +type NodeRegion struct { + Id int64 `json:"id"` + Name string `json:"name"` + Category string `json:"category"` + Value int `json:"value"` +} + +type Link struct { + Source string `json:"source"` + Target string `json:"target"` +} + +type Category struct { + Name string `json:"name"` +} From fb85474d7a58e237ee3c330efa9b93f02086bdf7 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Tue, 30 Apr 2024 16:35:26 +0800 Subject: [PATCH 2/3] schedule situation Former-commit-id: 82a4d7235fa176981899d8c43b35b7d96742eba6 --- .../handler/monitoring/schedulesituationhandler.go | 8 ++------ api/internal/logic/monitoring/schedulesituationlogic.go | 1 - 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/api/internal/handler/monitoring/schedulesituationhandler.go b/api/internal/handler/monitoring/schedulesituationhandler.go index 720c1944..4677a111 100644 --- a/api/internal/handler/monitoring/schedulesituationhandler.go +++ b/api/internal/handler/monitoring/schedulesituationhandler.go @@ -1,9 +1,9 @@ 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" ) @@ -12,10 +12,6 @@ func ScheduleSituationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { l := monitoring.NewScheduleSituationLogic(r.Context(), svcCtx) resp, err := l.ScheduleSituation() - 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/logic/monitoring/schedulesituationlogic.go b/api/internal/logic/monitoring/schedulesituationlogic.go index 5d7ef556..3dc1b64a 100644 --- a/api/internal/logic/monitoring/schedulesituationlogic.go +++ b/api/internal/logic/monitoring/schedulesituationlogic.go @@ -24,7 +24,6 @@ func NewScheduleSituationLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *ScheduleSituationLogic) ScheduleSituation() (resp *types.ScheduleSituationResp, err error) { - // todo: add your logic here and delete this line resp = &types.ScheduleSituationResp{} // node region tx := l.svcCtx.DbEngin.Raw("SELECT c.id, c.name, tdi.id AS category, count(DISTINCT ta.id)+count(DISTINCT tc.id)+COUNT(DISTINCT th.id)+COUNT(tv.id) as value FROM t_cluster c LEFT JOIN t_dict_item tdi ON c.region_dict = tdi.id left JOIN task_ai ta ON ta.cluster_id = c.id left JOIN task_cloud tc ON tc.cluster_id = c.id left JOIN task_hpc th ON th.cluster_id = c.id left JOIN task_vm tv ON tv.cluster_id = c.id WHERE tc.deleted_at IS NULL GROUP BY c.id").Scan(&resp.Nodes) From 8078d94e35d774c6c9fa0bd8d34ee5ff590a0006 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Tue, 30 Apr 2024 17:28:29 +0800 Subject: [PATCH 3/3] schedule situation Former-commit-id: ce8a1290894d1e17f1e46c254c53c2e383d232dd --- api/desc/pcm.api | 2 +- api/internal/handler/routes.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/desc/pcm.api b/api/desc/pcm.api index ba3c525d..a124833c 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -1021,7 +1021,7 @@ 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) diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index f32fa672..f72ac4f9 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -1284,7 +1284,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodPost, - Path: "/core/syncClusterAlert", + Path: "/monitoring/syncClusterAlert", Handler: monitoring.SyncClusterAlertHandler(serverCtx), }, {