From c0d98474f9b85942e2bb9537977f8093f4bea670 Mon Sep 17 00:00:00 2001 From: till Date: Sun, 24 Mar 2024 12:41:48 +0100 Subject: [PATCH] Update: basic pagination to return everything --- internal/cmd/stats.go | 74 ++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/internal/cmd/stats.go b/internal/cmd/stats.go index 0da8514..9798f02 100644 --- a/internal/cmd/stats.go +++ b/internal/cmd/stats.go @@ -2,7 +2,9 @@ package cmd import ( "fmt" + "log/slog" "os" + "strings" "github.com/Luzilla/acronis-s3-usage/internal/utils" "github.com/Luzilla/acronis-s3-usage/pkg/ostor" @@ -19,39 +21,67 @@ type stat struct { Uploaded int } -func List(cCtx *cli.Context) error { +func ShowStats(cCtx *cli.Context) error { client := cCtx.Context.Value(OstorClient).(*ostor.Ostor) - items, err := client.List() - if err != nil { - return err - } + // fmt.Printf("from: %s\n", cCtx.Timestamp("from").String()) - fmt.Printf("Found %d objects\n", items.Count) - fmt.Printf("Truncated: %t\n", items.Truncated) + keep := map[string]stat{} + var after *string - if items.Count == 0 { - fmt.Println("no items") - return nil - } + // this is for pure display + page := 0 - keep := map[string]stat{} + for { - for _, obj := range items.Items { - // fmt.Println(obj) - usage, err := client.ObjectUsage(obj) + if after != nil { + slog.Debug("next page", slog.String("after", *after)) + if strings.Contains(*after, "1970") { + break + } + } + + page++ + items, err := client.List(after) if err != nil { - fmt.Println("usage: " + err.Error()) - os.Exit(2) + return err + } + + slog.Debug("found items", slog.Int("page", page), slog.Int("items", items.Count), slog.Bool("truncated", items.Truncated)) + + if items.Count == 0 { + break } - for _, item := range usage.Items { - var b = item.Key.Bucket - if _, ok := keep[b]; !ok { - keep[b] = stat{} + for _, obj := range items.Items { + // fmt.Println(obj) + usage, err := client.ObjectUsage(obj) + if err != nil { + fmt.Println("usage: " + err.Error()) + os.Exit(2) + } + + for _, item := range usage.Items { + var b = item.Key.Bucket + if _, ok := keep[b]; !ok { + keep[b] = stat{} + } + keep[b] = addToStruct(keep[b], item.Counters.Operations, item.Counters.Net) } - keep[b] = addToStruct(keep[b], item.Counters.Operations, item.Counters.Net) + + after = &obj + } + + // end of the pagination - no more records + if !items.Truncated { + break } + + } + + if len(keep) == 0 { + slog.Info("no stats") + return nil } tbl := table.New("Bucket", "Put", "Get", "List", "Other", "Downloaded", "Uploaded")