Skip to content

Commit

Permalink
fix(frontend): mongodb实例视图 #2920
Browse files Browse the repository at this point in the history
  • Loading branch information
styLjc committed Jan 11, 2024
1 parent ca826a2 commit 07d480d
Show file tree
Hide file tree
Showing 48 changed files with 446 additions and 598 deletions.
2 changes: 1 addition & 1 deletion .gtmproject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ github:
repo_name: "blueking-dbm"

# 指定里程碑ID,
milestone_id: "7"
milestone_id: "8"

project:
# 主分支
Expand Down
190 changes: 115 additions & 75 deletions dbm-services/common/db-resource/internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,96 +248,136 @@ func (jsonQuery *JSONQueryExpression) Equals(value interface{}, keys ...string)
return jsonQuery
}

func (jsonQuery *JSONQueryExpression) jointOrContainsBuild(builder clause.Builder) {
for idx, v := range jsonQuery.jointOrContainVals {
if idx != 0 {
builder.WriteString(" OR ")
}
builder.WriteString("JSON_CONTAINS(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString(",'")
builder.WriteString("[\"")
builder.WriteString(v)
builder.WriteString("\"]') ")
}
}

func (jsonQuery *JSONQueryExpression) extractBuild(stmt *gorm.Statement, builder clause.Builder) {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQuery.path)
builder.WriteString(")")
}

func (jsonQuery *JSONQueryExpression) hasKeysBuild(stmt *gorm.Statement, builder clause.Builder) {
if len(jsonQuery.keys) > 0 {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") IS NOT NULL")
}
}

func (jsonQuery *JSONQueryExpression) gteBuild(stmt *gorm.Statement, builder clause.Builder) {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") >=")
builder.WriteString(strconv.Itoa(jsonQuery.Gtv))
}

func (jsonQuery *JSONQueryExpression) lteBuild(stmt *gorm.Statement, builder clause.Builder) {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") <=")
builder.WriteString(strconv.Itoa(jsonQuery.Ltv))
}

func (jsonQuery *JSONQueryExpression) numrangesBuild(stmt *gorm.Statement, builder clause.Builder) {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") ")
builder.WriteString(" BETWEEN ")
builder.WriteString(strconv.Itoa(jsonQuery.numRange.Min))
builder.WriteString(" AND ")
builder.WriteString(strconv.Itoa(jsonQuery.numRange.Max))
}

func (jsonQuery *JSONQueryExpression) mapcontainsBuild(stmt *gorm.Statement, builder clause.Builder) {
builder.WriteString("JSON_CONTAINS(JSON_KEYS(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString("),'[")
builder.WriteString(jsonArryJoin(jsonQuery.mapcontainVals))
builder.WriteString("]') ")
}

