Skip to content

The only tool you need to know what is happening and how to fix it.

cli: send order-by values as the columns the backend sorts on

narrow-adventured620551e3295
diff --git a/cli/pkg/client/endpoints.go b/cli/pkg/client/endpoints.go
index 372fbb98..5e2128a8 100644
--- a/cli/pkg/client/endpoints.go
+++ b/cli/pkg/client/endpoints.go
@@ -32,7 +32,8 @@ type ListEndpointsRequest struct {
Search string `json:"search,omitempty"`
}
-// MarshalJSON expands TimeRange into top-level fromDate/toDate.
+// MarshalJSON expands TimeRange into top-level fromDate/toDate and sends
+// OrderBy as the column name the backend sorts on.
func (r ListEndpointsRequest) MarshalJSON() ([]byte, error) {
type alias ListEndpointsRequest
wire := struct {
@@ -40,6 +41,7 @@ func (r ListEndpointsRequest) MarshalJSON() ([]byte, error) {
ToDate time.Time `json:"toDate"`
alias
}{r.TimeRange.From, r.TimeRange.To, alias(r)}
+ wire.OrderBy = orderByColumn(r.OrderBy)
return jsonMarshalNoHTMLEscape(wire)
}
diff --git a/cli/pkg/client/endpoints_test.go b/cli/pkg/client/endpoints_test.go
index d115d520..c28ce33f 100644
--- a/cli/pkg/client/endpoints_test.go
+++ b/cli/pkg/client/endpoints_test.go
@@ -57,6 +57,32 @@ func TestListEndpoints_callsGroupedRoute(t *testing.T) {
}
}
+// TestListRequests_sendOrderByColumns: the backend sorts on column names and
+// falls back to its default order for anything else, so the camelCase flag
+// values must reach it as those names.
+func TestListRequests_sendOrderByColumns(t *testing.T) {
+ wire := func(v any) string {
+ raw, err := json.Marshal(v)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var body map[string]any
+ _ = json.Unmarshal(raw, &body)
+ s, _ := body["orderBy"].(string)
+ return s
+ }
+ for in, want := range map[string]string{"impact": "impact", "count": "count", "p95": "p95_duration", "lastSeen": "last_seen", "": ""} {
+ if got := wire(ListEndpointsRequest{OrderBy: in}); got != want {
+ t.Errorf("endpoints --order-by %q sent %q, want %q", in, got, want)
+ }
+ }
+ for in, want := range map[string]string{"count": "count", "lastSeen": "last_seen", "firstSeen": "first_seen"} {
+ if got := wire(ListExceptionsRequest{OrderBy: in}); got != want {
+ t.Errorf("exceptions --order-by %q sent %q, want %q", in, got, want)
+ }
+ }
+}
+
func TestGetEndpointChart_callsChartRoute(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/endpoints/chart" {
diff --git a/cli/pkg/client/enums.go b/cli/pkg/client/enums.go
index 9360e0e7..f32ad8ff 100644
--- a/cli/pkg/client/enums.go
+++ b/cli/pkg/client/enums.go
@@ -22,3 +22,20 @@ var (
// endpoints, never from metrics/query.
MetricAggregationsExact = []string{"avg", "sum", "count", "min", "max"}
)
+
+// orderByColumns maps the camelCase order-by values above to the column names
+// the backend sorts on. The backend falls back to its default order for any
+// name it does not know, so sending "p95" or "lastSeen" as is silently did
+// not sort.
+var orderByColumns = map[string]string{
+ "p95": "p95_duration",
+ "lastSeen": "last_seen",
+ "firstSeen": "first_seen",
+}
+
+func orderByColumn(v string) string {
+ if c, ok := orderByColumns[v]; ok {
+ return c
+ }
+ return v
+}
diff --git a/cli/pkg/client/exceptions.go b/cli/pkg/client/exceptions.go
index 663f415e..76fb5326 100644
--- a/cli/pkg/client/exceptions.go
+++ b/cli/pkg/client/exceptions.go
@@ -41,7 +41,8 @@ type ListExceptionsRequest struct {
}
// MarshalJSON expands TimeRange.From / TimeRange.To into top-level fromDate /
-// toDate so the wire shape matches Traceway's ExceptionSearchRequest.
+// toDate so the wire shape matches Traceway's ExceptionSearchRequest, and
+// sends OrderBy as the column name the backend sorts on.
func (r ListExceptionsRequest) MarshalJSON() ([]byte, error) {
type alias ListExceptionsRequest
wire := struct {
@@ -53,6 +54,7 @@ func (r ListExceptionsRequest) MarshalJSON() ([]byte, error) {
ToDate: r.TimeRange.To,
alias: alias(r),
}
+ wire.OrderBy = orderByColumn(r.OrderBy)
return jsonMarshalNoHTMLEscape(wire)
}
diff --git a/cli/pkg/client/metrics.go b/cli/pkg/client/metrics.go
index 2d7251b9..fa60d06c 100644
--- a/cli/pkg/client/metrics.go
+++ b/cli/pkg/client/metrics.go
@@ -43,7 +43,7 @@ type TimeSeriesPoint struct {
}
// MetricQueryResult is one query's results, optionally grouped by tag.
-// The map key is the group label ("all" if no GroupBy was specified).
+// The map key is the group label ("__all__" if no GroupBy was specified).
type MetricQueryResult struct {
Name string `json:"name"`
Unit string `json:"unit"`
diff --git a/cli/pkg/mcpserver/server_test.go b/cli/pkg/mcpserver/server_test.go
index 42bda145..de15d8d0 100644
--- a/cli/pkg/mcpserver/server_test.go
+++ b/cli/pkg/mcpserver/server_test.go
@@ -187,7 +187,7 @@ func TestListExceptions_wireShapeAndDefaults(t *testing.T) {
if gotBody["fromDate"] == nil || gotBody["toDate"] == nil {
t.Errorf("body missing fromDate/toDate: %v", gotBody)
}
- if gotBody["orderBy"] != "lastSeen" || gotBody["searchType"] != "text" {
+ if gotBody["orderBy"] != "last_seen" || gotBody["searchType"] != "text" {
t.Errorf("defaults not applied: %v", gotBody)
}
page := gotBody["pagination"].(map[string]any)