package cloud

import (
	"context"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
	error2 "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/error"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
	"time"

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

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

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

func (l *ControllerMetricsLogic) ControllerMetrics(req *types.ControllerMetricsReq) (resp *types.ControllerMetricsResp, err error) {
	resp = &types.ControllerMetricsResp{}
	if _, ok := l.svcCtx.MonitorClient[req.ParticipantId]; ok {
		if len(req.Pod) != 0 {
			resp.Data = l.svcCtx.MonitorClient[req.ParticipantId].GetNamedMetricsByTime(req.Metrics, req.Start, req.End, 60*time.Minute, tracker.PodOption{
				PodName: req.Pod,
			})
		} else {

			resp.Data = l.svcCtx.MonitorClient[req.ParticipantId].GetNamedMetricsByTime(req.Metrics, req.Start, req.End, 60*time.Minute, tracker.ControllerOption{
				WorkloadName: req.WorkloadName,
			})
		}
	} else {
		return nil, error2.NewCodeError(400, "prometheus endpoint invalid!")
	}

	return resp, nil
}
