package monitoring

import (
	"context"

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

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

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

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

func (l *NodesLoadTopLogic) NodesLoadTop(req *types.NodesLoadTopReq) (resp *types.NodesLoadTopResp, err error) {

	resp = &types.NodesLoadTopResp{}

	var server string
	l.svcCtx.DbEngin.Raw("select ta.server from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and tc.name = ?", &req.ClusterName).Scan(&server)
	response, err := l.svcCtx.HttpClient.R().
		SetQueryParams(map[string]string{
			"clusterName": req.ClusterName,
			"metrics":     req.Metrics,
		}).
		SetResult(&resp).
		ForceContentType("application/json").
		Get(server + "/api/v1/monitoring/node")
	if err != nil || response.IsError() {
		return nil, err
	}
	return resp, nil
}
