@@ -20,6 +20,7 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"io"
23
+ "net/http"
23
24
internalHTTP "pb/pkg/http"
24
25
"strconv"
25
26
"strings"
@@ -200,6 +201,14 @@ var StatStreamCmd = &cobra.Command{
200
201
return err
201
202
}
202
203
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
+
203
212
// Check output format
204
213
output , _ := cmd .Flags ().GetString ("output" )
205
214
if output == "json" {
@@ -211,8 +220,9 @@ var StatStreamCmd = &cobra.Command{
211
220
"storage_size" : humanize .Bytes (uint64 (storageSize )),
212
221
"compression_ratio" : fmt .Sprintf ("%.2f%%" , compressionRatio ),
213
222
},
214
- "retention" : retention ,
215
- "alerts" : alertsData .Alerts ,
223
+ "retention" : retention ,
224
+ "alerts" : alertsData .Alerts ,
225
+ "stream_type" : streamType ,
216
226
}
217
227
218
228
jsonData , err := json .MarshalIndent (data , "" , " " )
@@ -227,13 +237,13 @@ var StatStreamCmd = &cobra.Command{
227
237
isRetentionSet := len (retention ) > 0
228
238
isAlertsSet := len (alertsData .Alerts ) > 0
229
239
240
+ // Render the info section with consistent alignment
230
241
fmt .Println (StyleBold .Render ("\n Info:" ))
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 )
237
247
fmt .Println ()
238
248
239
249
if isRetentionSet {
@@ -412,7 +422,7 @@ func fetchStats(client *internalHTTP.HTTPClient, name string) (data StreamStatsD
412
422
}
413
423
414
424
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 )
416
426
if err != nil {
417
427
return
418
428
}
@@ -439,7 +449,7 @@ func fetchRetention(client *internalHTTP.HTTPClient, name string) (data StreamRe
439
449
}
440
450
441
451
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 )
443
453
if err != nil {
444
454
return
445
455
}
@@ -464,3 +474,45 @@ func fetchAlerts(client *internalHTTP.HTTPClient, name string) (data AlertConfig
464
474
}
465
475
return
466
476
}
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\n Status Code: %d\n Response: %s\n " , resp .StatusCode , body )
517
+ return "" , errors .New (errMsg )
518
+ }
0 commit comments