Skip to content

Commit

Permalink
feat(dbm-services): 资源池统计视图相关接口 TencentBlueKing#6160
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Sep 20, 2024
1 parent 41d086a commit 5448ccf
Show file tree
Hide file tree
Showing 23 changed files with 1,187 additions and 573 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
// ImportMachParam 资源导入请求参数
type ImportMachParam struct {
// ForBizs 业务标签,表示这个资源将来给ForBizs这个业务使用
ForBizs []int `json:"for_bizs"`
RsTypes []string `json:"resource_types"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
BkBizId int `json:"bk_biz_id" binding:"number"`
Hosts []HostBase `json:"hosts" binding:"gt=0,dive,required"`
Labels map[string]string `json:"labels"`
Expand Down Expand Up @@ -120,29 +120,13 @@ type ImportHostResp struct {
NotFoundInCCHosts []string `json:"not_found_in_cc_hosts"`
}

func (p ImportMachParam) transParamToBytes() (lableJson, bizJson, rstypes json.RawMessage, err error) {
func (p ImportMachParam) transParamToBytes() (lableJson json.RawMessage, err error) {
// lableJson = []byte("{}")
lableJson, err = json.Marshal(cmutil.CleanStrMap(p.Labels))
if err != nil {
logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error()))
return
}
bizJson = []byte("[]")
if len(p.ForBizs) > 0 {
bizJson, err = json.Marshal(cmutil.IntSliceToStrSlice(p.ForBizs))
if err != nil {
logger.Error(fmt.Sprintf("conver biz json Failed,Error:%s", err.Error()))
return
}
}
rstypes = []byte("[]")
if len(p.RsTypes) > 0 {
rstypes, err = json.Marshal(cmutil.StringsRemoveEmpty(p.RsTypes))
if err != nil {
logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error()))
return
}
}
return
}

Expand Down Expand Up @@ -183,7 +167,7 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) {
}
resp.SearchDiskErrInfo = diskResp.IpFailedLogMap
resp.NotFoundInCCHosts = notFoundHosts
lableJson, bizJson, rstypes, err := param.transParamToBytes()
lableJson, err := param.transParamToBytes()
if err != nil {
return resp, err
}
Expand All @@ -204,7 +188,7 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) {
}
for _, h := range ccHostsInfo {
delete(hostsMap, h.InnerIP)
el := transHostInfoToDbModule(h, h.BkCloudId, param.BkBizId, rstypes, bizJson, lableJson)
el := param.transHostInfoToDbModule(h, h.BkCloudId, lableJson)
el.SetMore(h.InnerIP, diskResp.IpLogContentMap)
// gse agent 1.0的 agent 是用 cloudid:ip
gseAgentId := h.BkAgentId
Expand Down Expand Up @@ -265,16 +249,16 @@ func getCvmMachList(hosts []*cc.Host) []string {
}

// transHostInfoToDbModule 获取的到的主机信息赋值给db model
func transHostInfoToDbModule(h *cc.Host, bkCloudId, bkBizId int, rstp, biz, label []byte) model.TbRpDetail {
func (p ImportMachParam) transHostInfoToDbModule(h *cc.Host, bkCloudId int, label []byte) model.TbRpDetail {
osType := h.BkOsType
if cmutil.IsEmpty(osType) {
osType = bk.OsLinux
}
return model.TbRpDetail{
RsTypes: rstp,
DedicatedBizs: biz,
DedicatedBiz: p.ForBiz,
RsType: p.RsType,
BkCloudID: bkCloudId,
BkBizId: bkBizId,
BkBizId: p.BkBizId,
AssetID: h.AssetID,
BkHostID: h.BKHostId,
IP: h.InnerIP,
Expand Down
56 changes: 24 additions & 32 deletions dbm-services/common/db-resource/internal/controller/manage/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (
"strings"

rf "github.com/gin-gonic/gin"
"github.com/samber/lo"
"gorm.io/gorm"

"dbm-services/common/db-resource/internal/model"
"dbm-services/common/db-resource/internal/svr/apply"
"dbm-services/common/db-resource/internal/svr/bk"
"dbm-services/common/db-resource/internal/svr/dbmapi"
"dbm-services/common/db-resource/internal/svr/meta"
"dbm-services/common/go-pubpkg/cmutil"
"dbm-services/common/go-pubpkg/errno"
Expand All @@ -30,21 +31,21 @@ import (
// MachineResourceGetterInputParam TODO
type MachineResourceGetterInputParam struct {
// 专用业务Ids
ForBizs []int `json:"for_bizs"`
City []string `json:"city"`
SubZones []string `json:"subzones"`
DeviceClass []string `json:"device_class"`
Labels map[string]string `json:"labels"`
Hosts []string `json:"hosts"`
BkCloudIds []int `json:"bk_cloud_ids"`
RsTypes []string `json:"resource_types"`
MountPoint string `json:"mount_point"`
Cpu apply.MeasureRange `json:"cpu"`
Mem apply.MeasureRange `json:"mem"`
Disk apply.MeasureRange `json:"disk"`
DiskType string `json:"disk_type"`
OsType string `json:"os_type"`
StorageSpecs []apply.DiskSpec `json:"storage_spec"`
ForBiz int `json:"for_biz"`
City []string `json:"city"`
SubZoneIds []string `json:"subzone_ids"`
DeviceClass []string `json:"device_class"`
Labels map[string]string `json:"labels"`
Hosts []string `json:"hosts"`
BkCloudIds []int `json:"bk_cloud_ids"`
RsType string `json:"resource_type"`
MountPoint string `json:"mount_point"`
Cpu meta.MeasureRange `json:"cpu"`
Mem meta.MeasureRange `json:"mem"`
Disk meta.MeasureRange `json:"disk"`
DiskType string `json:"disk_type"`
OsType string `json:"os_type"`
StorageSpecs []meta.DiskSpec `json:"storage_spec"`
// true,false,""
GseAgentAlive string `json:"gse_agent_alive"`
Limit int `json:"limit"`
Expand Down Expand Up @@ -129,7 +130,7 @@ func (c *MachineResourceGetterInputParam) matchStorageSpecs(db *gorm.DB) {

func (c *MachineResourceGetterInputParam) getRealCitys() (realCistys []string, err error) {
for _, logicCity := range c.City {
rcitys, err := meta.GetIdcCityByLogicCity(logicCity)
rcitys, err := dbmapi.GetIdcCityByLogicCity(logicCity)
if err != nil {
logger.Error("from %s get real citys failed %s", logicCity, err.Error())
return nil, err
Expand Down Expand Up @@ -172,13 +173,8 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {
if len(c.BkCloudIds) > 0 {
db.Where("bk_cloud_id in (?) ", c.BkCloudIds)
}
if len(c.RsTypes) > 0 {
// 如果参数["all"],表示选择没有任何资源类型标签的资源
if c.RsTypes[0] == "all" {
db.Where("JSON_LENGTH(rs_types) <= 0")
} else {
db.Where("(?)", model.JSONQuery("rs_types").JointOrContains(c.RsTypes))
}
if lo.IsNotEmpty(c.RsType) {
db.Where("rs_type = ? ", c.RsType)
}
c.matchSpec(db)
c.matchStorageSpecs(db)
Expand All @@ -189,17 +185,13 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {
}
db.Where(" city in (?) ", realCitys)
}
if len(c.SubZones) > 0 {
db.Where(" sub_zone in (?) ", c.SubZones)
if len(c.SubZoneIds) > 0 {
db.Where(" sub_zone_id in (?) ", c.SubZoneIds)
}

if len(c.ForBizs) > 0 {
if c.ForBiz > 0 {
// 如果参数[0],表示选择没有任何业务标签的资源
if c.ForBizs[0] == 0 {
db.Where("JSON_LENGTH(dedicated_bizs) <= 0")
} else {
db.Where("(?)", model.JSONQuery("dedicated_bizs").JointOrContains(cmutil.IntSliceToStrSlice(c.ForBizs)))
}
db.Where("dedicated_biz = ?", c.ForBiz)
}
if cmutil.IsNotEmpty(c.OsType) {
db.Where("os_type = ?", c.OsType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"dbm-services/common/go-pubpkg/logger"

rf "github.com/gin-gonic/gin"
"github.com/samber/lo"
)

// MachineResourceHandler 主机处理handler
Expand Down Expand Up @@ -86,8 +87,8 @@ func (c *MachineResourceHandler) Delete(r *rf.Context) {
// BatchUpdateMachineInput 批量编辑主机信息请求参数
type BatchUpdateMachineInput struct {
BkHostIds []int `json:"bk_host_ids" binding:"required,dive,gt=0" `
ForBizs []int `json:"for_bizs"`
RsTypes []string `json:"resource_types"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
RackId string `json:"rack_id"`
SetBizEmpty bool `json:"set_empty_biz"`
SetRsTypeEmpty bool `json:"set_empty_resource_type"`
Expand All @@ -111,31 +112,13 @@ func (c *MachineResourceHandler) BatchUpdate(r *rf.Context) {
}

// update for biz
if len(input.ForBizs) > 0 {
bizJson, err := json.Marshal(cmutil.IntSliceToStrSlice(input.ForBizs))
if err != nil {
logger.Error(fmt.Sprintf("conver biz json Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["dedicated_bizs"] = bizJson
}
if input.SetBizEmpty {
updateMap["dedicated_bizs"] = EmptyArryJson
if input.ForBiz > 0 {
updateMap["dedicated_biz"] = input.ForBiz
}

// update resource type
if len(input.RsTypes) > 0 {
rstypes, err := json.Marshal(input.RsTypes)
if err != nil {
logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["rs_types"] = rstypes
}
if input.SetRsTypeEmpty {
updateMap["rs_types"] = EmptyArryJson
if lo.IsNotEmpty(input.RsType) {
updateMap["rs_type"] = input.RsType
}

// update disk
Expand All @@ -155,7 +138,7 @@ func (c *MachineResourceHandler) BatchUpdate(r *rf.Context) {
}

// do update
err := model.DB.Self.Table(model.TbRpDetailName()).Select("dedicated_bizs", "rs_types", "storage_device", "rack_id").
err := model.DB.Self.Table(model.TbRpDetailName()).Select("dedicated_biz", "rs_type", "storage_device", "rack_id").
Where("bk_host_id in (?)", input.BkHostIds).Updates(updateMap).Error
if err != nil {
c.SendResponse(r, err, requestId, err.Error())
Expand All @@ -175,8 +158,8 @@ type MachineResourceInputParam struct {
type MachineResource struct {
BkHostID int `json:"bk_host_id" binding:"required"`
Labels map[string]string `json:"labels"`
ForBizs []int `json:"for_bizs"`
RsTypes []string `json:"resource_types"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
StorageDevice map[string]bk.DiskDetail `json:"storage_device"`
}

Expand All @@ -199,23 +182,11 @@ func (c *MachineResourceHandler) Update(r *rf.Context) {
}
updateMap["lable"] = l
}
if len(v.ForBizs) > 0 {
bizJson, err := json.Marshal(cmutil.IntSliceToStrSlice(v.ForBizs))
if err != nil {
logger.Error(fmt.Sprintf("conver biz json Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["dedicated_bizs"] = bizJson
if v.ForBiz > 0 {
updateMap["dedicated_biz"] = v.ForBiz
}
if len(v.RsTypes) > 0 {
rstypes, err := json.Marshal(v.RsTypes)
if err != nil {
logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["rs_types"] = rstypes
if lo.IsNotEmpty(v.RsType) {
updateMap["rs_type"] = v.RsType
}
if len(v.StorageDevice) > 0 {
storageJson, err := json.Marshal(v.StorageDevice)
Expand All @@ -226,7 +197,7 @@ func (c *MachineResourceHandler) Update(r *rf.Context) {
}
updateMap["storage_device"] = storageJson
}
err := tx.Model(&model.TbRpDetail{}).Table(model.TbRpDetailName()).Select("dedicated_bizs", "rs_types",
err := tx.Model(&model.TbRpDetail{}).Table(model.TbRpDetailName()).Select("dedicated_biz", "rs_type",
"label").Where("bk_host_id=?", v.BkHostID).Updates(updateMap).Error
if err != nil {
tx.Rollback()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
package manage

import (
"strconv"

"github.com/gin-gonic/gin"

"dbm-services/common/db-resource/internal/model"
"dbm-services/common/db-resource/internal/svr/apply"
"dbm-services/common/db-resource/internal/svr/bk"
"dbm-services/common/db-resource/internal/svr/meta"
"dbm-services/common/go-pubpkg/cmutil"
"dbm-services/common/go-pubpkg/errno"
"dbm-services/common/go-pubpkg/logger"
Expand All @@ -33,10 +32,10 @@ type SpecCheckInput struct {

// SpecInfo TODO
type SpecInfo struct {
GroupMark string `json:"group_mark" binding:"required" `
DeviceClass []string `json:"device_class"`
Spec apply.Spec `json:"spec"`
StorageSpecs []apply.DiskSpec `json:"storage_spec"`
GroupMark string `json:"group_mark" binding:"required" `
DeviceClass []string `json:"device_class"`
Spec meta.Spec `json:"spec"`
StorageSpecs []meta.DiskSpec `json:"storage_spec"`
}

// SpecSum TODO
Expand Down Expand Up @@ -67,8 +66,7 @@ func (m MachineResourceHandler) SpecSum(r *gin.Context) {
// 如果没有指定资源类型,表示只能选择无资源类型标签的资源
// 没有资源类型标签的资源可以被所有其他类型使用
if input.ForbizId > 0 {
db.Where("( ? or JSON_LENGTH(dedicated_bizs)<=0 )", model.JSONQuery("dedicated_bizs").Contains([]string{
strconv.Itoa(input.ForbizId)}))
db.Where("dedicated_biz = ? ", input.ForbizId)
}
if cmutil.IsEmpty(input.ResourceType) {
db.Where("JSON_LENGTH(rs_types) <= 0")
Expand Down
Loading

0 comments on commit 5448ccf

Please sign in to comment.