Skip to content

Commit c75a391

Browse files
authored
add stream info to the output (#78)
1 parent 26ae193 commit c75a391

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

cmd/stream.go

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"io"
23+
"net/http"
2324
internalHTTP "pb/pkg/http"
2425
"strconv"
2526
"strings"
@@ -200,6 +201,14 @@ var StatStreamCmd = &cobra.Command{
200201
return err
201202
}
202203

204+
// Fetch stream type
205+
streamType, err := fetchInfo(&client, name)
206+
if err != nil {
207+
// Capture error
208+
cmd.Annotations["errors"] = fmt.Sprintf("Error: %s", err.Error())
209+
return err
210+
}
211+
203212
// Check output format
204213
output, _ := cmd.Flags().GetString("output")
205214
if output == "json" {
@@ -211,8 +220,9 @@ var StatStreamCmd = &cobra.Command{
211220
"storage_size": humanize.Bytes(uint64(storageSize)),
212221
"compression_ratio": fmt.Sprintf("%.2f%%", compressionRatio),
213222
},
214-
"retention": retention,
215-
"alerts": alertsData.Alerts,
223+
"retention": retention,
224+
"alerts": alertsData.Alerts,
225+
"stream_type": streamType,
216226
}
217227

218228
jsonData, err := json.MarshalIndent(data, "", " ")
@@ -227,13 +237,13 @@ var StatStreamCmd = &cobra.Command{
227237
isRetentionSet := len(retention) > 0
228238
isAlertsSet := len(alertsData.Alerts) > 0
229239

240+
// Render the info section with consistent alignment
230241
fmt.Println(StyleBold.Render("\nInfo:"))
231-
fmt.Printf(" Event Count: %d\n", ingestionCount)
232-
fmt.Printf(" Ingestion Size: %s\n", humanize.Bytes(uint64(ingestionSize)))
233-
fmt.Printf(" Storage Size: %s\n", humanize.Bytes(uint64(storageSize)))
234-
fmt.Printf(
235-
" Compression Ratio: %.2f%s\n",
236-
compressionRatio, "%")
242+
fmt.Printf(" %-18s %d\n", "Event Count:", ingestionCount)
243+
fmt.Printf(" %-18s %s\n", "Ingestion Size:", humanize.Bytes(uint64(ingestionSize)))
244+
fmt.Printf(" %-18s %s\n", "Storage Size:", humanize.Bytes(uint64(storageSize)))
245+
fmt.Printf(" %-18s %.2f%s\n", "Compression Ratio:", compressionRatio, "%")
246+
fmt.Printf(" %-18s %s\n", "Stream Type:", streamType)
237247
fmt.Println()
238248

239249
if isRetentionSet {
@@ -412,7 +422,7 @@ func fetchStats(client *internalHTTP.HTTPClient, name string) (data StreamStatsD
412422
}
413423

414424
func fetchRetention(client *internalHTTP.HTTPClient, name string) (data StreamRetentionData, err error) {
415-
req, err := client.NewRequest("GET", fmt.Sprintf("logstream/%s/retention", name), nil)
425+
req, err := client.NewRequest(http.MethodGet, fmt.Sprintf("logstream/%s/retention", name), nil)
416426
if err != nil {
417427
return
418428
}
@@ -439,7 +449,7 @@ func fetchRetention(client *internalHTTP.HTTPClient, name string) (data StreamRe
439449
}
440450

441451
func fetchAlerts(client *internalHTTP.HTTPClient, name string) (data AlertConfig, err error) {
442-
req, err := client.NewRequest("GET", fmt.Sprintf("logstream/%s/alert", name), nil)
452+
req, err := client.NewRequest(http.MethodGet, fmt.Sprintf("logstream/%s/alert", name), nil)
443453
if err != nil {
444454
return
445455
}
@@ -464,3 +474,45 @@ func fetchAlerts(client *internalHTTP.HTTPClient, name string) (data AlertConfig
464474
}
465475
return
466476
}
477+
478+
func fetchInfo(client *internalHTTP.HTTPClient, name string) (streamType string, err error) {
479+
// Create a new HTTP GET request
480+
req, err := client.NewRequest(http.MethodGet, fmt.Sprintf("logstream/%s/info", name), nil)
481+
if err != nil {
482+
return "", fmt.Errorf("failed to create request: %w", err)
483+
}
484+
485+
// Execute the request
486+
resp, err := client.Client.Do(req)
487+
if err != nil {
488+
return "", fmt.Errorf("request execution failed: %w", err)
489+
}
490+
defer resp.Body.Close()
491+
492+
// Read the response body
493+
bytes, err := io.ReadAll(resp.Body)
494+
if err != nil {
495+
return "", fmt.Errorf("failed to read response body: %w", err)
496+
}
497+
498+
// Check for successful status code
499+
if resp.StatusCode == http.StatusOK {
500+
// Define a struct to parse the response
501+
var response struct {
502+
StreamType string `json:"stream_type"`
503+
}
504+
505+
// Unmarshal JSON into the struct
506+
if err := json.Unmarshal(bytes, &response); err != nil {
507+
return "", fmt.Errorf("failed to unmarshal response: %w", err)
508+
}
509+
510+
// Return the extracted stream_type
511+
return response.StreamType, nil
512+
}
513+
514+
// Handle non-200 responses
515+
body := string(bytes)
516+
errMsg := fmt.Sprintf("Request failed\nStatus Code: %d\nResponse: %s\n", resp.StatusCode, body)
517+
return "", errors.New(errMsg)
518+
}

0 commit comments

Comments
 (0)