package monitoring

import (
	"context"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
	"time"

	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ClustersLoadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

type ClusterInfo struct {
	ClusterId   int64            `json:"clusterId"`
	ClusterName string           `json:"clusterName"`
	Metrics     []tracker.Metric `json:"metrics"`
}

func NewClustersLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClustersLoadLogic {
	return &ClustersLoadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ClustersLoadLogic) ClustersLoad(req *types.ClustersLoadReq) (resp *types.ClustersLoadResp, err error) {
	resp = &types.ClustersLoadResp{}
	// 查询集群列表
	var clustersModel []models.ComputeCluster
	l.svcCtx.DbEngin.Raw("select * from t_cluster where label = 'kubernetes'").Scan(&clustersModel)
	var result []ClusterInfo
	for _, cluster := range clustersModel {
		metrics := []string{"cluster_cpu_utilisation", "cluster_cpu_avail", "cluster_cpu_total", "cluster_memory_total", "cluster_memory_avail", "cluster_memory_utilisation", "cluster_disk_utilisation", "cluster_disk_avail", "cluster_disk_total", "cluster_pod_utilisation", "cluster_node_count"}
		data := l.svcCtx.PromClient.GetNamedMetrics(metrics, time.Now(), tracker.ClusterOption{ClusterName: cluster.Name.String})
		clusterInfo := ClusterInfo{
			ClusterId:   cluster.Id,
			ClusterName: cluster.Name.String,
			Metrics:     data,
		}
		result = append(result, clusterInfo)

	}
	resp.Data = result
	return resp, nil
}
