/*

 Copyright (c) [2023] [pcm]
 [pcm-coordinator] is licensed under Mulan PSL v2.
 You can use this software according to the terms and conditions of the Mulan PSL v2.
 You may obtain a copy of Mulan PSL v2 at:
          http://license.coscl.org.cn/MulanPSL2
 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 See the Mulan PSL v2 for more details.

*/

package core

import (
	"context"
	"fmt"
	"strconv"

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

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

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

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

func (l *GetComputilityStatisticsLogic) GetComputilityStatistics() (resp *types.ComputilityStatisticsResp, err error) {
	// todo: add your logic here and delete this line

	resp = &types.ComputilityStatisticsResp{}
	var computilityStatistics types.ComputilityStatistics
	var domainSum int64     //所有域的总和
	var AiFlopsNum float64  //所有域的总和
	var K8sFlopsNum float64 //所有域的总和
	var aiClusterSum int64  //所有域的总和
	var hpcClusterSum int64 //所有域的总和
	var PCClusterSum int64  //所有域的总和
	var ClusterSum int64    //所有域的总和

	//算力中心总数（计算域）：包括所有云算域，智算域，超算域以及鹏城返回的所有域的总和
	sqlStr := "select count(*) as domainSum from `pcm`.domain_resource t  where t.connection_state = 3 OR t.domain_source=0"
	tx := l.svcCtx.DbEngin.Raw(sqlStr).Scan(&domainSum)
	if tx.Error != nil {
		logx.Error(err)
		return nil, tx.Error
	}

	computilityStatistics.DomainSum = domainSum

	//统计集群
	clusterStr := "select  count(*) from `joint_domain`.domain_cluster"
	tc := l.svcCtx.DbEngin.Raw(clusterStr).Scan(&ClusterSum)
	if tc.Error != nil {
		logx.Error(err)
		return nil, tc.Error
	}
	//智算算力总和
	aiFlopsStr := "select sum(t.flops) as flopsNum from `pcm`.domain_resource t"
	tf := l.svcCtx.DbEngin.Raw(aiFlopsStr).Scan(&AiFlopsNum)
	if tf.Error != nil {
		logx.Error(err)
		return nil, tf.Error
	}

	//云算算力总和 SELECT sum(t.flops) FROM `resources` t
	k8sFlopsStr := "SELECT sum(t.flops) as k8sFlopsNum  FROM `joint_domain`.`resources` t"
	tk := l.svcCtx.DbEngin.Raw(k8sFlopsStr).Scan(&K8sFlopsNum)
	if tk.Error != nil {
		logx.Error(err)
		return nil, tk.Error
	}
	value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", K8sFlopsNum/1000000+AiFlopsNum), 64)

	computilityStatistics.TotalComputility = value

	//智算集群数
	aiClusterStr := "select  count(*) as aiClusterSum from `pcm`.ai_cluster"
	ts := l.svcCtx.DbEngin.Raw(aiClusterStr).Scan(&aiClusterSum)
	if ts.Error != nil {
		logx.Error(err)
		return nil, ts.Error
	}
	//云算集群数
	hpcClusterStr := "select  count(*)  as  hpcClusterSum  from `pcm`.hpc_cluster"
	th := l.svcCtx.DbEngin.Raw(hpcClusterStr).Scan(&hpcClusterSum)
	if th.Error != nil {
		logx.Error(err)
		return nil, th.Error
	}
	//鹏城集群数
	PCClusterStr := "select count(*) as PCClusterSum from `pcm`.domain_resource t  where t.connection_state = 3 "
	tp := l.svcCtx.DbEngin.Raw(PCClusterStr).Scan(&PCClusterSum)
	if tp.Error != nil {
		logx.Error(err)
		return nil, tp.Error
	}
	computilityStatistics.ClusterNum = ClusterSum + PCClusterSum + hpcClusterSum + aiClusterSum
	resp.ComputilityStatistics = computilityStatistics
	resp.Code = 200
	resp.Msg = "Success"
	return resp, nil
}
