Skip to content

Commit

Permalink
#830 custom template add string_code_map
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjiawei committed Oct 22, 2024
1 parent be3fdcb commit a91c61c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions monitor-server/api/v2/service/log_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func CheckLogMonitorRegExpMatch(c *gin.Context) {
middleware.ReturnValidateError(c, err.Error())
return
}
result := []*models.LogParamTemplateObj{}
var result []*models.LogParamTemplateObj
for _, v := range param.ParamList {
_, v.DemoMatchValue = db.CheckRegExpMatchPCRE(models.CheckRegExpParam{RegString: v.Regular, TestContext: param.DemoLog})
result = append(result, v)
Expand Down Expand Up @@ -985,7 +985,7 @@ func LogMonitorTemplateExport(c *gin.Context) {
middleware.ReturnHandleError(c, err.Error(), err)
return
}
resultData := []*models.LogMonitorTemplateDto{}
var resultData []*models.LogMonitorTemplateDto
for _, v := range param.GuidList {
templateObj, tmpErr := db.GetLogMonitorTemplate(v)
if tmpErr != nil {
Expand Down
19 changes: 10 additions & 9 deletions monitor-server/models/log_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ type LogMetricConfigDto struct {
}

type LogMetricStringMapTable struct {
Guid string `json:"guid" xorm:"guid"`
LogMetricConfig string `json:"log_metric_config" xorm:"log_metric_config"`
LogMetricGroup string `json:"log_metric_group" xorm:"log_metric_group"`
LogParamName string `json:"log_param_name" xorm:"log_param_name"`
ValueType string `json:"value_type" xorm:"value_type"`
SourceValue string `json:"source_value" xorm:"source_value"`
Regulative int `json:"regulative" xorm:"regulative"`
TargetValue string `json:"target_value" xorm:"target_value"`
UpdateTime string `json:"update_time" xorm:"update_time"`
Guid string `json:"guid" xorm:"guid"`
LogMetricConfig string `json:"log_metric_config" xorm:"log_metric_config"`
LogMetricGroup string `json:"log_metric_group" xorm:"log_metric_group"`
LogMonitorTemplate string `json:"log_monitor_template" xorm:"log_monitor_template"`
LogParamName string `json:"log_param_name" xorm:"log_param_name"`
ValueType string `json:"value_type" xorm:"value_type"`
SourceValue string `json:"source_value" xorm:"source_value"`
Regulative int `json:"regulative" xorm:"regulative"`
TargetValue string `json:"target_value" xorm:"target_value"`
UpdateTime string `json:"update_time" xorm:"update_time"`
}

type LogMetricEndpointRelTable struct {
Expand Down
2 changes: 1 addition & 1 deletion monitor-server/models/log_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (l *LogMetricTemplate) TransToLogMetric() (output *LogMetricConfigDto) {
type LogMonitorTemplateDto struct {
LogMonitorTemplate
CalcResultObj *CheckRegExpResult `json:"calc_result"`
ParamList []*LogParamTemplate `json:"param_list"`
ParamList []*LogParamTemplateObj `json:"param_list"`
MetricList []*LogMetricTemplate `json:"metric_list"`
Permission *LogMonitorTemplatePermission `json:"permission"`
LogMonitorTemplateVersion string `json:"log_monitor_template_version"`
Expand Down
33 changes: 31 additions & 2 deletions monitor-server/services/db/log_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func GetLogMonitorTemplate(logMonitorTemplateGuid string) (result *models.LogMon
}
logMonitorTemplateRow.CreateTimeString = logMonitorTemplateRow.CreateTime.Format(models.DatetimeFormat)
logMonitorTemplateRow.UpdateTimeString = logMonitorTemplateRow.UpdateTime.Format(models.DatetimeFormat)
result = &models.LogMonitorTemplateDto{LogMonitorTemplate: *logMonitorTemplateRow, CalcResultObj: &models.CheckRegExpResult{}, ParamList: []*models.LogParamTemplate{}, MetricList: []*models.LogMetricTemplate{}}
result = &models.LogMonitorTemplateDto{LogMonitorTemplate: *logMonitorTemplateRow, CalcResultObj: &models.CheckRegExpResult{}, ParamList: []*models.LogParamTemplateObj{}, MetricList: []*models.LogMetricTemplate{}}
result.LogMonitorTemplateVersion = logMonitorTemplateRow.UpdateTime.Format(models.DatetimeDigitFormat)
if result.CalcResult != "" {
if err = json.Unmarshal([]byte(result.CalcResult), result.CalcResultObj); err != nil {
Expand All @@ -73,8 +73,31 @@ func GetLogMonitorTemplate(logMonitorTemplateGuid string) (result *models.LogMon
err = fmt.Errorf("query log_param_template table fail,%s ", err.Error())
return
}
var logMetricStringMaps []*models.LogMetricStringMapTable
var stringCodeMap = make(map[string][]*models.LogMetricStringMapTable)
err = x.SQL("select * from log_metric_string_map where log_monitor_template=?", logMonitorTemplateGuid).Find(&logMetricStringMaps)
if err != nil {
err = fmt.Errorf("query log_metric_string_map table fail,%s ", err.Error())
return
}
for _, logMetricString := range logMetricStringMaps {
if arr, ok := stringCodeMap[logMetricString.LogParamName]; ok {
arr = append(arr, logMetricString)
} else {
stringCodeMap[logMetricString.LogParamName] = []*models.LogMetricStringMapTable{logMetricString}
}
}
for _, row := range logParamRows {
result.ParamList = append(result.ParamList, row)
if row == nil {
continue
}
logParamTemplateObj := &models.LogParamTemplateObj{
LogParamTemplate: *row,
}
if v, ok := stringCodeMap[row.Name]; ok {
logParamTemplateObj.StringMap = v
}
result.ParamList = append(result.ParamList, logParamTemplateObj)
}
var logMetricRows []*models.LogMetricTemplate
if logMetricRows, err = getLogMetricTemplateWithMonitor(logMonitorTemplateGuid); err != nil {
Expand Down Expand Up @@ -136,6 +159,12 @@ func getCreateLogMonitorTemplateActions(param *models.LogMonitorTemplateDto, ope
actions = append(actions, &Action{Sql: "insert into log_param_template(guid,log_monitor_template,name,display_name,json_key,regular,demo_match_value,create_user,update_user,create_time,update_time) values (?,?,?,?,?,?,?,?,?,?,?)", Param: []interface{}{
"lpt_" + logParamGuidList[i], param.Guid, logParamObj.Name, logParamObj.DisplayName, logParamObj.JsonKey, logParamObj.Regular, logParamObj.DemoMatchValue, operator, operator, nowTime, nowTime,
}})
tmpStringMapGuidList := guid.CreateGuidList(len(logParamObj.StringMap))
for stringMapIndex, stringMapObj := range logParamObj.StringMap {
actions = append(actions, &Action{Sql: "insert into log_metric_string_map(guid,log_monitor_template,log_param_name,value_type,source_value,regulative,target_value,update_time) values (?,?,?,?,?,?,?,?)", Param: []interface{}{
"lmsm_" + tmpStringMapGuidList[stringMapIndex], param.Guid, logParamObj.Name, stringMapObj.ValueType, stringMapObj.SourceValue, stringMapObj.Regulative, stringMapObj.TargetValue, nowTime.Format(models.DatetimeFormat),
}})
}
}
logMetricGuidList := guid.CreateGuidList(len(param.MetricList))
for i, logMetricObj := range param.MetricList {
Expand Down

0 comments on commit a91c61c

Please sign in to comment.