移除多余代码

Former-commit-id: 94004d05b18036a47773a3d4906aceb86e61e142
This commit is contained in:
zhangwei 2024-06-11 09:03:53 +08:00
parent 68e497760f
commit 8ba618bfdd
6 changed files with 70 additions and 103 deletions

View File

@ -46,20 +46,22 @@ func (l *CenterResourcesLogic) CenterResources() (resp *types.CenterResourcesRes
for _, centerIndex := range centersIndex { for _, centerIndex := range centersIndex {
// Query the types of resource centers // Query the types of resource centers
l.svcCtx.DbEngin.Raw("select name,type as CenterType from t_adapter where id = ?", centerIndex.Id).Scan(&centerIndex) l.svcCtx.DbEngin.Raw("select name,type as CenterType from t_adapter where id = ?", centerIndex.Id).Scan(&centerIndex)
cpuRawData, err := l.svcCtx.PromClient.GetRawData("center_cpu_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id}) var clustersName string
l.svcCtx.DbEngin.Raw("SELECT GROUP_CONCAT(name SEPARATOR '|' ) as clustersName from t_cluster where adapter_id = ?", centerIndex.Id).Scan(&clustersName)
cpuRawData, err := l.svcCtx.PromClient.GetRawData("center_cpu_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id, ClustersName: clustersName})
cpuData := cpuRawData.(model.Vector) cpuData := cpuRawData.(model.Vector)
if err != nil { if err != nil {
return nil, err return nil, err
} }
centerIndex.Cpu = cpuData[0].Value.String() centerIndex.Cpu = cpuData[0].Value.String()
memoryRawData, err := l.svcCtx.PromClient.GetRawData("center_memory_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id}) memoryRawData, err := l.svcCtx.PromClient.GetRawData("center_memory_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id, ClustersName: clustersName})
if err != nil { if err != nil {
return nil, err return nil, err
} }
memoryData := memoryRawData.(model.Vector) memoryData := memoryRawData.(model.Vector)
centerIndex.Memory = memoryData[0].Value.String() centerIndex.Memory = memoryData[0].Value.String()
diskRawData, err := l.svcCtx.PromClient.GetRawData("center_disk_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id}) diskRawData, err := l.svcCtx.PromClient.GetRawData("center_disk_utilisation", tracker.AdapterOption{AdapterId: centerIndex.Id, ClustersName: clustersName})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -2,12 +2,11 @@ package core
import ( import (
"context" "context"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
"strconv" tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"github.com/zeromicro/go-zero/core/logx"
) )
type SyncClusterLoadLogic struct { type SyncClusterLoadLogic struct {
@ -25,23 +24,11 @@ func NewSyncClusterLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *S
} }
func (l *SyncClusterLoadLogic) SyncClusterLoad(req *types.SyncClusterLoadReq) error { func (l *SyncClusterLoadLogic) SyncClusterLoad(req *types.SyncClusterLoadReq) error {
if len(req.ClusterLoadRecords) != 0 { if nil != req.ClusterLoadRecords {
for _, record := range req.ClusterLoadRecords { for _, record := range req.ClusterLoadRecords {
tracker.ClusterCpuUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuUtilisation) var param tracker.ClusterLoadRecord
tracker.ClusterCpuAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuAvail) tool.Convert(record, &param)
tracker.ClusterCpuTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuTotal) tracker.SyncClusterLoad(param)
tracker.ClusterMemoryUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryUtilisation)
tracker.ClusterMemoryAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryAvail)
tracker.ClusterMemoryTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryTotal)
tracker.ClusterDiskUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskUtilisation)
tracker.ClusterDiskAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskAvail)
tracker.ClusterDiskTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskTotal)
tracker.ClusterPodUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.PodsUtilisation)
tracker.ClusterPodCountGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsCount))
tracker.ClusterPodTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsTotal))
} }
} }
return nil return nil

View File

@ -6,6 +6,7 @@ import (
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
"gorm.io/gorm" "gorm.io/gorm"
"strconv" "strconv"
"time" "time"
@ -211,6 +212,18 @@ func (s *AiStorage) SaveClusterResources(clusterId string, clusterName string, c
if tx.Error != nil { if tx.Error != nil {
return tx.Error return tx.Error
} }
// prometheus
param := tracker.ClusterLoadRecord{
ClusterName: clusterName,
CpuAvail: cpuAvail,
CpuTotal: cpuTotal,
MemoryAvail: memAvail,
MemoryTotal: memTotal,
DiskAvail: diskAvail,
DiskTotal: diskTotal,
}
tracker.SyncClusterLoad(param)
return nil return nil
} }

