From c5d2f5732ed677532720bcd736b27277c5053f85 Mon Sep 17 00:00:00 2001 From: Robert Stam Date: Wed, 25 Nov 2026 13:45:40 +0000 Subject: [PATCH] feat(serverless): add --order-by to space metrics summary Expose the order_by field of GetSpaceSummaryMetrics (public API #571): --order-by name|points|storage. points and storage list the largest collections first, so --page-size N gives the top N without fetching every page. It can't be combined with --totals-only, which returns no items. Co-Authored-By: Claude Opus 5.5 --- ...qcloud_serverless_space_metrics_summary.md | 10 +++ .../cmd/serverless/space/metrics_helpers.go | 17 ++++ .../cmd/serverless/space/metrics_summary.go | 22 +++++ .../serverless/space/metrics_summary_test.go | 81 +++++++++++++++++++ 4 files changed, 130 insertions(+) diff --git a/docs/reference/qcloud_serverless_space_metrics_summary.md b/docs/reference/qcloud_serverless_space_metrics_summary.md index b64182b..8ead579 100644 --- a/docs/reference/qcloud_serverless_space_metrics_summary.md +++ b/docs/reference/qcloud_serverless_space_metrics_summary.md @@ -18,6 +18,12 @@ matching collections are shown as well; they don't depend on pagination. The search latency total is weighted by search rate. Use --totals-only to fetch only the quota and the totals, without the per-collection table. +Collections are ordered by name. Use --order-by points or --order-by storage to +list the largest collections first; together with --page-size this shows the +top collections without fetching every page. Ties are ordered by name. Pages +are not stable while the metrics change, so a later page may skip or repeat a +collection whose value moved between calls. + By default, all collections are fetched automatically across multiple pages. Use --page-size and --page-token for manual pagination. @@ -37,6 +43,9 @@ qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --c # Only show collections whose name contains "staging" qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --collection-contains staging +# Show the 5 collections that use the most storage +qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --order-by storage --page-size 5 + # Only show the space-level totals qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --totals-only @@ -50,6 +59,7 @@ qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --j --collection string Only include the collection with this exact name --collection-contains string Only include collections whose name contains this substring -h, --help[=true|false] help for summary + --order-by string Order of the collections: name, points or storage (largest first) (default "name") --page-size int32 Maximum number of collections to return per page (manual pagination mode) --page-token string Page token from a previous response to resume from (manual pagination mode) --totals-only[=true|false] Only fetch the quota and the totals over all matching collections, without per-collection metrics diff --git a/internal/cmd/serverless/space/metrics_helpers.go b/internal/cmd/serverless/space/metrics_helpers.go index c0dc6ad..bbad13a 100644 --- a/internal/cmd/serverless/space/metrics_helpers.go +++ b/internal/cmd/serverless/space/metrics_helpers.go @@ -110,6 +110,23 @@ func printQuotaSnapshot(w io.Writer, q *serverlessmonitoringv1.SpaceQuotaSnapsho fmt.Fprintln(w) } +// metricsOrderValues are the accepted --order-by values, in completion order. +var metricsOrderValues = []string{"name", "points", "storage"} + +// parseMetricsOrder maps the --order-by flag value to the proto enum. +func parseMetricsOrder(s string) (serverlessmonitoringv1.SpaceCollectionMetricsOrder, error) { + switch s { + case "name": + return serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_UNSPECIFIED, nil + case "points": + return serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_POINT_COUNT_DESC, nil + case "storage": + return serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_USED_STORAGE_BYTES_DESC, nil + default: + return 0, fmt.Errorf("invalid --order-by %q: must be one of name, points, storage", s) + } +} + // parseAggregator maps the --aggregator flag value to the proto enum. func parseAggregator(s string) (serverlessmonitoringv1.Aggregator, error) { switch s { diff --git a/internal/cmd/serverless/space/metrics_summary.go b/internal/cmd/serverless/space/metrics_summary.go index 4269684..e05f9ca 100644 --- a/internal/cmd/serverless/space/metrics_summary.go +++ b/internal/cmd/serverless/space/metrics_summary.go @@ -35,6 +35,12 @@ matching collections are shown as well; they don't depend on pagination. The search latency total is weighted by search rate. Use --totals-only to fetch only the quota and the totals, without the per-collection table. +Collections are ordered by name. Use --order-by points or --order-by storage to +list the largest collections first; together with --page-size this shows the +top collections without fetching every page. Ties are ordered by name. Pages +are not stable while the metrics change, so a later page may skip or repeat a +collection whose value moved between calls. + By default, all collections are fetched automatically across multiple pages. Use --page-size and --page-token for manual pagination.`, Example: `# Show an overview of all collections in a space @@ -46,6 +52,9 @@ qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --c # Only show collections whose name contains "staging" qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --collection-contains staging +# Show the 5 collections that use the most storage +qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --order-by storage --page-size 5 + # Only show the space-level totals qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --totals-only @@ -58,6 +67,15 @@ qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --j return nil, err } + orderBy := serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_UNSPECIFIED + if cmd.Flags().Changed("order-by") { + v, _ := cmd.Flags().GetString("order-by") + orderBy, err = parseMetricsOrder(v) + if err != nil { + return nil, err + } + } + ctx := cmd.Context() client, err := s.Client(ctx) if err != nil { @@ -75,6 +93,7 @@ qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --j AccountId: accountID, SpaceId: args[0], TotalsOnly: totalsOnly, + OrderBy: orderBy, PageSize: pageSize, PageToken: pageToken, } @@ -194,6 +213,9 @@ qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --j addCollectionFilterFlags(cmd, false) util.AddPaginationFlags(cmd, "collections") addTotalsOnlyFlag(cmd) + cmd.Flags().String("order-by", "name", "Order of the collections: name, points or storage (largest first)") + cmd.MarkFlagsMutuallyExclusive("order-by", "totals-only") + _ = cmd.RegisterFlagCompletionFunc("order-by", cobra.FixedCompletions(metricsOrderValues, cobra.ShellCompDirectiveNoFileComp)) return cmd } diff --git a/internal/cmd/serverless/space/metrics_summary_test.go b/internal/cmd/serverless/space/metrics_summary_test.go index c1c1d78..05550a2 100644 --- a/internal/cmd/serverless/space/metrics_summary_test.go +++ b/internal/cmd/serverless/space/metrics_summary_test.go @@ -3,6 +3,7 @@ package space_test import ( "context" "encoding/json" + "strings" "testing" "time" @@ -423,3 +424,83 @@ func TestSpaceMetricsSummary_TotalsOnlyWithPagination(t *testing.T) { }) } } + +func TestSpaceMetricsSummary_OrderBy(t *testing.T) { + tests := []struct { + value string + want serverlessmonitoringv1.SpaceCollectionMetricsOrder + }{ + {"name", serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_UNSPECIFIED}, + {"points", serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_POINT_COUNT_DESC}, + {"storage", serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_USED_STORAGE_BYTES_DESC}, + } + for _, tt := range tests { + t.Run(tt.value, func(t *testing.T) { + env := testutil.NewTestEnv(t) + env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls.Returns(sampleSummary(), nil) + + _, _, err := testutil.Exec(t, env, "serverless", "space", "metrics", "summary", "space-abc", "--order-by", tt.value) + require.NoError(t, err) + + req, ok := env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls.Last() + require.True(t, ok) + assert.Equal(t, tt.want, req.GetOrderBy()) + }) + } +} + +func TestSpaceMetricsSummary_OrderByDefault(t *testing.T) { + env := testutil.NewTestEnv(t) + env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls.Returns(sampleSummary(), nil) + + _, _, err := testutil.Exec(t, env, "serverless", "space", "metrics", "summary", "space-abc") + require.NoError(t, err) + + req, ok := env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls.Last() + require.True(t, ok) + assert.Equal(t, serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_UNSPECIFIED, req.GetOrderBy()) +} + +func TestSpaceMetricsSummary_OrderByKeptAcrossPages(t *testing.T) { + env := testutil.NewTestEnv(t) + env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls. + OnCall(0, func(_ context.Context, _ *serverlessmonitoringv1.GetSpaceSummaryMetricsRequest) (*serverlessmonitoringv1.GetSpaceSummaryMetricsResponse, error) { + return &serverlessmonitoringv1.GetSpaceSummaryMetricsResponse{ + Items: []*serverlessmonitoringv1.SpaceCollectionMetrics{{CollectionName: "big"}}, + NextPageToken: new("page-2"), + }, nil + }). + OnCall(1, func(_ context.Context, _ *serverlessmonitoringv1.GetSpaceSummaryMetricsRequest) (*serverlessmonitoringv1.GetSpaceSummaryMetricsResponse, error) { + return &serverlessmonitoringv1.GetSpaceSummaryMetricsResponse{ + Items: []*serverlessmonitoringv1.SpaceCollectionMetrics{{CollectionName: "small"}}, + }, nil + }) + + stdout, _, err := testutil.Exec(t, env, "serverless", "space", "metrics", "summary", "space-abc", "--order-by", "points") + require.NoError(t, err) + assert.Less(t, strings.Index(stdout, "big"), strings.Index(stdout, "small"), "the server's order is kept") + + reqs := env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls.All() + require.Len(t, reqs, 2) + for _, req := range reqs { + assert.Equal(t, serverlessmonitoringv1.SpaceCollectionMetricsOrder_SPACE_COLLECTION_METRICS_ORDER_POINT_COUNT_DESC, req.GetOrderBy()) + } +} + +func TestSpaceMetricsSummary_OrderByInvalid(t *testing.T) { + env := testutil.NewTestEnv(t) + + _, _, err := testutil.Exec(t, env, "serverless", "space", "metrics", "summary", "space-abc", "--order-by", "size") + require.Error(t, err) + assert.Contains(t, err.Error(), `invalid --order-by "size"`) + assert.Equal(t, 0, env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls.Count()) +} + +func TestSpaceMetricsSummary_OrderByWithTotalsOnly(t *testing.T) { + env := testutil.NewTestEnv(t) + + _, _, err := testutil.Exec(t, env, "serverless", "space", "metrics", "summary", "space-abc", "--order-by", "points", "--totals-only") + require.Error(t, err) + assert.Contains(t, err.Error(), "order-by") + assert.Equal(t, 0, env.ServerlessMonitoringServer.GetSpaceSummaryMetricsCalls.Count()) +}