func (jsonQuery *JSONQueryExpression) containsBuild(stmt *gorm.Statement, builder clause.Builder) {
builder.WriteString("JSON_CONTAINS(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString(",'")
builder.WriteString("[")
builder.WriteString(jsonArryJoin(jsonQuery.containVals))
builder.WriteString("]') ")
}

func (jsonQuery *JSONQueryExpression) subcontainsBuild(stmt *gorm.Statement, builder clause.Builder) {
builder.WriteString("JSON_CONTAINS(JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString(",'$.*.\"")
builder.WriteString(jsonQuery.keys[0])
builder.WriteString("\"'),'[\"")
builder.WriteString(jsonQuery.subcontainVal)
builder.WriteString("\"]') ")
}

func (jsonQuery *JSONQueryExpression) equalsBuild(stmt *gorm.Statement, builder clause.Builder) {
if len(jsonQuery.keys) > 0 {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") = ")
if value, ok := jsonQuery.equalsValue.(bool); ok {
builder.WriteString(strconv.FormatBool(value))
} else {
stmt.AddVar(builder, jsonQuery.equalsValue)
}
}
}

// Build implements clause.Expression
func (jsonQuery *JSONQueryExpression) Build(builder clause.Builder) {
if stmt, ok := builder.(*gorm.Statement); ok {
switch stmt.Dialector.Name() {
case "mysql", "sqlite":
case "mysql":
switch {
case jsonQuery.extract:
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQuery.path)
builder.WriteString(")")
jsonQuery.extractBuild(stmt, builder)
case jsonQuery.hasKeys:
if len(jsonQuery.keys) > 0 {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") IS NOT NULL")
}
jsonQuery.hasKeysBuild(stmt, builder)
case jsonQuery.gte:
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") >=")
builder.WriteString(strconv.Itoa(jsonQuery.Gtv))
jsonQuery.gteBuild(stmt, builder)
case jsonQuery.lte:
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") <=")
builder.WriteString(strconv.Itoa(jsonQuery.Ltv))
jsonQuery.lteBuild(stmt, builder)
case jsonQuery.numranges:
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") ")
builder.WriteString(" BETWEEN ")
builder.WriteString(strconv.Itoa(jsonQuery.numRange.Min))
builder.WriteString(" AND ")
builder.WriteString(strconv.Itoa(jsonQuery.numRange.Max))
jsonQuery.numrangesBuild(stmt, builder)
case jsonQuery.mapcontains:
builder.WriteString("JSON_CONTAINS(JSON_KEYS(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString("),'[")
builder.WriteString(jsonArryJoin(jsonQuery.mapcontainVals))
builder.WriteString("]') ")
jsonQuery.mapcontainsBuild(stmt, builder)
case jsonQuery.contains:
builder.WriteString("JSON_CONTAINS(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString(",'")
builder.WriteString("[")
builder.WriteString(jsonArryJoin(jsonQuery.containVals))
builder.WriteString("]') ")
jsonQuery.containsBuild(stmt, builder)
case jsonQuery.jointOrContains:
for idx, v := range jsonQuery.jointOrContainVals {
if idx != 0 {
builder.WriteString(" OR ")
}
builder.WriteString("JSON_CONTAINS(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString(",'")
builder.WriteString("[\"")
builder.WriteString(v)
builder.WriteString("\"]') ")
}
jsonQuery.jointOrContainsBuild(builder)
case jsonQuery.subcontains:
builder.WriteString("JSON_CONTAINS(JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteString(",'$.*.\"")
builder.WriteString(jsonQuery.keys[0])
builder.WriteString("\"'),'[\"")
builder.WriteString(jsonQuery.subcontainVal)
builder.WriteString("\"]') ")
jsonQuery.subcontainsBuild(stmt, builder)
case jsonQuery.equals:
if len(jsonQuery.keys) > 0 {
builder.WriteString("JSON_EXTRACT(")
builder.WriteQuoted(jsonQuery.column)
builder.WriteByte(',')
builder.AddVar(stmt, jsonQueryJoin(jsonQuery.keys))
builder.WriteString(") = ")
if value, ok := jsonQuery.equalsValue.(bool); ok {
builder.WriteString(strconv.FormatBool(value))
} else {
stmt.AddVar(builder, jsonQuery.equalsValue)
}
}
jsonQuery.equalsBuild(stmt, builder)
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions dbm-services/mysql/db-partition/cron/cron_basic_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ package cron
import (
"fmt"
"log/slog"
"strings"
"time"

"dbm-services/common/go-pubpkg/errno"
"dbm-services/mysql/db-partition/model"
"dbm-services/mysql/db-partition/monitor"
"dbm-services/mysql/db-partition/service"
"dbm-services/mysql/db-partition/util"
)

// Scheduler TODO
Expand All @@ -30,12 +28,7 @@ var Scheduler string
func (m PartitionJob) Run() {
var err error
var key string
Scheduler, err = util.ExecShellCommand(false, `hostname -I`)
Scheduler = strings.Replace(Scheduler, " ", "", -1)
Scheduler = strings.Replace(Scheduler, "\n", "", -1)
if err != nil {
Scheduler = "0.0.0.0"
}
Scheduler = "127.0.0.1"
offetSeconds := m.ZoneOffset * 60 * 60
zone := time.FixedZone(m.ZoneName, offetSeconds)
m.CronDate = time.Now().In(zone).Format("20060102")
Expand Down Expand Up @@ -80,7 +73,7 @@ func (m PartitionJob) ExecutePartitionCron(clusterType string) {
} else {
dimension := monitor.NewPartitionEventDimension(item.BkBizId, *item.BkCloudId, item.ImmuteDomain)
content := fmt.Sprintf("partition error. get partition sql fail: %s", err.Error())
monitor.SendEvent(monitor.PartitionEvent, dimension, content, "0.0.0.0")
monitor.SendEvent(monitor.PartitionEvent, dimension, content, "127.0.0.1")
_ = service.AddLog(item.ConfigId, item.BkBizId, item.ClusterId, *item.BkCloudId, 0,
item.ImmuteDomain, zone, m.CronDate, Scheduler, content, service.CheckFailed, item.ClusterType)
slog.Error(fmt.Sprintf("%v", *item), "get partition sql fail", err)
Expand Down
8 changes: 8 additions & 0 deletions dbm-services/mysql/db-partition/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package handler

import (
"dbm-services/mysql/db-partition/monitor"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -304,6 +305,13 @@ func CronStart(r *gin.Context) {
return
}

// InitMonitor 初始监控配置
func InitMonitor(r *gin.Context) {
monitor.InitMonitor()
SendResponse(r, nil, "初始监控配置")
return
}

// Response TODO
type Response struct {
Code int `json:"code"`
Expand Down
6 changes: 4 additions & 2 deletions dbm-services/mysql/db-partition/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func SendEvent(eventName string, dimension map[string]interface{}, content strin
// NewDeveloperEventDimension 构建自定监控事件的维度,发送给平台管理员
func NewDeveloperEventDimension(serverIp string) map[string]interface{} {
dimension := make(map[string]interface{})
dimension["appid"] = viper.GetString("dba.bk_biz_id")
dimension["bk_biz_id"] = viper.GetString("dba.bk_biz_id")
dimension["bk_cloud_id"] = 0
dimension["cluster_domain"] = PartitionCron
Expand All @@ -70,6 +71,7 @@ func NewDeveloperEventDimension(serverIp string) map[string]interface{} {
// NewPartitionEventDimension 构建自定监控事件的维度,发送给业务的dba
func NewPartitionEventDimension(bkBizId int, bkCloudId int, domain string) map[string]interface{} {
dimension := make(map[string]interface{})
dimension["appid"] = bkBizId
dimension["bk_biz_id"] = bkBizId
dimension["bk_cloud_id"] = bkCloudId
dimension["cluster_domain"] = domain
Expand All @@ -78,7 +80,7 @@ func NewPartitionEventDimension(bkBizId int, bkCloudId int, domain string) map[s

// TestSendEvent 测试监控上报链路
func TestSendEvent(dataId int, token string, serviceHost string) error {
dimension := NewDeveloperEventDimension("0.0.0.0")
dimension := NewDeveloperEventDimension("127.0.0.1")
l, _ := time.LoadLocation("Local")

body := eventBody{
Expand All @@ -93,7 +95,7 @@ func TestSendEvent(dataId int, token string, serviceHost string) error {
"content": "test partition monitor",
},
commonData: commonData{
Target: "0.0.0.0",
Target: "127.0.0.1",
Timestamp: time.Now().In(l).UnixMilli(),
Dimension: dimension,
Metrics: nil,
Expand Down
1 change: 1 addition & 0 deletions dbm-services/mysql/db-partition/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func RegisterRouter(engine *gin.Engine) {
p.POST("/cron_start", handler.CronStart)
p.POST("/cron_entries", handler.CronEntries)
p.POST("/cron_stop", handler.CronStop)
p.POST("/init_monitor", handler.InitMonitor)
// 迁移分区配置
p.POST("/migrate_config", handler.MigrateConfig)
}
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func CreatePartitionTicket(check Checker, objects []PartitionObject, zoneOffset
if err != nil {
dimension := monitor.NewPartitionEventDimension(check.BkBizId, *check.BkCloudId, check.ImmuteDomain)
content := fmt.Sprintf("partition error. create ticket fail: %s", err.Error())
monitor.SendEvent(monitor.PartitionEvent, dimension, content, "0.0.0.0")
monitor.SendEvent(monitor.PartitionEvent, dimension, content, "127.0.0.1")
slog.Error("msg", fmt.Sprintf("create ticket fail: %v", ticket), err)
_ = AddLog(check.ConfigId, check.BkBizId, check.ClusterId, *check.BkCloudId,
0, check.ImmuteDomain, zone, date, scheduler,
Expand Down
Loading

0 comments on commit 07d480d

Please sign in to comment.