Skip to content

Commit

Permalink
fix parseRule
Browse files Browse the repository at this point in the history
  • Loading branch information
fuwx295 committed Oct 15, 2024
1 parent 6863af5 commit 694f525
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
10 changes: 8 additions & 2 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4374,7 +4374,10 @@ const docTemplate = `{
"type": "string"
},
"routeRule": {
"type": "string"
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
Expand Down Expand Up @@ -5148,7 +5151,10 @@ const docTemplate = `{
"type": "string"
},
"routeRule": {
"type": "string"
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"tableName": {
"type": "string"
Expand Down
10 changes: 8 additions & 2 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4366,7 +4366,10 @@
"type": "string"
},
"routeRule": {
"type": "string"
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
Expand Down Expand Up @@ -5140,7 +5143,10 @@
"type": "string"
},
"routeRule": {
"type": "string"
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"tableName": {
"type": "string"
Expand Down
8 changes: 6 additions & 2 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,9 @@ definitions:
parseRule:
type: string
routeRule:
type: string
additionalProperties:
type: string
type: object
type: object
request.AddOtherTableRequest:
properties:
Expand Down Expand Up @@ -1143,7 +1145,9 @@ definitions:
parseRule:
type: string
routeRule:
type: string
additionalProperties:
type: string
type: object
tableName:
type: string
type: object
Expand Down
20 changes: 10 additions & 10 deletions backend/pkg/model/request/log_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ type QueryLogParseRequest struct {
}

type UpdateLogParseRequest struct {
DataBase string `json:"dataBase"`
TableName string `json:"tableName"`
ParseName string `json:"parseName"`
RouteRule string `json:"routeRule"`
ParseRule string `json:"parseRule"`
DataBase string `json:"dataBase"`
TableName string `json:"tableName"`
ParseName string `json:"parseName"`
RouteRule map[string]string `json:"routeRule"`
ParseRule string `json:"parseRule"`
}

type AddLogParseRequest struct {
ParseName string `json:"parseName"`
ParseInfo string `json:"parseInfo"`
RouteRule string `json:"routeRule"`
ParseRule string `json:"parseRule"`
LogTable LogTable `json:"logTable"`
ParseName string `json:"parseName"`
ParseInfo string `json:"parseInfo"`
RouteRule map[string]string `json:"routeRule"`
ParseRule string `json:"parseRule"`
LogTable LogTable `json:"logTable"`
}

type LogTable struct {
Expand Down
15 changes: 12 additions & 3 deletions backend/pkg/services/log/service_add_log_parse_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"encoding/json"
"fmt"

"github.com/CloudDetail/apo/backend/pkg/model/request"
"github.com/CloudDetail/apo/backend/pkg/model/response"
Expand All @@ -10,6 +11,14 @@ import (
"gopkg.in/yaml.v3"
)

func getRouteRule(routeMap map[string]string) string {
var res string
for k, v := range routeMap {
res += fmt.Sprintf(".\"%s\" == \"%s\"\n", k, v)
}
return res
}

func (s *service) AddLogParseRule(req *request.AddLogParseRequest) (*response.LogParseResponse, error) {
// 先去建表
logReq := &request.LogTableRequest{
Expand All @@ -24,7 +33,7 @@ func (s *service) AddLogParseRule(req *request.AddLogParseRequest) (*response.Lo
res := &response.LogParseResponse{
ParseName: req.ParseName,
ParseRule: req.ParseRule,
RouteRule: req.RouteRule,
RouteRule: getRouteRule(req.RouteRule),
}
data, err := s.k8sApi.GetVectorConfigFile()
if err != nil {
Expand All @@ -39,7 +48,7 @@ func (s *service) AddLogParseRule(req *request.AddLogParseRequest) (*response.Lo
ParseName: req.ParseName,
TableName: req.ParseName,
ParseRule: req.ParseRule,
RouteRule: req.RouteRule,
RouteRule: getRouteRule(req.RouteRule),
}
newData, err := p.AddParseRule(vectorCfg)
if err != nil {
Expand All @@ -65,7 +74,7 @@ func (s *service) AddLogParseRule(req *request.AddLogParseRequest) (*response.Lo
ParseInfo: req.ParseInfo,
ParseName: req.ParseName,
ParseRule: req.ParseRule,
RouteRule: req.RouteRule,
RouteRule: getRouteRule(req.RouteRule),
Table: req.ParseName,
DataBase: logReq.DataBase,
Cluster: logReq.Cluster,
Expand Down
6 changes: 3 additions & 3 deletions backend/pkg/services/log/service_update_log_parse_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (s *service) UpdateLogParseRule(req *request.UpdateLogParseRequest) (*respo
res := &response.LogParseResponse{
ParseName: req.ParseName,
ParseRule: req.ParseRule,
RouteRule: req.RouteRule,
RouteRule: getRouteRule(req.RouteRule),
}
data, err := s.k8sApi.GetVectorConfigFile()
if err != nil {
Expand All @@ -27,7 +27,7 @@ func (s *service) UpdateLogParseRule(req *request.UpdateLogParseRequest) (*respo
p := vector.ParseInfo{
ParseName: req.ParseName,
ParseRule: req.ParseRule,
RouteRule: req.RouteRule,
RouteRule: getRouteRule(req.RouteRule),
}
newData, err := p.UpdateParseRule(vectorCfg)
if err != nil {
Expand All @@ -41,7 +41,7 @@ func (s *service) UpdateLogParseRule(req *request.UpdateLogParseRequest) (*respo
// 更新sqlite表信息
log := database.LogTableInfo{
ParseRule: req.ParseRule,
RouteRule: req.RouteRule,
RouteRule: getRouteRule(req.RouteRule),
Table: req.TableName,
DataBase: req.DataBase,
}
Expand Down
4 changes: 2 additions & 2 deletions backend/pkg/services/log/vector/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (p *ParseInfo) AddParseRule(config VectorConfig) ([]byte, error) {

_, ok = config.Transforms["parse_"+p.ParseName].(map[string]interface{})
if ok {
return nil, errors.New("parseName found")
return nil, errors.New("规则解析名已存在,请确保唯一")
} else {
new_transform := map[string]interface{}{
"type": "remap",
Expand Down Expand Up @@ -90,7 +90,7 @@ func (p *ParseInfo) DeleteParseRule(config VectorConfig) ([]byte, error) {
delete(config.Sinks, "to_"+p.ParseName)
updatedData, err := yaml.Marshal(&config)
if err != nil {
return nil, errors.New("marshal failed")
return nil, errors.New("配置文件更新出错")
}
return updatedData, nil
}

0 comments on commit 694f525

Please sign in to comment.