Skip to content

Commit

Permalink
feat(dbm-services): 追加写标签 #8444
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq authored and iSecloud committed Dec 13, 2024
1 parent 2077b5c commit c61669b
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 375 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package manage

import (
rf "github.com/gin-gonic/gin"

"dbm-services/common/db-resource/internal/model"
"dbm-services/common/go-pubpkg/logger"
)

// AddLabelsParam add labels param
type AddLabelsParam struct {
BkHostIds []int `json:"bk_host_ids" binding:"required,gt=0,dive"`
Labels []string `json:"labels,omitempty"`
}

// AddLabels add labels
func (c *MachineResourceHandler) AddLabels(r *rf.Context) {
var input AddLabelsParam
if err := c.Prepare(r, &input); err != nil {
logger.Error("Preare Error %s", err.Error())
return
}
db := model.DB.Self.Table("tb_rp_detail").Exec("update tb_rp_detail set labels=? where bk_host_id in (?)",
model.JsonMerge("labels", input.Labels), input.BkHostIds)
err := db.Error
if err != nil {
logger.Error("failed to add labels:%s", err.Error())
c.SendResponse(r, err, nil)
return
}
c.SendResponse(r, nil, map[string]interface{}{"affected_count": db.RowsAffected})
}

This file was deleted.

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 All @@ -45,6 +46,7 @@ func (c *MachineResourceHandler) RegisterRouter(engine *rf.Engine) {
r.POST("/list/all", c.ListAll)
r.POST("/update", c.Update)
r.POST("/batch/update", c.BatchUpdate)
r.POST("/append/labels", c.AddLabels)
r.POST("/delete", c.Delete)
r.POST("/import", c.Import)
r.POST("/mountpoints", c.GetMountPoints)
Expand Down Expand Up @@ -307,15 +309,15 @@ func (c *MachineResourceHandler) GroupByLabelCount(r *rf.Context) {
logger.Info("rs len %d", len(rs))
ret := make(map[string]int)
for _, v := range rs {
var lables []string
var labels []string
logger.Info("labels %s", string(v.Labels))
if err := json.Unmarshal(v.Labels, &lables); err != nil {
if err := json.Unmarshal(v.Labels, &labels); err != nil {
logger.Error("unmarshal failed %s", err.Error())
c.SendResponse(r, err, err.Error())
return
}
if len(lables) > 0 {
for _, l := range lables {
if len(labels) > 0 {
for _, l := range lo.Uniq(labels) {
ret[l]++
}
}
Expand Down
49 changes: 49 additions & 0 deletions dbm-services/common/db-resource/internal/model/json_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package model

import (
"github.com/samber/lo"
"gorm.io/gorm/clause"
)

// JSONMergeBuilder json query expression, implements clause.Expression interface to use as querier
type JSONMergeBuilder struct {
column string
keys []string
}

// Build json merge expression
// nolint
func (m *JSONMergeBuilder) Build(builder clause.Builder) {
builder.WriteString("JSON_MERGE(")
builder.WriteString(m.column)
builder.WriteString(",")
builder.WriteByte('\'')
builder.WriteByte('[')
for i, key := range lo.Uniq(m.keys) {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString("\"" + key + "\"")
}
builder.WriteByte(']')
builder.WriteByte('\'')
builder.WriteByte(')')
}

// JsonMerge IntArry json merge int array
func JsonMerge(column string, keys []string) *JSONMergeBuilder {
return &JSONMergeBuilder{
column: column,
keys: keys,
}
}
Loading

0 comments on commit c61669b

Please sign in to comment.