-
Notifications
You must be signed in to change notification settings - Fork 148
Feat/record proto #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/record proto #1091
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
773dbfc
feat: record proto
SAKURA-CAT 2ae1de1
chore: update workflow
SAKURA-CAT 485ae66
refactor: record proto
SAKURA-CAT 1adcf3e
chore: file header
Kaikaikaifang 5e112f7
fix: yRange
Kaikaikaifang 5d74bb2
fix: record
SAKURA-CAT 5e1eac9
refactor: proto
SAKURA-CAT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package api | ||
|
||
// @Title api.go | ||
// @Description SwanLab API intended for requests to the SwanLab-Server. | ||
// @Description SwanLab API intended for requests to the SwanLab Server. | ||
// @Create cunyue 2025/6/10 13:00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// @Title send.go | ||
// @Description parse the proto message to json | ||
// @Create cunyue 2025/6/12 18:49 | ||
|
||
package api | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
|
||
"github.com/SwanHubX/SwanLab/core/pkg/pb" | ||
) | ||
|
||
// Some constants for the parser. | ||
const ( | ||
minRangeLen = 2 | ||
) | ||
|
||
// Parser provides methods to parse proto messages to JSON sent to the SwanLab Server. | ||
type Parser struct { | ||
} | ||
|
||
// convertRange will convert the YRange from a proto message (like ["0", "None"]) to a JSON-compatible format(like [0, null]). | ||
// We expect the input to be a slice of strings with 2 elements, any other length will return an empty slice. | ||
func (p *Parser) convertRange(r []string) []interface{} { | ||
var left, right interface{} = nil, nil | ||
// must be at least 2 elements | ||
if len(r) != minRangeLen { | ||
return []interface{}{} | ||
} | ||
if val, err := strconv.Atoi(r[0]); err == nil { | ||
left = val | ||
} | ||
if val, err := strconv.Atoi(r[1]); err == nil { | ||
right = val | ||
} | ||
return []interface{}{left, right} | ||
} | ||
|
||
// validateColumnClass will validate the columnClass. | ||
func (p *Parser) validateColumnClass(record *pb.ColumnRecord) (string, error) { | ||
var class string | ||
switch record.GetColumnClass() { | ||
SAKURA-CAT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
case pb.ColumnRecord_COL_CUSTOM: | ||
class = "CUSTOM" | ||
case pb.ColumnRecord_COL_SYSTEM: | ||
class = "SYSTEM" | ||
default: | ||
return "", errors.New("invalid column class: " + record.GetColumnClass().String()) | ||
} | ||
return class, nil | ||
} | ||
|
||
// validateColumnType will validate the columnType. | ||
func (p *Parser) validateColumnType(record *pb.ColumnRecord) (string, error) { | ||
SAKURA-CAT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
switch record.GetColumnType() { | ||
case pb.ColumnRecord_COL_FLOAT: | ||
return "FLOAT", nil | ||
case pb.ColumnRecord_COL_IMAGE: | ||
return "IMAGE", nil | ||
case pb.ColumnRecord_COL_AUDIO: | ||
return "AUDIO", nil | ||
case pb.ColumnRecord_COL_TEXT: | ||
return "TEXT", nil | ||
case pb.ColumnRecord_COL_OBJECT3D: | ||
return "OBJECT3D", nil | ||
case pb.ColumnRecord_COL_MOLECULE: | ||
return "MOLECULE", nil | ||
case pb.ColumnRecord_COL_ECHARTS: | ||
return "ECHARTS", nil | ||
default: | ||
return "", errors.New("invalid column type: " + record.GetColumnType().String()) | ||
} | ||
} | ||
|
||
// validateSectionType will validate the sectionType. | ||
func (p *Parser) validateSectionType(record *pb.ColumnRecord) (string, error) { | ||
var sectionType string | ||
switch record.GetSectionType() { | ||
case pb.ColumnRecord_SEC_CUSTOM: | ||
sectionType = "CUSTOM" | ||
case pb.ColumnRecord_SEC_SYSTEM: | ||
sectionType = "SYSTEM" | ||
case pb.ColumnRecord_SEC_PINNED: | ||
sectionType = "PINNED" | ||
case pb.ColumnRecord_SEC_HIDDEN: | ||
sectionType = "HIDDEN" | ||
case pb.ColumnRecord_SEC_PUBLIC: | ||
sectionType = "PUBLIC" | ||
default: | ||
return "", errors.New("invalid section type: " + record.GetSectionType().String()) | ||
} | ||
return sectionType, nil | ||
} | ||
|
||
type ColumnDTO struct { | ||
Class string `json:"class"` | ||
Type string `json:"type"` | ||
Key string `json:"key"` | ||
Name string `json:"name,omitempty"` | ||
Error interface{} `json:"error,omitempty"` | ||
SectionName string `json:"section_name,omitempty"` | ||
SectionType string `json:"section_type,omitempty"` | ||
// allow [0, 100] or [0, null] for y_range | ||
YRange []interface{} `json:"y_range,omitempty"` | ||
ChartIndex string `json:"chart_index,omitempty"` | ||
ChartName string `json:"chart_name,omitempty"` | ||
MetricName string `json:"metric_name,omitempty"` | ||
MetricColor []string `json:"metric_color,omitempty"` | ||
} | ||
|
||
// ParseColumnRecord parses a ColumnRecord proto message into a map[string]interface{} for JSON serialization. | ||
// Attention: y_range should be serialized to [0, null] in JSON, not ["0", "None"]. | ||
func (p *Parser) ParseColumnRecord(record *pb.ColumnRecord) (ColumnDTO, error) { | ||
// 1. parse the y_range, if error, yRange will be nil | ||
yRange := p.convertRange(record.GetChartYRange()) | ||
// 2. parse the enum type | ||
// 2.1 column class | ||
class, err := p.validateColumnClass(record) | ||
if err != nil { | ||
return ColumnDTO{}, err | ||
} | ||
// 2.2 column type | ||
columnType, err := p.validateColumnType(record) | ||
if err != nil { | ||
return ColumnDTO{}, err | ||
} | ||
// 2.3 section type | ||
sectionType, err := p.validateSectionType(record) | ||
if err != nil { | ||
return ColumnDTO{}, err | ||
} | ||
// 4. check if the key is empty, if so, return an error | ||
key := record.GetColumnKey() | ||
if key == "" { | ||
return ColumnDTO{}, errors.New("column key cannot be EMPTY") | ||
} | ||
// 5. check the metric color length, if it is not empty, it must be 2 elements | ||
if len(record.GetMetricColor()) > 0 && len(record.GetMetricColor()) != 2 { | ||
return ColumnDTO{}, errors.New("metric color must be empty or have exactly 2 elements") | ||
} | ||
return ColumnDTO{ | ||
Class: class, | ||
Type: columnType, | ||
Key: record.GetColumnKey(), | ||
Name: record.GetColumnName(), | ||
Error: record.GetColumnError(), | ||
SectionName: record.GetSectionName(), | ||
SectionType: sectionType, | ||
YRange: yRange, | ||
ChartIndex: record.GetChartIndex(), | ||
ChartName: record.GetChartName(), | ||
MetricName: record.GetMetricName(), | ||
MetricColor: record.GetMetricColor(), | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// @Title parse_test.go | ||
// @Description test the parse function | ||
// @Create cunyue 2025/6/12 18:50 | ||
|
||
package api_test | ||
|
||
import ( | ||
"encoding/json" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/SwanHubX/SwanLab/core/internal/api" | ||
"github.com/SwanHubX/SwanLab/core/pkg/pb" | ||
) | ||
|
||
// TestRangeSerialization tests the ParseColumnRecord function of the Parser. | ||
// Just a simple test to ensure that the yRange can be parsed correctly. | ||
func TestRangeSerialization(t *testing.T) { | ||
// Define a test case with a valid yRange | ||
tests := []struct { | ||
name string | ||
input []string | ||
expected []interface{} | ||
yRangeStr string | ||
}{ | ||
{ | ||
name: "Valid yRange", | ||
input: []string{"1", "2"}, | ||
expected: []interface{}{1, 2}, | ||
yRangeStr: "[1,2]", | ||
}, | ||
{ | ||
name: "Too many values in yRange", | ||
input: []string{"1", "2", "3"}, | ||
expected: []interface{}{}, | ||
yRangeStr: "[]", | ||
}, | ||
{ | ||
name: "Left is none", | ||
input: []string{"None", "2"}, | ||
expected: []interface{}{nil, 2}, | ||
yRangeStr: "[null,2]", | ||
}, | ||
{ | ||
name: "Right is none", | ||
input: []string{"1", "None"}, | ||
expected: []interface{}{1, nil}, | ||
yRangeStr: "[1,null]", | ||
}, | ||
{ | ||
name: "Invalid yRange", | ||
input: []string{"x", "invalid"}, | ||
expected: []interface{}{nil, nil}, | ||
}, | ||
} | ||
parser := api.Parser{} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
record := &pb.ColumnRecord{ | ||
ColumnKey: "test-key", | ||
ColumnClass: pb.ColumnRecord_COL_CUSTOM, | ||
ColumnType: pb.ColumnRecord_COL_ECHARTS, | ||
SectionName: "", | ||
SectionType: pb.ColumnRecord_SEC_PUBLIC, | ||
ChartName: "test-chart-name", | ||
ChartYRange: tt.input, | ||
ChartIndex: "test-chart-index" + string(rune(rand.Intn(1000))), | ||
MetricName: "test-metric-name", | ||
MetricColor: []string{"#FF0000", "#0000"}, | ||
} | ||
// Call the ParseColumnRecord function with the test input | ||
result, err := parser.ParseColumnRecord(record) | ||
SAKURA-CAT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if err != nil { | ||
t.Errorf("Parse error: %s", err) | ||
return | ||
} | ||
// Check base fields | ||
assert.Equal(t, record.GetColumnKey(), result.Key) | ||
assert.Empty(t, result.Name) | ||
assert.Equal(t, "CUSTOM", result.Class) | ||
assert.Equal(t, "ECHARTS", result.Type) | ||
assert.Empty(t, result.SectionName) | ||
assert.Equal(t, "PUBLIC", result.SectionType) | ||
assert.Equal(t, record.GetChartName(), result.ChartName) | ||
assert.Equal(t, record.GetChartIndex(), result.ChartIndex) | ||
assert.Equal(t, record.GetMetricName(), result.MetricName) | ||
assert.Equal(t, record.GetMetricColor()[0], result.MetricColor[0]) | ||
assert.Equal(t, record.GetMetricColor()[1], result.MetricColor[1]) | ||
// Check if the yRange is parsed correctly | ||
if len(result.YRange) != len(tt.expected) { | ||
t.Errorf("Expected yRange length %d, got %d", len(tt.expected), len(result.YRange)) | ||
} | ||
// check if the yRange values are equal | ||
if len(tt.expected) == 2 { | ||
assert.Equal(t, tt.expected[0], result.YRange[0], "Left value mismatch") | ||
assert.Equal(t, tt.expected[1], result.YRange[1], "Right value mismatch") | ||
} else { | ||
assert.Empty(t, result.YRange, "Expected empty yRange for invalid input") | ||
} | ||
// JSON serialization, check if it can be serialized without error | ||
// Also check the yRange is serialized correctly | ||
jsonData, err := json.Marshal(result) | ||
require.NoError(t, err) | ||
jsonStr := string(jsonData) | ||
if len(tt.expected) == 2 { | ||
require.Contains(t, jsonStr, tt.yRangeStr, "Expected yRange to be serialized correctly") | ||
} else { | ||
require.NotContains(t, "y_range", jsonStr, "Expected yRange to be empty for invalid input") | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.