This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 3b7d6cdf6 fix(rmqctl): disclose the page a table view shows instead of 
presenting it as the whole list (#6726)
3b7d6cdf6 is described below

commit 3b7d6cdf60b1b35a0c4181e2788d285709355339
Author: bai mengxuan <[email protected]>
AuthorDate: Fri Nov 27 12:26:50 2026 +0800

    fix(rmqctl): disclose the page a table view shows instead of presenting it 
as the whole list (#6726)
---
 rmqctl/cmd/catalog_renderers.go      | 47 ++++++++++++++++++++++
 rmqctl/cmd/catalog_renderers_test.go | 76 ++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+)

diff --git a/rmqctl/cmd/catalog_renderers.go b/rmqctl/cmd/catalog_renderers.go
index 6dbbe1ce3..e1fd43aba 100644
--- a/rmqctl/cmd/catalog_renderers.go
+++ b/rmqctl/cmd/catalog_renderers.go
@@ -17,6 +17,7 @@
 package cmd
 
 import (
+       "encoding/json"
        "fmt"
        "io"
        "maps"
@@ -48,9 +49,55 @@ func renderTable(w, errOut io.Writer, tool toolcatalog.Tool, 
result any) error {
                        return err
                }
        }
+       if notice := pagedTableNotice(result, len(rows)); notice != "" {
+               _, err := fmt.Fprint(errOut, notice)
+               return err
+       }
        return nil
 }
 
+// pagedTableNotice reports the page a table view is showing. Every paged 
listing carries page,
+// pageSize and total, and the default page size is the server's 20, so a 
table that prints one page
+// must not read as the whole list. The note goes to stderr and leaves stdout 
to the rows, the way
+// the message tools' truncation warning does.
+func pagedTableNotice(result any, shown int) string {
+       payload, ok := result.(map[string]any)
+       if !ok {
+               return ""
+       }
+       page := payloadNumber(payload["page"])
+       pageSize := payloadNumber(payload["pageSize"])
+       total := payloadNumber(payload["total"])
+       if page < 1 || pageSize < 1 || total <= int64(shown) {
+               return ""
+       }
+       pages := (total + pageSize - 1) / pageSize
+       return fmt.Sprintf(
+               "WARNING: the table shows page %d of %d (%d of %d rows); pass 
--page/--page-size to see the rest, or --output json for the full payload.\n",
+               page, pages, shown, total)
+}
+
+// payloadNumber accepts the json.Number values the client decodes with 
UseNumber plus the plain
+// integers a future or stubbed payload may carry; anything else means the 
field is absent.
+func payloadNumber(value any) int64 {
+       switch typed := value.(type) {
+       case json.Number:
+               number, err := typed.Int64()
+               if err != nil {
+                       return 0
+               }
+               return number
+       case float64:
+               return int64(typed)
+       case int64:
+               return typed
+       case int:
+               return int64(typed)
+       default:
+               return 0
+       }
+}
+
 func tableRows(result any, dataKey string) ([]map[string]any, error) {
        if dataKey == "" {
                return output.MapsFromAny(result)
diff --git a/rmqctl/cmd/catalog_renderers_test.go 
b/rmqctl/cmd/catalog_renderers_test.go
index 83c064d5b..57a82fe97 100644
--- a/rmqctl/cmd/catalog_renderers_test.go
+++ b/rmqctl/cmd/catalog_renderers_test.go
@@ -208,6 +208,82 @@ func TestRenderTableWarningScopeTest(t *testing.T) {
        }
 }
 
+// A paged listing renders one page, and the payload always carries the page, 
its size and the total,
+// so the table view has to say which page it printed: `rmqctl audit list` 
used to show 20 rows of 180
+// with nothing to tell the two apart, while the message tools already 
disclose a bounded result.
+func TestPagedTableDisclosureTest(t *testing.T) {
+       pageRows := func(count int) []any {
+               rows := make([]any, 0, count)
+               for i := 0; i < count; i++ {
+                       rows = append(rows, map[string]any{"id": i + 1})
+               }
+               return rows
+       }
+       cases := []struct {
+               name    string
+               payload map[string]any
+               want    string
+       }{
+               {
+                       name:    "first_page_of_many",
+                       payload: map[string]any{"page": 1, "pageSize": 20, 
"total": 180, "items": pageRows(1)},
+                       want:    "WARNING: the table shows page 1 of 9 (1 of 
180 rows); pass --page/--page-size to see the rest, or --output json for the 
full payload.\n",
+               },
+               {
+                       name:    "last_page_still_short_of_the_total",
+                       payload: map[string]any{"page": 9, "pageSize": 20, 
"total": 180, "items": pageRows(20)},
+                       want:    "WARNING: the table shows page 9 of 9 (20 of 
180 rows); pass --page/--page-size to see the rest, or --output json for the 
full payload.\n",
+               },
+               {
+                       name:    "listing_that_fits_one_page",
+                       payload: map[string]any{"page": 1, "pageSize": 20, 
"total": 3, "items": pageRows(3)},
+                       want:    "",
+               },
+               {
+                       name:    "unpaged_payload",
+                       payload: map[string]any{"items": pageRows(2)},
+                       want:    "",
+               },
+       }
+       tools := []struct {
+               tool string
+               args []string
+       }{
+               {"rmq.audit.list", []string{"audit", "list"}},
+               {"rmq.acl.list", []string{"acl", "list"}},
+               {"rmq.message.query_dlq", []string{"message", "query-dlq"}},
+       }
+       for _, entry := range tools {
+               for _, tc := range cases {
+                       t.Run(entry.tool+"/"+tc.name, func(t *testing.T) {
+                               stdout, stderr, exitCode := 
executeCatalogResult(t, entry.tool, tc.payload, entry.args...)
+                               if exitCode != 0 {
+                                       t.Fatalf("exit code = %d, stderr = %q", 
exitCode, stderr)
+                               }
+                               if stderr != tc.want {
+                                       t.Fatalf("stderr = %q, want %q", 
stderr, tc.want)
+                               }
+                               if !bytes.Contains([]byte(stdout), 
[]byte("ID")) || bytes.Contains([]byte(stdout), []byte("WARNING")) {
+                                       t.Fatalf("stdout must carry only the 
table: %q", stdout)
+                               }
+                       })
+               }
+       }
+       // The structured formats carry the whole envelope, so they need no 
notice.
+       for _, format := range []string{"json", "yaml"} {
+               t.Run("structured_"+format, func(t *testing.T) {
+                       payload := map[string]any{"page": 1, "pageSize": 20, 
"total": 180, "items": pageRows(1)}
+                       stdout, stderr, exitCode := executeCatalogResult(t, 
"rmq.audit.list", payload, "audit", "list", "--output", format)
+                       if exitCode != 0 || stderr != "" {
+                               t.Fatalf("exit = %d, stderr = %q", exitCode, 
stderr)
+                       }
+                       if !bytes.Contains([]byte(stdout), []byte("180")) {
+                               t.Fatalf("structured output lost the total: 
%q", stdout)
+                       }
+               })
+       }
+}
+
 type catalogErrorWriter struct{ err error }
 
 func (w catalogErrorWriter) Write([]byte) (int, error) { return 0, w.err }

Reply via email to