Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/reference/qcloud_serverless_space_metrics_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions internal/cmd/serverless/space/metrics_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions internal/cmd/serverless/space/metrics_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 {
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
}
Expand Down
81 changes: 81 additions & 0 deletions internal/cmd/serverless/space/metrics_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package space_test
import (
"context"
"encoding/json"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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())
}