View File

@ -85,8 +85,6 @@ func makeExpr(metric string, opts QueryOptions) string {
return makeContainerMetricExpr(tmpl, opts) return makeContainerMetricExpr(tmpl, opts)
case LevelPVC: case LevelPVC:
return makePVCMetricExpr(tmpl, opts) return makePVCMetricExpr(tmpl, opts)
case LevelIngress:
return makeIngressMetricExpr(tmpl, opts)
case LevelComponent: case LevelComponent:
return tmpl return tmpl
default: default:
@ -108,6 +106,9 @@ func makeAdapterMetricExpr(tmpl string, o QueryOptions) string {
if o.AdapterId != 0 { if o.AdapterId != 0 {
adapterSelector = fmt.Sprintf(`adapter_id="%d"`, o.AdapterId) adapterSelector = fmt.Sprintf(`adapter_id="%d"`, o.AdapterId)
} }
if len(o.ClustersName) != 0 {
adapterSelector = fmt.Sprintf(`adapter_id="%d"`, o.AdapterId)
}
return strings.Replace(tmpl, "$1", adapterSelector, -1) return strings.Replace(tmpl, "$1", adapterSelector, -1)
} }

View File

@ -29,12 +29,10 @@ const (
LevelNamespace LevelNamespace
LevelApplication LevelApplication
LevelController LevelController
LevelService
LevelPod LevelPod
LevelContainer LevelContainer
LevelPVC LevelPVC
LevelComponent LevelComponent
LevelIngress
LevelAdapter LevelAdapter
) )
@ -46,7 +44,6 @@ var MeteringLevelMap = map[string]int{
"LevelNamespace": LevelNamespace, "LevelNamespace": LevelNamespace,
"LevelApplication": LevelApplication, "LevelApplication": LevelApplication,
"LevelController": LevelController, "LevelController": LevelController,
"LevelService": LevelService,
"LevelPod": LevelPod, "LevelPod": LevelPod,
"LevelContainer": LevelContainer, "LevelContainer": LevelContainer,
"LevelPVC": LevelPVC, "LevelPVC": LevelPVC,
@ -57,15 +54,15 @@ type QueryOption interface {
Apply(*QueryOptions) Apply(*QueryOptions)
} }
type Meteroptions struct { type MeterOptions struct {
Start time.Time Start time.Time
End time.Time End time.Time
Step time.Duration Step time.Duration
} }
type QueryOptions struct { type QueryOptions struct {
Level Level Level Level
ClustersName string
NamespacedResourcesFilter string NamespacedResourcesFilter string
QueryType string QueryType string
ResourceFilter string ResourceFilter string
@ -81,10 +78,9 @@ type QueryOptions struct {
ContainerName string ContainerName string
AdapterId int64 AdapterId int64
ServiceName string ServiceName string
Ingress string
Job string Job string
Duration *time.Duration Duration *time.Duration
MeterOptions *Meteroptions MeterOptions *MeterOptions
} }
func NewQueryOptions() *QueryOptions { func NewQueryOptions() *QueryOptions {
@ -92,12 +88,14 @@ func NewQueryOptions() *QueryOptions {
} }
type AdapterOption struct { type AdapterOption struct {
AdapterId int64 AdapterId int64
ClustersName string
} }
func (a AdapterOption) Apply(o *QueryOptions) { func (a AdapterOption) Apply(o *QueryOptions) {
o.Level = LevelAdapter o.Level = LevelAdapter
o.AdapterId = a.AdapterId o.AdapterId = a.AdapterId
o.ClustersName = a.ClustersName
} }
type ClusterOption struct { type ClusterOption struct {
@ -154,31 +152,6 @@ func (no NamespaceOption) Apply(o *QueryOptions) {
o.Namespace = no.NamespaceName o.Namespace = no.NamespaceName
} }
type ApplicationsOption struct {
NamespaceName string
Applications []string
StorageClassName string
}
func (aso ApplicationsOption) Apply(o *QueryOptions) {
// nothing should be done
//nolint:gosimple
return
}
type OpenpitrixsOption struct {
Cluster string
NamespaceName string
Openpitrixs []string
StorageClassName string
}
func (oso OpenpitrixsOption) Apply(o *QueryOptions) {
// nothing should be done
//nolint:gosimple
return
}
// ApplicationsOption & OpenpitrixsOption share the same ApplicationOption struct // ApplicationsOption & OpenpitrixsOption share the same ApplicationOption struct
type ApplicationOption struct { type ApplicationOption struct {
NamespaceName string NamespaceName string
@ -213,17 +186,6 @@ func (wo WorkloadOption) Apply(o *QueryOptions) {
o.WorkloadKind = wo.WorkloadKind o.WorkloadKind = wo.WorkloadKind
} }
type ServicesOption struct {
NamespaceName string
Services []string
}
func (sso ServicesOption) Apply(o *QueryOptions) {
// nothing should be done
//nolint:gosimple
return
}
type ServiceOption struct { type ServiceOption struct {
ResourceFilter string ResourceFilter string
NamespaceName string NamespaceName string
@ -231,20 +193,6 @@ type ServiceOption struct {
PodNames []string PodNames []string
} }
func (so ServiceOption) Apply(o *QueryOptions) {
o.Level = LevelService
o.Namespace = so.NamespaceName
o.ServiceName = so.ServiceName
pod_names := strings.Join(so.PodNames, "|")
if len(pod_names) > 0 {
o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, pod_names, o.Namespace)
} else {
o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, ".*", o.Namespace)
}
}
type PodOption struct { type PodOption struct {
NamespacedResourcesFilter string NamespacedResourcesFilter string
ResourceFilter string ResourceFilter string
@ -310,25 +258,6 @@ func (po PVCOption) Apply(o *QueryOptions) {
o.Namespace = po.NamespaceName o.Namespace = po.NamespaceName
} }
type IngressOption struct {
ResourceFilter string
NamespaceName string
Ingress string
Job string
Pod string
Duration *time.Duration
}
func (no IngressOption) Apply(o *QueryOptions) {
o.Level = LevelIngress
o.ResourceFilter = no.ResourceFilter
o.Namespace = no.NamespaceName
o.Ingress = no.Ingress
o.Job = no.Job
o.PodName = no.Pod
o.Duration = no.Duration
}
type ComponentOption struct{} type ComponentOption struct{}
func (_ ComponentOption) Apply(o *QueryOptions) { func (_ ComponentOption) Apply(o *QueryOptions) {
@ -342,7 +271,7 @@ type MeterOption struct {
} }
func (mo MeterOption) Apply(o *QueryOptions) { func (mo MeterOption) Apply(o *QueryOptions) {
o.MeterOptions = &Meteroptions{ o.MeterOptions = &MeterOptions{
Start: mo.Start, Start: mo.Start,
End: mo.End, End: mo.End,
Step: mo.Step, Step: mo.Step,

View File

@ -107,6 +107,23 @@ var (
} }
) )
type ClusterLoadRecord struct {
AdapterId int64 `json:"adapterId,optional"`
ClusterName string `json:"clusterName,optional"`
CpuAvail float64 `json:"cpuAvail,optional"`
CpuTotal float64 `json:"cpuTotal,optional"`
CpuUtilisation float64 `json:"cpuUtilisation,optional"`
MemoryAvail float64 `json:"memoryAvail,optional"`
MemoryUtilisation float64 `json:"memoryUtilisation,optional"`
MemoryTotal float64 `json:"memoryTotal,optional"`
DiskAvail float64 `json:"diskAvail,optional"`
DiskTotal float64 `json:"diskTotal,optional"`
DiskUtilisation float64 `json:"diskUtilisation,optional"`
PodsUtilisation float64 `json:"podsUtilisation,optional"`
PodsCount int64 `json:"podsCount,optional"`
PodsTotal int64 `json:"podsTotal,optional"`
}
func init() { func init() {
prometheus.MustRegister(metrics...) prometheus.MustRegister(metrics...)
} }
@ -302,3 +319,21 @@ func (p Prometheus) GetRawData(expr string, o QueryOption) (model.Value, error)
} }
return value, nil return value, nil
} }
func SyncClusterLoad(record ClusterLoadRecord) {
ClusterCpuUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuUtilisation)
ClusterCpuAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuAvail)
ClusterCpuTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.CpuTotal)
ClusterMemoryUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryUtilisation)
ClusterMemoryAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryAvail)
ClusterMemoryTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.MemoryTotal)
ClusterDiskUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskUtilisation)
ClusterDiskAvailGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskAvail)
ClusterDiskTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.DiskTotal)
ClusterPodUtilisationGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(record.PodsUtilisation)
ClusterPodCountGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsCount))
ClusterPodTotalGauge.WithLabelValues(record.ClusterName, strconv.FormatInt(record.AdapterId, 10)).Set(float64(record.PodsTotal))
}