Go CLI: add admin_command response table funtion (#16454)

### Summary

Go CLI: add admin_command  response table funtion

---------

Co-authored-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
maoyifeng
2026-06-30 00:25:39 +08:00
committed by GitHub
parent 6370fce3f0
commit 5276baf1f9
2 changed files with 83 additions and 13 deletions

View File

@@ -2437,16 +2437,16 @@ func (c *CLI) AdminShowDataSummaryCommand(cmd *Command) (ResponseIf, error) {
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to get users summary: %w", err)
return nil, fmt.Errorf("failed to get data summary: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get users summary: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
return nil, fmt.Errorf("failed to get data summary: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
var result OrderedCommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("get users summary failed: invalid JSON (%w)", err)
return nil, fmt.Errorf("get data summary failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
@@ -2474,7 +2474,7 @@ func (c *CLI) AdminShowDataOrphanCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to get orphan data: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
var result OrderedCommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("get orphan data failed: invalid JSON (%w)", err)
}
@@ -2504,7 +2504,7 @@ func (c *CLI) AdminShowDataStorageCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to get data storage: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
var result OrderedCommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("get data storage failed: invalid JSON (%w)", err)
}
@@ -2534,7 +2534,7 @@ func (c *CLI) AdminShowDataIndexCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to get data index: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
var result OrderedCommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("get data index failed: invalid JSON (%w)", err)
}
@@ -2564,7 +2564,7 @@ func (c *CLI) AdminShowQuotaSummaryCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to get users quota summary: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
var result QuotaSummaryResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("get users quota summary failed: invalid JSON (%w)", err)
}
@@ -2587,16 +2587,16 @@ func (c *CLI) AdminShowTasksSummaryCommand(cmd *Command) (ResponseIf, error) {
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to get users quota summary: %w", err)
return nil, fmt.Errorf("failed to get tasks summary: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get users quota summary: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
return nil, fmt.Errorf("failed to get tasks summary: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
var result OrderedCommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("get users quota summary failed: invalid JSON (%w)", err)
return nil, fmt.Errorf("get tasks summary failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
@@ -2786,7 +2786,7 @@ func (c *CLI) AdminListUserIngestionTasksCommand(cmd *Command) (ResponseIf, erro
return nil, fmt.Errorf("failed to list user %s ingestion tasks: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body))
}
var result CommonResponse
var result OrderedCommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list user %s ingestion tasks failed: invalid JSON (%w)", userName, err)
}

View File

@@ -1173,3 +1173,73 @@ func (r *OrderedCommonDataResponse) PrintOut() {
fmt.Printf("%d, %s\n", r.Code, r.Message)
}
}
type QuotaSummaryResponse struct {
CommonDataResponse
}
func (r *QuotaSummaryResponse) Type() string {
return "quota_summary"
}
func (r *QuotaSummaryResponse) TimeCost() float64 {
return r.Duration
}
func (r *QuotaSummaryResponse) SetOutputFormat(format OutputFormat) {
r.OutputFormat = format
}
func (r *QuotaSummaryResponse) PrintOut() {
if r.Code != 0 {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
return
}
sections := []struct {
key string
title string
columns []string
}{
{"storage", "Storage", []string{"Plan", "Users", "Avg Used", "Limit", "Avg Usage"}},
{"apps", "Apps", []string{"Plan", "Avg Used", "Limit", "Avg Usage"}},
{"api", "API Requests", []string{"Plan", "Tokens", "Limit/min"}},
}
for i, section := range sections {
if i > 0 {
fmt.Println()
}
fmt.Printf("--- %s ---\n", section.title)
rowsRaw, ok := r.Data[section.key]
if !ok {
fmt.Println("No data")
continue
}
rows, ok := rowsRaw.([]interface{})
if !ok || len(rows) == 0 {
fmt.Println("No data")
continue
}
table := make([]map[string]interface{}, 0, len(rows))
for _, row := range rows {
if m, ok := row.(map[string]interface{}); ok {
orderedRow := make(map[string]interface{})
for _, col := range section.columns {
if v, exists := m[col]; exists {
orderedRow[col] = v
} else {
orderedRow[col] = ""
}
}
table = append(table, orderedRow)
}
}
PrintTableSimpleByFormatWithOrder(table, section.columns, r.OutputFormat)
}
}