Skip to content

Commit

Permalink
256 feature request add channel name in generated file name (#340)
Browse files Browse the repository at this point in the history
* feat: fix property.json & log file pattern to include channel name

* feat: change to not use sjson/gjson to fix cannot add new property issue
  • Loading branch information
plutoless authored Oct 17, 2024
1 parent 9659896 commit 53fd561
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 40 deletions.
4 changes: 0 additions & 4 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ require (
github.com/gogf/gf v1.16.9
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/tidwall/gjson v1.17.1
github.com/tidwall/sjson v1.2.5
)

require (
Expand All @@ -34,8 +32,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
Expand Down
9 changes: 0 additions & 9 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
Expand Down
89 changes: 62 additions & 27 deletions server/internal/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/gogf/gf/crypto/gmd5"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

type HttpServer struct {
Expand Down Expand Up @@ -388,7 +386,13 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo
return
}

propertyJson := string(content)
// Unmarshal the JSON content into a map
var propertyJson map[string]interface{}
err = json.Unmarshal(content, &propertyJson)
if err != nil {
slog.Error("handlerStart unmarshal property.json failed", "err", err, "requestId", req.RequestId, logTag)
return
}

// Get graph name
graphName := req.GraphName
Expand All @@ -407,15 +411,24 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo
}
}

graph := fmt.Sprintf(`_ten.predefined_graphs.#(name=="%s")`, graphName)
// Locate the predefined graphs array
tenSection, ok := propertyJson["_ten"].(map[string]interface{})
if !ok {
slog.Error("Invalid format: _ten section missing", "requestId", req.RequestId, logTag)
return
}

// Get the array of graphs
graphs := gjson.Get(propertyJson, "_ten.predefined_graphs").Array()
predefinedGraphs, ok := tenSection["predefined_graphs"].([]interface{})
if !ok {
slog.Error("Invalid format: predefined_graphs missing or not an array", "requestId", req.RequestId, logTag)
return
}

// Create a new array for graphs that match the name
var newGraphs []gjson.Result
for _, graph := range graphs {
if graph.Get("name").String() == graphName {
// Filter the graph with the matching name
var newGraphs []interface{}
for _, graph := range predefinedGraphs {
graphMap, ok := graph.(map[string]interface{})
if ok && graphMap["name"] == graphName {
newGraphs = append(newGraphs, graph)
}
}
Expand All @@ -426,45 +439,67 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo
return
}

// Set the array of graphs directly using sjson.Set
graphData := make([]interface{}, len(newGraphs))
for i, graph := range newGraphs {
graphData[i] = graph.Value() // Convert gjson.Result to interface{}
}

// Replace the predefined_graphs array with the filtered array
propertyJson, _ = sjson.Set(propertyJson, "_ten.predefined_graphs", graphData)
tenSection["predefined_graphs"] = newGraphs

// Automatically start on launch
propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.auto_start`, graph), true)
for _, graph := range newGraphs {
graphMap, _ := graph.(map[string]interface{})
graphMap["auto_start"] = true
}

// Set additional properties to property.json
for extensionName, props := range req.Properties {
if extKey := extensionName; extKey != "" {
if extensionName != "" {
for prop, val := range props {
// Construct the path
path := fmt.Sprintf(`%s.nodes.#(name=="%s").property.%s`, graph, extKey, prop)
propertyJson, err = sjson.Set(propertyJson, path, val)
if err != nil {
slog.Error("handlerStart set property failed", "err", err, "graph", graphName, "extensionName", extensionName, "prop", prop, "val", val, "requestId", req.RequestId, logTag)
// Construct the path in the nested graph structure
for _, graph := range newGraphs {
graphMap, _ := graph.(map[string]interface{})
nodes, _ := graphMap["nodes"].([]interface{})
for _, node := range nodes {
nodeMap, _ := node.(map[string]interface{})
if nodeMap["name"] == extensionName {
properties := nodeMap["property"].(map[string]interface{})
properties[prop] = val
}
}
}
}
}
}

// Set start parameters to property.json
for key, props := range startPropMap {
if val := getFieldValue(req, key); val != "" {
val := getFieldValue(req, key)
if val != "" {
for _, prop := range props {
propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.nodes.#(name=="%s").property.%s`, graph, prop.ExtensionName, prop.Property), val)
// Set each start parameter to the appropriate graph and property
for _, graph := range newGraphs {
graphMap, _ := graph.(map[string]interface{})
nodes, _ := graphMap["nodes"].([]interface{})
for _, node := range nodes {
nodeMap, _ := node.(map[string]interface{})
if nodeMap["name"] == prop.ExtensionName {
properties := nodeMap["property"].(map[string]interface{})
properties[prop.Property] = val
}
}
}
}
}
}

// Marshal the modified JSON back to a string
modifiedPropertyJson, err := json.MarshalIndent(propertyJson, "", " ")
if err != nil {
slog.Error("handlerStart marshal modified JSON failed", "err", err, "requestId", req.RequestId, logTag)
return
}

ts := time.Now().Format("20060102_150405_000")
propertyJsonFile = fmt.Sprintf("%s/property-%s-%s.json", s.config.LogPath, url.QueryEscape(req.ChannelName), ts)
logFile = fmt.Sprintf("%s/app-%s-%s.log", s.config.LogPath, url.QueryEscape(req.ChannelName), ts)
os.WriteFile(propertyJsonFile, []byte(propertyJson), 0644)
os.WriteFile(propertyJsonFile, []byte(modifiedPropertyJson), 0644)

return
}
Expand Down

0 comments on commit 53fd561

Please sign in to comment.