forked from JointCloud/pcm-octopus
129 lines
3.5 KiB
Go
129 lines
3.5 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"github.com/go-redis/redis"
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
"gitlink.org.cn/JointCloud/pcm-octopus/internal/common"
|
||
"gitlink.org.cn/JointCloud/pcm-octopus/internal/config"
|
||
"gitlink.org.cn/JointCloud/pcm-octopus/internal/svc"
|
||
"gitlink.org.cn/JointCloud/pcm-octopus/octopus"
|
||
"log"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
type GetComputingPowerLogic struct {
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
logx.Logger
|
||
}
|
||
|
||
func NewGetComputingPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetComputingPowerLogic {
|
||
return &GetComputingPowerLogic{
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
Logger: logx.WithContext(ctx),
|
||
}
|
||
}
|
||
|
||
func (l *GetComputingPowerLogic) GetComputingPower(in *octopus.ResourceReq) (*octopus.CpResp, error) {
|
||
var resp octopus.CpResp
|
||
var cp float32
|
||
|
||
redisClient := redis.NewClient(&redis.Options{
|
||
Addr: l.svcCtx.Config.RedisConf.Host,
|
||
Password: l.svcCtx.Config.RedisConf.Pass,
|
||
})
|
||
|
||
defer redisClient.Close()
|
||
_, err := redisClient.Ping().Result()
|
||
|
||
if err != nil {
|
||
log.Println("redis连接失败", err)
|
||
cp = getCPFromOctopus()
|
||
redisClient.Set(l.svcCtx.Config.OctopusConfig.OctopusCp, cp, 168*time.Hour)
|
||
} else {
|
||
res, err := redisClient.Get(l.svcCtx.Config.OctopusConfig.OctopusCp).Float32()
|
||
if err == redis.Nil {
|
||
log.Println("redis key未找到或已过期,重新请求")
|
||
cp = getCPFromOctopus()
|
||
redisClient.Set(l.svcCtx.Config.OctopusConfig.OctopusCp, cp, 168*time.Hour)
|
||
} else {
|
||
cp = res
|
||
}
|
||
}
|
||
resp.POpsAtFp16 = cp
|
||
return &resp, nil
|
||
}
|
||
|
||
func getCPFromOctopus() float32 {
|
||
octopusConfig := config.Cfg
|
||
|
||
urlMap := map[string]string{
|
||
common.Hanwuji: octopusConfig.OctopusConfig.HanwujiUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||
common.Suiyuan: octopusConfig.OctopusConfig.SuiyuanUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||
common.Sailingsi: octopusConfig.OctopusConfig.SailingsiUrl + octopusConfig.OctopusConfig.OctopusResouceSpec,
|
||
}
|
||
|
||
var computingPowerInTops int32
|
||
for k, v := range urlMap {
|
||
token := common.GetToken(k)
|
||
if token == "" {
|
||
continue
|
||
}
|
||
body, err := common.OctopusHttpClient("GET", v, nil, token)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
//获取训练资源算力
|
||
switch k {
|
||
case common.Hanwuji:
|
||
resourceSpec := common.HanwujiResp{}
|
||
err := json.Unmarshal(body, &resourceSpec)
|
||
if err != nil {
|
||
log.Println("Hanwuji json转换失败 : ", err)
|
||
continue
|
||
}
|
||
if !resourceSpec.Success {
|
||
log.Println("Hanwuji 获取训练资源失败 : ", resourceSpec.Error)
|
||
continue
|
||
}
|
||
for _, spec := range resourceSpec.Payload.MapResourceSpecIdList.Train.ResourceSpecs {
|
||
numOfCards, err := strconv.ParseInt(spec.ResourceQuantity.CambriconComMlu, 10, 32)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
computingPowerInTops += octopusConfig.OctopusConfig.CambriconMLU290 * int32(numOfCards)
|
||
}
|
||
case common.Suiyuan:
|
||
resourceSpec := common.SuiyuanResp{}
|
||
err := json.Unmarshal(body, &resourceSpec)
|
||
if err != nil {
|
||
log.Println("Suiyuan json转换失败 : ", err)
|
||
continue
|
||
}
|
||
if !resourceSpec.Success {
|
||
log.Println("Suiyuan 获取训练资源失败 : ", resourceSpec.Error)
|
||
continue
|
||
}
|
||
for _, spec := range resourceSpec.Payload.MapResourceSpecIdList.Train.ResourceSpecs {
|
||
numOfCards, err := strconv.ParseInt(spec.ResourceQuantity.EnflameComDtu, 10, 32)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
computingPowerInTops += octopusConfig.OctopusConfig.EnflameT20 * int32(numOfCards)
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
if computingPowerInTops == 0 {
|
||
return 0
|
||
}
|
||
|
||
return float32(computingPowerInTops) / 1024
|
||
}
|