schedule situation

Former-commit-id: 460875a761b3fad8b384c4bf5167ed24381e9d45
This commit is contained in:
zhangwei 2024-04-30 16:29:16 +08:00
parent fdc7d53cd9
commit 27c0aa6fc4
6 changed files with 159 additions and 0 deletions

View File

@ -81,3 +81,28 @@ type (
version string `json:"version"` 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"`
}
)

View File

@ -1028,4 +1028,7 @@ service pcm {
@handler adapterInfoHandler @handler adapterInfoHandler
get /monitoring/adapter/info (adapterInfoReq) returns (adapterInfoResp) get /monitoring/adapter/info (adapterInfoReq) returns (adapterInfoResp)
@handler scheduleSituationHandler
get /monitoring/schedule/situation returns (scheduleSituationResp)
} }

View File

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

View File

@ -1297,6 +1297,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/monitoring/adapter/info", Path: "/monitoring/adapter/info",
Handler: monitoring.AdapterInfoHandler(serverCtx), Handler: monitoring.AdapterInfoHandler(serverCtx),
}, },
{
Method: http.MethodGet,
Path: "/monitoring/schedule/situation",
Handler: monitoring.ScheduleSituationHandler(serverCtx),
},
}, },
rest.WithPrefix("/pcm/v1"), rest.WithPrefix("/pcm/v1"),
) )

View File

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

View File

@ -5693,3 +5693,25 @@ type AdapterInfoResp struct {
Name string `json:"name"` Name string `json:"name"`
Version string `json:"version"` 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"`
}